python 操作 Elasticsearch7.x版本

1|0python 操作 Elasticsearch7.x版本

1|11. 连接Elasticsearch

from elasticsearch import Elasticsearch # 连接到Elasticsearch es = Elasticsearch( ['127.0.0.1:9200'], # 在做任何操作之前,先进行嗅探 # sniff_on_start=True, # # 节点没有响应时,进行刷新,重新连接 sniff_on_connection_fail=True, # # 每 60 秒刷新一次 sniffer_timeout=60 )

1|22. 索引操作

from elasticsearch import Elasticsearch # 连接到Elasticsearch es = Elasticsearch( ['127.0.0.1:9200'], # 在做任何操作之前,先进行嗅探 # sniff_on_start=True, # # 节点没有响应时,进行刷新,重新连接 sniff_on_connection_fail=True, # # 每 60 秒刷新一次 sniffer_timeout=60 ) request_body = { 'mappings': { 'properties': { 'name': { 'type': 'text' }, 'id': { 'type': 'integer' }, } } } index_name = 'my_index' # 1. 创建索引 # 为防止在创建索引的时候出现重复,产生错误,在创建之前最好判断一下索引是否存在 # 索引存在,先删除索引 if es.indices.exists(index_name): pass else: print('索引不存在,可以创建') es.indices.create(index=index_name, body=request_body, ignore=400) # 2. 查看索引的信息 print(es.info()) # 3. 删除索引 es.indices.delete(index=index_name, ignore=[400, 404]) """ ignore参数通常用于控制是否忽略不存在的文档或索引时的错误。这个参数通常在执行删除操作或者在获取文档时使用。 ignore参数可以设置为True或False。当设置为True时,如果尝试删除不存在的文档或获取不存在的文档,Elasticsearch将不会引发错误,而是简单地返回一个空的响应或者指示文档不存在。当设置为False时,如果文档或索引不存在,Elasticsearch将引发错误。 请注意,ignore参数还能接收一个错误代码列表,其中包括应被忽略的HTTP错误代码。400是访问的页面域名不存在或者请求错误,404是“未找到”错误的代码,意味着如果尝试删除的文档或索引不存在,Elasticsearch将不会引发异常 """

1|33. 文档操作

  • es7,doc_type 参数可以不传,也可以查询到
# 1. 添加一个文档到索引 doc = { 'name': 'John Doe', 'age': 30, 'about': 'I love to go rock climbing' } results1 = es.index(index='my_index', id=1, document=doc) # 2. 获取一个文档 result2 = es.get(index='my_index', id=1) print(result2['_source']) # 3. 更新一个文档 doc['about'] = 'I now love pasta' results3 = es.update(index='my_index', id=1, document=doc) # 4. 搜索文档 results4 = es.search(index='my_index', query={'match': {'about': 'climbing'}}) # 也可以用body格式的参数进行搜索 for hit in results4['hits']['hits']: print(hit) # 5. 删除文档 results5 = es.delete(index="users", id=doc_id) print(results5['result'])

1|44. 查询/搜索文档

1|04.1 body 和 query参数

""" 在Python中使用Elasticsearch 7.x版本时,query参数和body参数的主要区别在于它们的用途。 query参数:通常用于定义查询条件,适用于查询API中查询请求的一部分,例如使用search方法时,你可以将查询条件传递给query参数。 body参数:通常用于定义更复杂的请求体,例如包括查询、聚合、脚本操作等高级功能。在Elasticsearch中,body参数是一个JSON格式的字符串,用于传递更多的查询参数和定义复杂的查询结构 """ from elasticsearch import Elasticsearch # 假设ES地址为 http://localhost:9200 es = Elasticsearch("http://localhost:9200") # 使用query参数的查询 query = { "match": { "title": "python" } } # 执行查询 response = es.search(index="my_index", query=query) # 使用body参数的查询 body = { "query": { "match": { "title": "python" } } } # 执行查询 response = es.search(index="my_index", body=body)

1|04.2 其他查询示例

from elasticsearch import Elasticsearch es = Elasticsearch(['127.0.0.1:9200']) index_name = 'student' # -------------------------------------------查询 # ------------------------查询所有数据 dsl = { "query":{ "match_all":{} } } res = es.search(index=index_name, body=dsl) print(res) # ------------------------等于查询,term与terms # term: 查询 xx = “xx” dsl = { "query": { "term": { "name": "张三" } } } res = es.search(index=index_name, body=dsl) # 查询name="张三"的所有数据 print(res) # terms: 查询 xx = “xx” 或 xx = “yy” dsl = { "query": { "terms": { "name": [ "张三", "李四" ] } } } res = es.search(index=index_name, body=dsl) # 查询 name="张三" 或 name="李四" 的所有数据 print(res) # ------------------------包含查询,match与multi_match dsl = { "query": { "match": { "name": "张三" } } } res = es.search(index=index_name, body=dsl) # match: 匹配name包含"张三"关键字的数据 print(res) dsl = { "query": { "multi_match": { "query": "李四", "fields": ["name", "addr"] } } } res = es.search(index=index_name, body=dsl) # multi_match: 在name和addr里匹配包含 李四 关键字的数据 print(res) # ------------------------ids dsl = { "query": { "ids": { # "type": "", "values": [ "1", "2" ] } } } res = es.search(index=index_name, body=dsl) # 搜索出id为1或2的所有数据 print(res) # ------------------------复合查询bool # bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足) dsl = { "query": { "bool": { "must": [ { "term": { "name": "张三" } }, { "term": { "age": "16" # 实际张三是15岁,故而未命中 } } ] } } } res = es.search(index=index_name, body=dsl) # 获取name="张三"并且age=16的所有数据 print(res) # -----------------------切片式查询 dsl = { "query":{ "match_all":{} }, "from":0, # 从第0条数据开始 "size":1 # 获取1条数据 } res = es.search(index=index_name, body=dsl) # 从第0条数据开始,获取1条数据 print(res) # -----------------------范围查询 dsl = { "query": { "range": { "age": { "gte": 18, # >=18 "lte": "30" # <=30 } } } } res = es.search(index=index_name, body=dsl) # 查询前缀为"李"的所有数据 print(res) # -----------------------前缀查询 dsl = { "query": { "prefix": { "name": "李" } } } res = es.search(index=index_name, body=dsl) # 查询前缀为"李"的所有数据 print(res) # -----------------------通配符查询 dsl = { "query": { "wildcard": { "name": "*三" } } } res = es.search(index=index_name, body=dsl) # 查询name以三为后缀的所有数据 print(res) #---------------------------排序 dsl = { "query":{ "match_all":{} }, "sort":{ "age":{ # 根据age字段升序排序 "order":"asc" # asc升序,desc降序 } } } res = es.search(index=index_name, body=dsl) print(res) dsl = { "query":{ "match_all":{} }, "sort":[ { "age":{ # 先根据age字段升序排序 "order":"asc" # asc升序,desc降序 } }, { "name":{ # 后根据name字段升序排序 "order":"asc" # asc升序,desc降序 } } ], } # 多字段排序,注意顺序!写在前面的优先排序 res = es.search(index=index_name, body=dsl) print(res) # --------------------------filter_path, 响应过滤 # 只需要获取_id数据,多个条件用逗号隔开 # res = es.search(index=index_name, filter_path=["hits.hits._id"]) # # 只需要获取_id数据,多个条件用逗号隔开 res = es.search(index=index_name, filter_path=["hits.hits._*"]) # # 获取所有数据 print(res) # ------------------count, 执行查询并获取该查询的匹配数 # 获取数据量 res = es.count(index=index_name) print(res) # ---------------------度量类聚合 # 获取最小值 dsl = { "query":{ "match_all":{} }, "aggs":{ # 聚合查询 "min_age":{ # 最小值的key "min":{ # 最小 "field":"age" # 查询"age"的最小值 } } } } res = es.search(index=index_name, body=dsl) # # 搜索所有数据,并获取age最小的值 print(res) # 获取最大值 dsl = { "query":{ "match_all":{} }, "aggs":{ # 聚合查询 "max_age":{ # 最大值的key "max":{ # 最大 "field":"age" # 查询"age"的最大值 } } } } res = es.search(index=index_name, body=dsl) # # 搜索所有数据,并获取age最大的值 print(res) # 获取和 dsl ={ "query":{ "match_all":{} }, "aggs":{ # 聚合查询 "sum_age":{ # 和的key "sum":{ # 和 "field":"age" # 获取所有age的和 } } } } res = es.search(index=index_name, body=dsl) # 搜索所有数据,并获取所有age的和 print(res) # 获取平均值 dsl = { "query":{ "match_all":{} }, "aggs":{ # 聚合查询 "avg_age":{ # 平均值的key "sum":{ # 平均值 "field":"age" # 获取所有age的平均值 } } } } res = es.search(index=index_name, body=dsl) print(res) # from、size dsl = { "query":{ "match_all":{} }, "size":"50", "from":"0" } res = es.search(index=index_name, body=dsl) print(res)

__EOF__

本文作者BigSun丶
本文链接https://www.cnblogs.com/Mcoming/p/18129071.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   BigSun丶  阅读(867)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示