我在使用 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/