您现在的位置是:网站首页> 编程资料编程资料
python中elasticsearch_dsl模块的使用方法_python_
2023-05-25
418人已围观
简介 python中elasticsearch_dsl模块的使用方法_python_
前言
elasticsearch-dsl是基于elasticsearch-py封装实现的,提供了更简便的操作elasticsearch的方法。
安装:
install elasticsearch_dsl

连接elasticsearch
from elasticsearch_dsl import connections, Search es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) print(es)
还可以通过alias给连接设置别名,后续可以通过别名来引用该连接,默认别名为default。
from elasticsearch_dsl import connections, Search # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) print(es) # 方式二:连接es connections.create_connection(alias="my_new_connection", hosts=["127.0.0.1:9200"], timeout=20)
elasticsearch_dsl.Search
search对象代表整个搜索请求,包括:queries、filters、aggregations、sort、pagination、additional parameters、associated client。
API被设置为可链接的即和用.连续操作。search对象是不可变的,除了聚合,对对象的所有更改都将导致创建包含该更改的浅表副本。
当初始化Search对象时,传递elasticsearch客户端作为using的参数
示例代码1:
from elasticsearch_dsl import connections, Search # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # 方式二:连接es connections.create_connection(alias="my_new_connection", hosts=["127.0.0.1:9200"], timeout=20) # 不使用别名使用 res = Search(using=es).index("test_index").query() # print(res) for data in res: print(data.to_dict()) print("*" * 100) # 使用别名后这样使用 res2 = Search(using="my_new_connection").index('test_index').query() # print(e) for data in res2: print(data.to_dict())运行结果:

示例代码2:
from elasticsearch_dsl import connections, Search # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # 不使用别名使用 res = Search(using=es).index("test_index").query() # print(res) for data in res: print(data.to_dict()) print("*" * 100) # 书写方式一:按条件查询数据 res2 = Search(using=es).index("test_index").query("match", name="张三") # 查询时注意分词器的使用 for data in res2: print(data.to_dict()) print("*" * 100) # 书写方式二:按条件查询数据 res3 = Search(using=es).index("test_index").query({"match": {"name": "张三"}}) for data in res3: print(data.to_dict())运行结果:

在上述执行execute方法将请求发送给elasticsearch:
response = res.execute()不需要执行execute()方法,迭代后可以通过to_dict()方法将Search对象序列化为一个dict对象,这样可以方便调试。
query方法
查询,参数可以是Q对象,也可以是query模块中的一些类,还可以是自已写上如何查询。
示例代码1:
from elasticsearch_dsl import connections, Search, Q import time # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) res = Search(using=es, index="test_index").query().query() # 当调用.query()方法多次时,内部会使用&操作符 print(res.to_dict())
运行结果:

filter方法
在过滤上下文中添加查询,可以使用filter()函数来使之变的简单。
示例代码1:
from elasticsearch_dsl import connections, Search, Q # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # res = Search(using=es).index("test_index").filter({"match": {"name": "北"}}) # res = Search(using=es).index("test_index").filter("terms", tags=["name", "id"]) res = Search(using=es).index("test_index").query("bool", filter=[ Q("terms", tags=["name", "id"])]) # 上面代码在背后会产生一个bool查询,并将指定的条件查询放入到filter分支 print(res) for data in res: print(data.to_dict())示例代码2:
from elasticsearch_dsl import connections, Search, Q import time # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # 范围查询 # res = Search(using=es, index="test_index").filter("range", timestamp={"gte": 0, "lt": time.time()}).query({"match": {"name": "北"}}) res = Search(using=es, index="test_index").filter("range", id={"gte": 1, "lte": 4}).query({"match": {"name": "北"}}) print(res) for data in res: print(data.to_dict()) # 普通过滤 res2 = Search(using=es, index="test_index").filter("terms", id=["2", "4"]).execute() print(res2) for data in res2: print(data.to_dict())运行结果:

示例代码3:
from elasticsearch_dsl import connections, Search, Q # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # 方式一 q = Q('range', age={"gte": 25, "lte": 27}) res = Search(using=es, index="account_info").query(q) print(res.to_dict()) for data in res: print(data.to_dict()) print("*" * 100) # 方式二 q2 = Q('range', **{"age": {"gte": 25, "lte": 27}}) res2 = Search(using=es, index="account_info").query(q2) print(res2.to_dict()) for data in res2: print(data.to_dict())运行结果:

index方法
指定索引
usring方法
指定哪个elasticsearch
elasticsearch_dsl.query
该库为所有的Elasticsearch查询类型都提供了类。以关键字参数传递所有的参数,最终会把参数序列化后传递给Elasticsearch,这意味着在原始查询和它对应的dsl之间有这一个清理的一对一的映射。
示例代码:
from elasticsearch_dsl import connections, Search, Q from elasticsearch_dsl.query import MultiMatch, Match # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # 相对与{"multi_match": {"query": "ha", "fields": ["firstname", "lastname"]}} m1 = MultiMatch(query="Ha", fields=["firstname", "lastname"]) res = Search(using=es, index="test_index").query(m1) print(res) for data in res: print(data.to_dict()) # 相当于{"match": {"firstname": {"query": "Hughes"}}} m2 = Match(firstname={"query": "Hughes"}) res = Search(using=es, index="test_index").query(m2) print(res) for data in res: print(data.to_dict())elasticsearch_dsl.Q
使用快捷方式Q通过命名参数或者原始dict类型数据来构建一个查询实例。Q的格式一般是Q("查询类型", 字段="xxx")或Q("查询类型", query="xxx", fields=["字段1", "字段2"])
示例代码1:
from elasticsearch_dsl import connections, Search, Q from elasticsearch_dsl.query import MultiMatch, Match # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # q = Q("match", city="Summerfield") q = Q("multi_match", query="Summerfield", fields=["city", "firstname"]) res = Search(using=es, index="test_index").query(q) print(res) for data in res: print(data.to_dict())查询对象可以通过逻辑运算符组合起来:
Q("match", title="python") | Q("match", title="django") # {"bool": {"should": [...]}} Q("match", title="python") & Q("match", title="django") # {"bool": {"must": [...]}} ~Q("match", title="python") # {"bool": {"must_not": [...]}}示例代码2:
from elasticsearch_dsl import connections, Search, Q # 方式一:连接es es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20) # print(es) # q = Q("multi_match", query="123.244.101.255", fields=["clientip", "timestamp"]) q = Q('match', name='张') | Q("match", name="北") res = Search(using=es, index="test_index").query(q) # print(res) for data in res: print(data.to_dict(), data.name) print("*" * 100) q = Q('match', name='张') & Q("match", name="北") res = Search(using=es, index="test_index").query(q) # print(res) for data in res: print(data.to_dict(), data.name) print("*" * 100) q = ~Q('match', name='张') res = Search(using=es, index="test_index").query(q) # print(res) for data in res: print(data.to_dict(), data.name)运行结果:
提示:
本文由神整理自网络,如有侵权请联系本站删除!
本站声明:
1、本站所有资源均来源于互联网,不保证100%完整、不提供任何技术支持;
2、本站所发布的文章以及附件仅限用于学习和研究目的;不得将用于商业或者非法用途;否则由此产生的法律后果,本站概不负责!
相关内容
- python中validators库的使用方法详解_python_
- 基于Python制作一个汇率换算程序_python_
- python3中requests库重定向获取URL_python_
- python中内置类型添加属性问题详解_python_
- python项目报错:bs4.FeatureNotFound: Couldn‘t find a tree builder with the features you requests_python_
- PyTorch中torch.utils.data.DataLoader实例详解_python_
- Python重试库 Tenacity详解(推荐)_python_
- Python制作数据分析透视表的方法详解_python_
- Python+fuzzywuzzy计算两个字符串之间的相似度_python_
- Python利用redis-py实现哈希数据类型的常用指令操作_python_
