Python *** 作ES

Python *** 作ES,第1张

概述 环境使用pip安装elasticsearch包:pipinstallelasticsearch==7.7.0 使用引包这里分别介绍使用elasticsearch包和request包查询ES的方式: 使用request包可以补充elasticsearch包里不方便或者还没有实现的功能,作为对elasticsearch包的一个补充,建议组合使用。 from

 

环境

使用pip安装elasticsearch包:

pip install elasticsearch==7.7.0
 使用引包

这里分别介绍使用elasticsearch包和request包查询ES的方式: 

使用request包可以补充elasticsearch包里不方便或者还没有实现的功能,作为对elasticsearch包的一个补充,建议组合使用。

 from elasticsearch import Elasticsearch import requestsElasticSearch包 获取ES对象   #获取es连接 def get_es_engine(host,port,user=None,pwd=None):  if user and pwd:  es = Elasticsearch(host+':'+str(port), http_auth=(user, pwd), maxsize=15) # 有XPACK安全认证的ES集群  else:  es = Elasticsearch(host+':'+str(port), maxsize=15)#无安全认证的集群  return es状态 es.Ping() es.info()

 

查询 #查单个记录,指定index,type,ID es.get(index='test2',ID=1,doc_type='_doc')   #根据条件查询,body里是DSL bd={  "query":{  "bool":{  "should": [  {  "match_phrase_prefix":{  "email":"yikai"  }  }  ]  }  } } es.search(body=bd,index='test2')

 

 #查看索引数据是否存在 es.exists(index='test2',ID=1,doc_type='_doc') Out[132]: True es.exists(index='test2',ID=2,doc_type='_doc') Out[133]: False

更新

指定ID单条更新:

 #指定ID进行更新单条记录 data={  "doc":{  "age":77  } } es.update(index='test2',ID=3,doc_type='_doc',body=data)

根据DSL条件批量更新:

 data_all={  "query": {  "match_all": {}   },   "script": {  "source": "ctx._source.age = params.age;",  "lang": "painless",  "params" : {  "age": "88"  }  } } es.update_by_query(index='test2',body=data_all)   #语法参考 #https://www.elastic.co/guIDe/en/elasticsearch/painless/current/index.HTML

新增

插入一条记录:

 data_ins={  "name" : "Rick.Wang",  "company" : "CSDN",  "age" : "10",  "email" : "[email protected]"  }   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:

 trunc={  "query": {"match_all": {}} } es.delete_by_query(index='test2',body=trunc)

使用BulK命令批量 *** 作

批量插入

 #JsON数据不能有回车换行 batch_data= [  {"index": {}},  {"name": "王义凯", "age": 11, "email":"[email protected]", "company":"CSDN1"},  {"index": {}},  {"name": "wang,yi-kai", "age": 22, "email":"[email protected]", "company":"CSDN2"},  {"index": {}},  {"name": "Rick.Wang", "age": 33, "email":"[email protected]", "company":"CSDN3"},  {"index": {}},  {"name": "义凯王", "age": 44, "email":"[email protected]", "company":"CSDN4"},  ] es.bulk(index='test2',doc_type='_doc',body=batch_data)

 

批量插入更新删除

 使用bulk命令可批量对不同的索引进行插入更新删除等 *** 作:

 #批量对不同的索引进行增删改查 *** 作,每个Json一行 batch_action=[  {"index": {"_index": "test2", "_type": "_doc", "_ID": "999"}},  {"name": "rick99", "age": 99, "email":"[email protected]", "company":"CSDN9" },  {"index": {"_index": "test2", "_type": "_doc", "_ID": "888"}},  {"name": "rick88", "age": 88, "email":"[email protected]", "company":"CSDN8" },  {"delete": {"_index": "test2", "_type": "_doc", "_ID": "999"}},  {"create": {"_index" : "test2", "_type" : "_doc", "_ID": "000"}},  {"name": "rick00", "age": 100, "email":"[email protected]", "company":"CSDN0" },  {"update": {"_index": "test2", "_type": "_doc", "_ID": "888"}},  {"doc": {"age": "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数据:

 import requests es_http = 'http://localhost:9200' index='test2' type='_doc' ID='888' auth=('elastic','r12345635x') #tuple格式的账号密码,如果没有开启xpack安全认证可忽略此参数   #查询指定的ID数据 res=requests.get(es_http+'/'+index+'/'+type+'/'+ID,auth=auth) #如果没有安全认证则不需要auth参数 res.text   #查询该索引下所有数据 res=requests.get(es_http+'/'+index+'/_search',auth=auth) res.text   #使用DSL查询数据 bd={  "query":{  "bool":{  "should": [  {  "match_phrase_prefix":{  "name":"rick.wang"  }  }  ]  }  } } res=requests.get(es_http+'/'+index+'/_search/?pretty',auth=auth,Json=bd)#pretty是为了格式化Json样式,看起来更好看,可以忽略 print(res.text)

POST

使用POST方法可以与在Kibana中进行一样的 *** 作,比如插入一条记录,比如根据DSL批量更新: 

 #使用POST方法往ES中插入数据 data={"name": "rick999", "age": 999, "email":"[email protected]", "company":"CSDN999" } res = requests.post(es_http+'/'+index+'/_doc/999',auth=auth,Json=data) res.text   res = requests.get(es_http+'/'+index+'/_doc/999',auth=auth) res.text     #使用POST方法根据DSL对ES进行 *** 作 bd={  "query": {  "match_all": {}   },   "script": {  "source": "ctx._source.age = params.age;",  "lang": "painless",  "params" : {  "age": "99"  }  } } res = requests.post(es_http+'/'+index+'/_update_by_query',auth=auth,Json=bd) res.text

@H_494_502@

PUT

使用PUT可以创建索引

 res = requests.put(es_http+'/'+'new_index',auth=auth) res.text

DELETE

使用DELETE方法可以删除索引 删除数据等

 #往新创建的索引里插入一条记录 data={"name": "rick999", "age": 999, "email":"[email protected]", "company":"CSDN999" } requests.post(es_http+'/'+'new_index'+'/_doc/999',auth=auth,Json=data)  #判断ID为999的是否存在 es.exists(index='new_index',ID=999,doc_type='_doc')   #使用DELETE方法删除ID为999的记录 requests.delete(es_http+'/'+'new_index'+'/_doc/999',auth=auth) #判断ID为999的是否存在 es.exists(index='new_index',ID=999,doc_type='_doc')   #使用DELETE方法删除new_index的索引 res=requests.delete(es_http+'/'+'new_index',auth=auth) #删除索引 res.text

 

 

 

总结

以上是内存溢出为你收集整理的Python *** 作ES全部内容,希望文章能够帮你解决Python *** 作ES所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1183642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存