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?

2
  • did the solution work? Commented Jan 12, 2016 at 1:20
  • Yeah, sorry for the delayed response. Your answer was to the point Commented Feb 4, 2016 at 2:19

1 Answer 1

2

You are kind of close. You need to specify bool and then query on the Search object like this

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

You can also see the actual query with es.to_dict() which can help you understand

Sign up to request clarification or add additional context in comments.

1 Comment

Your immediately answer was quite helpful. If i have the right reputation I'll give you a vote up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.