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

文章展示了如何使用Elasticsearch的DSL进行各种查询和聚合操作,包括基本查询、布尔查询、分页、聚合统计以及嵌套聚合。示例涵盖了匹配查询、范围查询、条件组合、聚合桶及指标计算等,强调了在Python环境中对Elasticsearch数据的高效处理。
摘要由CSDN通过智能技术生成

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

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

示例代码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())

运行结果:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值