你如何在Python DSL中调用带状疱疹?
这是一个简单的示例,它在“name”字段中搜索一个短语,在“surname”字段中搜索另一个短语。
import json
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
def make_dsl_query(fields):
"""
Construct a query
"""
es_client = Elasticsearch()
my_query = Search(using=es_client, index="my_index", doc_type="my_type")
if fields['name'] and fields['surname']:
my_query = my_query.query(Q('bool', should=
[Q("match", name=fields['name']),
Q("match", surname=fields['surname'])]))
return my_query
if __name__ == '__main__':
my_query = make_dsl_query(fields={"name": "Ivan The Terrible", "surname": "Conqueror of the World"})
response = my_query.execute()
# print response
for hit in response:
print(hit.meta.score, hit.name, hit.surname)
1) 可以使用带状疱疹吗?如何?我已经尝试了很多东西,但在它的文档中找不到任何东西。
这将适用于普通的 Elasticsearch 查询,但显然在 Python DSL 中以不同的方式调用......
my_query = my_query.query(Q('bool', should=
[Q("match", name.shingles=fields['name']),
Q("match", surname.shingles=fields['surname'])]))
2)我如何将模糊参数传递给我的比赛?似乎也找不到任何东西。理想情况下,我可以做这样的事情:
my_query = my_query.query(Q('bool', should=
[Q("match", name=fields['name'], fuzziness="AUTO", max_expansions=10),
Q("match", surname=fields['surname'])]))