2

I have the following problem with the elasticsearch_dsl python library.

I am developing a web application (DJANGO framework) with search functionality. I want to build a dynamic query, must mode.

So here it is the following code

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 returns exceptions.TypeError

I have already the documentation of elasticsearch_dsl but i didn't find something like my issue. Do you know the way how i fix this problem?

4

1 回答 1

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()可以帮助您理解的实际查询

于 2016-01-11T00:56:04.577 回答