【问题标题】:How i build Dynamicx queries elasticsearch_dsl python我如何构建动态查询 elasticsearch dsl python
【发布时间】:2016-01-11 00:06:53
【问题描述】:

我对 elasticsearch_dsl python 库有以下问题。

我正在开发一个具有搜索功能的网络应用程序(DJANGO 框架)。我想建立一个动态查询,必须模式。

所以这里是下面的代码

i = 0
must = []
while (i < len(results['words'])):
    must.append(Q('match', tags=results['words'][i]))
    i += 1

print must
client = Elasticsearch()
es = Search(using=client, index="_______")
es.query(must)
response = es.execute()
for hit in response:
    print hit
return response.hits.total

Python 返回异常。TypeError

我已经有了 elasticsearch_dsl 的文档,但我没有找到类似我的问题的东西。你知道我如何解决这个问题吗?

【问题讨论】:

  • 解决方案有效吗?
  • 是的,很抱歉延迟回复。你的回答很中肯

标签: python django elasticsearch elasticsearch-dsl


【解决方案1】:

你有点亲近。您需要指定bool,然后像这样查询Search 对象

i = 0
must = []
while (i < len(results['words'])):
    must.append(Q('match', tags=results['words'][i]))
    i += 1

print must
client = Elasticsearch()
q = Q('bool', must=must)   <--- This is important
es = Search(using=client, index="_______").query(q)
response = es.execute()
for hit in response:
    print hit
return response.hits.total

你也可以用es.to_dict()查看实际查询,这可以帮助你理解

【讨论】:

  • 您的即时回答很有帮助。如果我有正确的声誉,我会给你投票。
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 2012-12-31
  • 2016-07-21
  • 2018-10-17
  • 1970-01-01
相关资源
最近更新 更多