gpt4 book ai didi

python - 列表中的Python Elasticsearch DSL查询值

转载 作者:行者123 更新时间:2023-12-02 22:24:06 27 4
gpt4 key购买 nike

我正在使用Python的Elasticsearch DSL软件包:
http://elasticsearch-dsl.readthedocs.org/en/latest/search_dsl.html

我的样本elasticsearch条目如下所示:

{
"_index" : "users",
"_type" : "player",
"_id" : "1",
"_version" : 5,
"found" : true,
"_source":{"doc": {"weight": 92, "height": 184, "gender": "male", "firstname": "John", "lastname": "Smith", "age": 31, "country": "France"}}

我正在尝试获取 firstname = "John*"列表中 [France, Australia]国家的所有条目。这是我的代码:
firstname = 'John'
s = Search(index='users').using(elasticsearch)
must = [
{'match': {'doc.firstname': {'query': firstname + '*', 'boost': 2}}},
{'terms': {'doc.country': [u'France', u'Australia']}}]

s = s.query(Q('bool', must=must))
response = s.execute()

它返回空结果,我在做什么错?我正在使用来自这里的建议:
Elasticsearch match list against field

我是Elasticsearch的新手,还是对我们在哪里使用查询vs过滤器感到困惑。

最佳答案

我为此使用了应参数:

firstname = 'John'
s = Search(index='users').using(elasticsearch)
must = [
{'match': {'doc.firstname': {'query': firstname + '*', 'boost': 2}}}]

should = []
for country in countries:
should.append({'match': {'doc.country': country}})

s = s.query(Q('bool', must=must, should=should, minimum_should_match=1))
response = s.execute()

关于python - 列表中的Python Elasticsearch DSL查询值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368157/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com