python - 我如何构建动态查询elasticsearch dsl python

标签 python django elasticsearch elasticsearch-dsl

我在使用 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的文档,但我没有找到类似我的问题的内容。你知道我如何解决这个问题吗?

最佳答案

你们的关系有点亲密。您需要指定 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()查看实际查询,这可以帮助您理解

关于python - 我如何构建动态查询elasticsearch dsl python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34712303/

相关文章:

python - Pandas:如何仅选择组内标准差较小的组?

python - 设置扩展表单的用户

python - Django模板问题

elasticsearch - 如何在Elasticsearch中过滤掉超过一击的东西?

python - 停止 Python 程序,直到用户执行操作

Python 解析 HTML 时的 Unicode 和 ASCII 问题

python - '==' 运算符在 Python 中无法正常工作

python - 自定义渲染器破坏站点

elasticsearch - JHipster elasticsearch-reindexer 生成失败

java - Spring数据Elasticsearch |通过存储库进行全文本搜索