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’])

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

s =

你可能感兴趣的:(Python Elasticsearch DSL 查询、过滤、聚合操作实例)