欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解

 更新時(shí)間:2023年07月25日 09:50:39   作者:IT之一小佬  
這篇文章主要介紹了python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解,ElasticSearch在實(shí)際生產(chǎn)里通常和LogStash,Kibana,F(xiàn)ileBeat一起構(gòu)成Elastic?Stack來使用,它是這些組件里面最核心的一個(gè),需要的朋友可以參考下

elasticsearch_dsl查詢語句轉(zhuǎn)換成es語句

使用代碼運(yùn)行效果來演示轉(zhuǎn)換結(jié)果。

示例代碼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()  # 當(dāng)調(diào)用.query()方法多次時(shí),內(nèi)部會(huì)使用&操作符
print(res.to_dict())

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

示例代碼5:  【分頁(yè)】

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())

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

示例代碼8:  【聚合內(nèi)置排序】

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())

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

示例代碼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條數(shù)據(jù)" % 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條數(shù)據(jù)" % res.count())

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

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

運(yùn)行結(jié)果:

示例代碼14:  【使用pycharm打斷點(diǎn)查看查詢語句】

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())
# 執(zhí)行并拿到返回值
response = s.execute()
# res是bucket指定的名字
for data in response.aggregations.res:
    print(data.to_dict())

運(yùn)行結(jié)果:

注意:即使數(shù)據(jù)庫(kù)中沒有數(shù)據(jù),也可以打印出查詢語句!

到此這篇關(guān)于python中的elasticsearch_dsl查詢語句轉(zhuǎn)換成es查詢語句詳解的文章就介紹到這了,更多相關(guān)python的elasticsearch_dsl轉(zhuǎn)換es內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Jupyter Notebook內(nèi)使用argparse報(bào)錯(cuò)的解決方案

    Jupyter Notebook內(nèi)使用argparse報(bào)錯(cuò)的解決方案

    這篇文章主要介紹了在Jupyter Notebook內(nèi)使用argparse報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 在pycharm上mongodb配置及可視化設(shè)置方法

    在pycharm上mongodb配置及可視化設(shè)置方法

    今天小編就為大家分享一篇在pycharm上mongodb配置及可視化設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • python密碼學(xué)Base64編碼和解碼教程

    python密碼學(xué)Base64編碼和解碼教程

    這篇文章主要為大家介紹了python密碼學(xué)Base64編碼和解碼教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 基于python調(diào)用psutil模塊過程解析

    基于python調(diào)用psutil模塊過程解析

    這篇文章主要介紹了基于python調(diào)用psutils模塊過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • PyTorch之怎樣選擇合適的優(yōu)化器和損失函數(shù)

    PyTorch之怎樣選擇合適的優(yōu)化器和損失函數(shù)

    這篇文章主要介紹了PyTorch怎樣選擇合適的優(yōu)化器和損失函數(shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python批量處理PDF圖片的操作指南(插入、壓縮、提取、替換、分頁(yè)、旋轉(zhuǎn)、刪除)

    Python批量處理PDF圖片的操作指南(插入、壓縮、提取、替換、分頁(yè)、旋轉(zhuǎn)、刪除)

    圖片是 PDF 文檔的核心元素之一,它們不僅能夠增強(qiáng)文檔的視覺吸引力,還能有效傳達(dá)信息,幫助讀者更好地理解內(nèi)容和主題,在實(shí)際操作中,我們常需要對(duì)PDF中的圖片進(jìn)行多種處理,這篇文章將詳細(xì)介紹如何使用Python在PDF中實(shí)現(xiàn)圖片插入、提取、替換、壓縮等操作
    2025-04-04
  • Flask中特殊裝飾器的使用

    Flask中特殊裝飾器的使用

    在Flask中,before_request和after_request是用作裝飾器的特殊函數(shù),本文主要介紹了Flask中特殊裝飾器的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • 基于Python輕松制作一個(gè)股票K線圖網(wǎng)站

    基于Python輕松制作一個(gè)股票K線圖網(wǎng)站

    在當(dāng)今這個(gè)人手一個(gè)?Web?服務(wù)的年代,GUI?程序還是沒有?Web?服務(wù)來的香啊。所以本文將用Python制作一個(gè)簡(jiǎn)單的股票K線圖網(wǎng)站,感興趣的可以了解一下
    2022-09-09
  • linux下python中文亂碼解決方案詳解

    linux下python中文亂碼解決方案詳解

    這篇文章主要介紹了linux下python中文亂碼解決方案詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • Python Flask基礎(chǔ)教程示例代碼

    Python Flask基礎(chǔ)教程示例代碼

    這篇文章主要介紹了Python Flask基礎(chǔ)教程示例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02

最新評(píng)論