python中elasticsearch_dsl查询语句转换成es查询语句

命运对每个人都是一样的,不一样的是各自的努力和付出不同,付出的越多,努力的越多,得到的回报也越多,在你累的时候请看一下身边比你成功却还比你更努力的人,这样,你就会更有动力。

导读:本篇文章讲解 python中elasticsearch_dsl查询语句转换成es查询语句,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

使用代码运行效果来演示转换结果。

示例代码1: 

from elasticsearch_dsl import connections, Search, Q

es = connections.create_connection(hosts=["192.168.104.49:9200"], timeout=20)
# print(es)

res = Search(using=es, index="test_index").query().query()  # 当调用.query()方法多次时,内部会使用&操作符
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码2:

from elasticsearch_dsl import connections, Search, Q

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = ~Q("match", title="python")
res = Search(using=es, index="test_index").query(q)
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码3:

from elasticsearch_dsl import connections, Search, Q

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q('match', name='张') & Q("match", name="北")
res = Search(using=es, index="test_index").query(q)
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码4:

from elasticsearch_dsl import connections, Search, Q

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("bool", must=[Q("match", address="山")], should=[Q("match", gender="男"), Q("match", emplyer="AAA")], minimum_should_match=1)
res = Search(using=es, index="test_index").query(q)
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码5:  【分页】

from elasticsearch_dsl import connections, Search, Q

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("bool", must=[Q("match", address="山")], should=[Q("match", gender="男"), Q("match", emplyer="AAA")], minimum_should_match=1)
res = Search(using=es, index="test_index").query(q)[2: 5]
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码6:   【聚合】

from elasticsearch_dsl import connections, Search, Q, A

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("match", sex='男')
a = A("terms", field="gender")
res = Search(using=es, index="test_index").query(q)
res.aggs.bucket("gender_terms", a)
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码7:  【聚合】

from elasticsearch_dsl import connections, Search, Q, A

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("match", sex='男')
res = Search(using=es, index="test_index").query(q)
res.aggs.bucket("per_gender", "terms", field="gender")
res.aggs["per_gender"].metric("sum_age", "sum", field="age")
res.aggs["per_gender"].bucket("terms_balance", "terms", field="balance")
res.aggs["per_gender"].bucket("terms_balance2", "terms", field="balance2")
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码8:  【聚合内置排序】

from elasticsearch_dsl import connections, Search, Q, A

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("match", sex='男')
res = Search(using=es, index="test_index").query(q)
res.aggs.bucket("agg_age", "terms", field="age", order={"_count": "desc"})
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码9:

from elasticsearch_dsl import connections, Search, Q, A

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("match", sex='男')
res = Search(using=es, index="test_index").query(q)
res.aggs.bucket("agg_age", "terms", field="age", order={"_count": "asc"}).metric("avg_age", "avg", field="age")
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码10:  【_source字段】

from elasticsearch_dsl import connections, Search, Q, A

es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

q = Q("match", sex='男')
res = Search(using=es, index="test_index").query(q).source(['account_number', 'address'])
print(res.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码11:

from elasticsearch_dsl import connections, Search, Q

# 连接es
es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

s = Search(using=es, index="account_info")

# 方式一:
# 省份为北京
q1 = Q("match", province="北京")
# 25或30岁的男性信息
q2 = Q("bool", must=[Q("terms", age=[25, 30]), Q("term", gender="男")])

# and
q = q1 & q2

res = s.query(q)
print(res.to_dict())
# for data in res:
#     print(data.to_dict())

print("共查到%d条数据" % res.count())

print("*" * 100)

# 方式二
# 省份为北京
q1 = Q("match", province="北京")
# 25或30岁的信息
# q2 = Q("bool", must=[Q("terms", age=[25, 30]), Q("term", gender="男")])
q2 = Q("term", age=25) | Q("term", age=30)
# 男性
q3 = Q("term", gender="男")

res = s.query(q1).query(q2).query(q3)  # 多次query就是& ==> and 操作
print(res.to_dict())
# for data in res:
#     print(data.to_dict())

print("共查到%d条数据" % res.count())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码12:

from elasticsearch_dsl import connections, Search, Q, A

# 连接es
es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

s = Search(using=es, index="account_info")

s.query()
q = A("terms", field="age", size=100).metric("age_per_balance", "avg", field="balance")

s.aggs.bucket("res", q)
print(s.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码13:  【多次嵌套聚合】

from elasticsearch_dsl import connections, Search, Q, A

# 连接es
es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

s = Search(using=es, index="account_info")

a1 = A("range", field="age", ranges={"from": 25, "to": 28})
a2 = A("terms", field="gender")
a3 = A("avg", field="balance")

s.aggs.bucket("res", a1).bucket("gender_group", a2).metric("balance_avg", a3)

print(s.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

示例代码14:  【使用pycharm打断点查看查询语句】

from elasticsearch_dsl import connections, Search, Q, A

# 连接es
es = connections.create_connection(hosts=["192.168.124.49:9200"], timeout=20)
# print(es)

s = Search(using=es, index="account_info")

a1 = A("range", field="age", ranges={"from": 25, "to": 28})
a2 = A("terms", field="gender")
a3 = A("avg", field="balance")

s.aggs.bucket("res", a1).bucket("gender_group", a2).metric("balance_avg", a3)

# print(s.to_dict())
# 执行并拿到返回值
response = s.execute()

# res是bucket指定的名字
for data in response.aggregations.res:
    print(data.to_dict())

运行结果:

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

python中elasticsearch_dsl查询语句转换成es查询语句

注意:即使数据库中没有数据,也可以打印出查询语句!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/142767.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!