使用代碼運作效果來示範轉換結果。
示例代碼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())
運作結果:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIyZuBnLhVTNwgDMwczNlJjMmdjZ5UjMmRjNkJjZkFTYlRDNkFzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
示例代碼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())
運作結果:
注意:即使資料庫中沒有資料,也可以列印出查詢語句!