环境
使用pip安装elasticsearch包:
pip install elasticsearch==7.7.0
使用
引包
这里分别介绍使用elasticsearch包和request包查询ES的方式:
使用request包可以补充elasticsearch包里不方便或者还没有实现的功能,作为对elasticsearch包的一个补充,建议组合使用。
-
from elasticsearch import Elasticsearch
-
ElasticSearch包
获取ES对象
-
-
-
def get_es_engine(host,port,user=None,pwd=None):
-
-
es = Elasticsearch(host+‘:‘+str(port), http_auth=(user, pwd), maxsize=15)
-
-
es = Elasticsearch(host+‘:‘+str(port), maxsize=15)
-
状态
-
-
查询
-
-
es.get(index=‘test2‘,id=1,doc_type=‘_doc‘)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
es.search(body=bd,index=‘test2‘)
-
-
es.exists(index=‘test2‘,id=1,doc_type=‘_doc‘)
-
-
es.exists(index=‘test2‘,id=2,doc_type=‘_doc‘)
-

更新
指定ID单条更新:
-
-
-
-
-
-
-
es.update(index=‘test2‘,id=3,doc_type=‘_doc‘,body=data)

根据DSL条件批量更新:
-
-
-
-
-
-
"source": "ctx._source.age = params.age;",
-
-
-
-
-
-
-
es.update_by_query(index=‘test2‘,body=data_all)
-
-
-

新增
插入一条记录:
-
-
-
-
-
"email" : "wangyikai@csdn.com"
-
-
-
es.index(index=‘test2‘,body=data_ins,doc_type=‘_doc‘,id=8)

删除
指定ID删除记录:
es.delete(index=‘test2‘,doc_type=‘_doc‘,id=8)

根据DSL条件批量删除:
-
bd= {‘query‘: {‘bool‘: {‘should‘: [{‘match_phrase_prefix‘: {‘email‘: ‘yikai‘}}]}}}
-
-
es.delete_by_query(index=‘test2‘,body=bd)
清空
清空索引不删除索引,等同于关系型数据库里的truncate table:
-
-
"query": {"match_all": {}}
-
-
es.delete_by_query(index=‘test2‘,body=trunc)

使用BULK命令批量操作
批量插入
-
-
-
-
{"name": "王义凯", "age": 11, "email":"wangyikai1@csdn.com", "company":"CSDN1"},
-
-
{"name": "wang,yi-kai", "age": 22, "email":"wangyikai2@csdn.com", "company":"CSDN2"},
-
-
{"name": "Rick.Wang", "age": 33, "email":"wangyikai3@csdn.com", "company":"CSDN3"},
-
-
{"name": "义凯王", "age": 44, "email":"wangyikai4@csdn.com", "company":"CSDN4"},
-
-
es.bulk(index=‘test2‘,doc_type=‘_doc‘,body=batch_data)

批量插入更新删除
使用bulk命令可批量对不同的索引进行插入更新删除等操作:
-
-
-
{"index": {"_index": "test2", "_type": "_doc", "_id": "999"}},
-
{"name": "rick99", "age": 99, "email":"wangyikai9@csdn.com", "company":"CSDN9" },
-
{"index": {"_index": "test2", "_type": "_doc", "_id": "888"}},
-
{"name": "rick88", "age": 88, "email":"wangyikai8@csdn.com", "company":"CSDN8" },
-
{"delete": {"_index": "test2", "_type": "_doc", "_id": "999"}},
-
{"create": {"_index" : "test2", "_type" : "_doc", "_id": "000"}},
-
{"name": "rick00", "age": 100, "email":"wangyikai0@csdn.com", "company":"CSDN0" },
-
{"update": {"_index": "test2", "_type": "_doc", "_id": "888"}},
-
-
-
es.bulk(index=‘test2‘,doc_type=‘_doc‘,body=batch_action)


使用bulk批量操作的时候,对于不同的操作类型,一定要在前面加上与之对应的操作头信息({“index”: {}}, {‘delete’: {…}}, …),否则会报TransportError(400, u’illegal_argument_exception’)的错误。
Request包
前面介绍过ES支持Restful接口,我们可以使用curl命令对其进行操作,同样我们也可以使用python里的request包访问操作ES库。
GET查询
使用get函数查询ES数据:
-
-
es_http = ‘http://localhost:9200‘
-
-
-
-
auth=(‘elastic‘,‘r12345635x‘)
-
-
-
res=requests.get(es_http+‘/‘+index+‘/‘+type+‘/‘+id,auth=auth)
-
-
-
-
res=requests.get(es_http+‘/‘+index+‘/_search‘,auth=auth)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
res=requests.get(es_http+‘/‘+index+‘/_search/?pretty‘,auth=auth,json=bd)
-



POST
使用POST方法可以与在Kibana中进行一样的操作,比如插入一条记录,比如根据DSL批量更新:
-
-
data={"name": "rick999", "age": 999, "email":"wangyikai999@csdn.com", "company":"CSDN999" }
-
res = requests.post(es_http+‘/‘+index+‘/_doc/999‘,auth=auth,json=data)
-
-
-
res = requests.get(es_http+‘/‘+index+‘/_doc/999‘,auth=auth)
-
-
-
-
-
-
-
-
-
-
"source": "ctx._source.age = params.age;",
-
-
-
-
-
-
-
res = requests.post(es_http+‘/‘+index+‘/_update_by_query‘,auth=auth,json=bd)
-


PUT
使用PUT可以创建索引
-
res = requests.put(es_http+‘/‘+‘new_index‘,auth=auth)
-

DELETE
使用DELETE方法可以删除索引 删除数据等
-
-
data={"name": "rick999", "age": 999, "email":"wangyikai999@csdn.com", "company":"CSDN999" }
-
requests.post(es_http+‘/‘+‘new_index‘+‘/_doc/999‘,auth=auth,json=data)
-
-
es.exists(index=‘new_index‘,id=999,doc_type=‘_doc‘)
-
-
-
requests.delete(es_http+‘/‘+‘new_index‘+‘/_doc/999‘,auth=auth)
-
-
es.exists(index=‘new_index‘,id=999,doc_type=‘_doc‘)
-
-
-
res=requests.delete(es_http+‘/‘+‘new_index‘,auth=auth)
-
