python-elasticsearch
Python Elasticsearch客户端文档:https://elasticsearch-py.readthedocs.io/en/master/index.html
1、安装Elasticsearch包
pip install elasticsearch
2、连接ES
from elasticsearch import Elasticsearch # 连接ES es = Elasticsearch([{'host': 'localhost', 'port': 9200}], timeout=3600)
用户名认证:
from elasticsearch import Elasticsearch # 连接ES es = Elasticsearch([{'host': 'localhost', 'port': 9200}], http_auth=('elastic', 'XcF8EbPPmgRgiLqoVAcI'), timeout=3600)
3、Elasticsearch操作
from elasticsearch import Elasticsearch # 连接ES es = Elasticsearch([{'host': 'localhost', 'port': 9200}], http_auth=('elastic', 'XcF8EbPPmgRgiLqoVAcI'), timeout=3600) # 查询 query = { "query": { "match_all": {} } } result = es.search(index="megacorp", body=query) print(result) # 插入 es.index(index="megacorp", body={"first_name": "xiao", "last_name": "mi", 'age': 25, 'about': 'I love to go rock climbing', 'interests': ['phone', 'tv']}) # 删除 es.delete(index='megacorp', id='24dYV34Bin_Y48vkEG7y') # 更新 # 修改last_name字段 # 修改部分字段 doc_body = { "doc": {"last_name": "yongjie"} } # 增加字段 doc_body = { 'script': "ctx._source.address = '南京'" } # 根据id更新 es.update(index="megacorp", id=1, body=doc_body) # 更新所有满足条件的文档 query = { "query": { "match": { "last_name": "Smith" } }, "script": { "ctx._source.address = '南京'", } } result = es.update_by_query(index="megacorp", body=query) print(result)
bulk操作:
helpers:
from elasticsearch import Elasticsearch, helpers
# 连接ES
es = Elasticsearch([{'host': 'localhost', 'port': 9200}], http_auth=('elastic', 'XcF8EbPPmgRgiLqoVAcI'), timeout=3600)
actions = [
{
'_op_type': 'index', # 操作 index update create delete
'_index': 'index-name',
'_type': 'doc',
'_source': {'level': 'info'}
},
{
'_op_type': 'delete',
'_index': 'index-name',
'_id': 42,
},
{
'_op_type': 'update',
'_index': 'index-name',
'_id': 42,
'doc': {'question': 'The life, universe and everything.'}
}
]
# 使用bulk方式
helpers.bulk(client=es, actions=actions)
# streaming_bulk与parallel_bulk类似 需要遍历才会运行
# 都可以设置每个批次的大小,parallel_bulk还可以设置线程数
for ok, response in helpers.streaming_bulk(es, actions):
if not ok:
print(response)
RESTful:
resp = client.bulk(
body=[
{"index": {"_index": "test", "_id": "1"}},
{"field1": "value1"},
{"delete": {"_index": "test", "_id": "2"}},
{"create": {"_index": "test", "_id": "3"}},
{"field1": "value3"},
{"update": {"_id": "1", "_index": "test"}},
{"doc": {"field2": "value2"}},
],
)
print(resp)
END.