2024年最新Python Elasticsearch DSL 查询、过滤、聚合操作实例

q = Q(“multi_match”, query=“hello”, fields=[‘title’, ‘content’])

s = s.query(q).execute()

print s.to_dict()

Q() 第一个参数是查询方法,还可以是 bool

q = Q(‘bool’, must=[Q(‘match’, title=‘hello’), Q(‘match’, content=‘world’)])

s = s.query(q).execute()

print s.to_dict()

通过 Q() 进行组合查询,相当于上面查询的另一种写法。

q = Q(“match”, title=‘python’) | Q(“match”, title=‘django’)

s = s.query(q).execute()

print(s.to_dict())

{“bool”: {“should”: […]}}

q = Q(“match”, title=‘python’) & Q(“match”, title=‘django’)

s = s.query(q).execute()

print(s.to_dict())

{“bool”: {“must”: […]}}

q = ~Q(“match”, title=“python”)

s = s.query(q).execute()

print(s.to_dict())

{“bool”: {“must_not”: […]}}

过滤,在此为范围过滤,range 是方法,timestamp 是所要查询的 field 名字,gte 为大于等于,lt 为小于,根据需要设定即可。

关于 termmatch 的区别,term 是精确匹配,match 会模糊化,会进行分词,返回匹配度分数,(term 如果查询小写字母的字符串,有大写会返回空即没有命中,match 则是不区分大小写都可以进行查询,返回结果也一样)

范围查询

s = s.filter(“range”, timestamp={“gte”: 0, “lt”: time.time()}).query(“match”, country=“in”)

普通过滤

res_3 = s.filter(“terms”, balance_num=[“39225”, “5686”]).execute()

其他写法:

s = Search()

s = s.filter(‘terms’, tags=[‘search’, ‘python’])

print(s.to_dict())

{‘query’: {‘bool’: {‘filter’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}}

s = s.query(‘bool’, filter=[Q(‘terms’, tags=[‘search’, ‘python’])])

print(s.to_dict())

{‘query’: {‘bool’: {‘filter’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}}

s = s.exclude(‘terms’, tags=[‘search’, ‘python’])

或者

s = s.query(‘bool’, filter=[~Q(‘terms’, tags=[‘search’, ‘python’])])

print(s.to_dict())

{‘query’: {‘bool’: {‘filter’: [{‘bool’: {‘must_not’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}]}}}

聚合可以放在查询,过滤等操作的后面叠加,需要加 aggs

bucket 即为分组,其中第一个参数是分组的名字,自己指定即可,第二个参数是方法,第三个是指定的 field

metric 也是同样,metric 的方法有 sumavgmaxmin 等,但是需要指出的是,有两个方法可以一次性返回这些值,statsextended_stats,后者还可以返回方差等值。

实例1

s.aggs.bucket(“per_country”, “terms”, field=“timestamp”).metric(“sum_click”, “stats”, field=“click”).metric(“sum_request”, “stats”, field=“request”)

实例2

s.aggs.bucket(“per_age”, “terms”, field=“click.keyword”).metric(“sum_click”, “stats”, field=“click”)

实例3

s.aggs.metric(“sum_age”, “extended_stats”, field=“impression”)

实例4

s.aggs.bucket(“per_age”, “terms”, field=“country.keyword”)

实例5,此聚合是根据区间进行聚合

a = A(“range”, field=“account_number”, ranges=[{“to”: 10}, {“from”: 11, “to”: 21}])

res = s.execute()

最后依然要执行 execute(),此处需要注意,s.aggs 操作不能用变量接收(如 res=s.aggs,这个操作是错误的),聚合的结果会保存到 res 中显示。

排序

s = Search().sort(

‘category’,

‘-title’,

{“lines” : {“order” : “asc”, “mode” : “avg”}}

)

分页

s = s[10:20]

{“from”: 10, “size”: 10}

一些扩展方法,感兴趣的同学可以看看:

s = Search()

设置扩展属性使用.extra()方法

s = s.extra(explain=True)

设置参数使用.params()

s = s.params(search_type=“count”)

如要要限制返回字段,可以使用source()方法

only return the selected fields

s = s.source([‘title’, ‘body’])

don’t return any fields, just the metadata

s = s.source(False)

explicitly include/exclude fields

s = s.source(include=[“title”], exclude=[“user.*”])

reset the field selection

s = s.source(None)

使用dict序列化一个查询

s = Search.from_dict({“query”: {“match”: {“title”: “python”}}})

修改已经存在的查询

s.update_from_dict({“query”: {“match”: {“title”: “python”}}, “size”: 42})

在这里插入图片描述

感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的:

① 2000多本Python电子书(主流和经典的书籍应该都有了)

② Python标准库资料(最全中文版)

③ 项目源码(四五十个有趣且经典的练手项目及源码)

④ Python基础入门、爬虫、web开发、大数据分析方面的视频(适合小白学习)

⑤ Python学习路线图(告别不入流的学习)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里无偿获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 21
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值