Python Elasticsearch DSL 查询、过滤、聚合操作实例

Python Elasticsearch DSL 使用简介


连接 Es:

import elasticsearch

es = elasticsearch.Elasticsearch([{‘host’: ‘127.0.0.1’, ‘port’: 9200}])

先看一下搜索,q 是指搜索内容,空格对 q 查询结果没有影响,size 指定个数,from_ 指定起始位置,filter_path 可以指定需要显示的数据,如本例中显示在最后的结果中的只有 _id_type

res_3 = es.search(index=“bank”, q=“Holmes”, size=1, from_=1)

res_4 = es.search(index=“bank”, q=" 39225 5686 ", size=1000, filter_path=[‘hits.hits._id’, ‘hits.hits._type’])

查询指定索引的所有数据:

其中,index 指定索引,字符串表示一个索引;列表表示多个索引,如 index=["bank", "banner", "country"];正则形式表示符合条件的多个索引,如 index=["apple*"],表示以 apple 开头的全部索引。

search 中同样可以指定具体 doc-type

from elasticsearch_dsl import Search

s = Search(using=es, index=“index-test”).execute()

print s.to_dict()

根据某个字段查询,可以多个查询条件叠加:

s = Search(using=es, index=“index-test”).query(“match”, sip=“192.168.1.1”)

s = s.query(“match”, dip=“192.168.1.2”)

s = s.excute()

多字段查询:

from elasticsearch_dsl.query import MultiMatch, Match

multi_match = MultiMatch(query=‘hello’, fields=[‘title’, ‘content’])

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值