目录
目录
es的curd
curl操作es
1、写入
POST _bulk
{"index":{"_index":"index_name-2021-09-15","_type":"doc","_id":"3"}}
{"phone" : "13810172311", "logtime" : "2021-09-15 00:00:01"}
2、update by query
Update By Query API | Elasticsearch Guide [7.14] | Elastic
POST my_index/_update_by_query?refresh
{
"script":{
"source":"ctx._source.flag='finish'", # 直接赋值,也可以"ctx._source.count++" 数值型计算
"lang":"painless"
},
"query":{
"term":{
"_id":"-KapDHwBHyKyjyAVIqyG"
}
}
}
refresh的目的是update之后立即刷新文档。
python操作es
connect es
http_auth=(es_user, es_password)
verify_certs=False # 确认没有加密证书
retry_on_timeout=True
max_retries=5
sniff_on_start=True,sniff_on_connection_fail=True,sniffer_timeout=600 # 增加嗅探机制
加入使用ssl
use_ssl=True #打开SSL
verify_certs=True#确保我们验证了SSL证书,默认是false
ca_cets="/path/to/CA_certs" # 提供CA证书的路径
client_cert='/path/to/clientcert.pem' # PEM格式的SSL客户端证书client_key='/path/to/clientkey.pem' # PEM格式的SSL客户端密钥
update_by_query
使用过程中遇到的问题:
version conflict,required seqNo [],primary term [],but no document was found
原因是存在了并发,version冲突,而实际上代码中没有并发场景,经排查是由于index所属的template中设置了refresh时间间隔为30s,导致update之后并没有及时更新document,所以在调用时加上参数refresh=True,即可避免这种问题。
es_service.update_by_query(index=index_name, doc_type="", body=update_body, refresh=True)
delete_by_query
query = {'query': {'range': {'age': {'lt': 11}}}}
es.delete_by_query(index='indexName', body=query, doc_type='typeName')
问题排查汇总
1、spark写入es:
org.elasticsearch.hadoop.rest.EsHadoopRemoteException:
cluster_block_exception: blocked by: [FORBIDDEN/8/index write (api)]
原因是index所属的生命周期中设置了warm阶段计时,到这个阶段index会被设置为只读。所以 forbidden write