0

I want to use the query_string (Lucene Syntax) search with Elasticsearch DSL for Python. Unfortunately the first line doesn't work as easy as the second one:

s = Search(using=client, index='abc').query("query_string", doctext=searchstr).highlight('doctext', fragment_size=200)
s = Search(using=client, index='abc').query("match", doctext=searchstr).highlight('doctext', fragment_size=200)

Which function of the Elasticsearch DSL library could I use for query_string?

1 Answer 1

1

You can use query_string but you need to use the proper parameters it defines in documentation:

s = Search(using=client, index='abc')
s = s.query("query_string", query=searchstr, default_field="doctext") # note the query here
s = s.highlight('doctext', fragment_size=200)

Also please note that using query_string for input provided by users can be dangerous as there is no way to restrict what users can do in the query and they can potentially query against any field or craft a super expensive query that will be costly to execute.

Hope this helps!

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

2 Comments

I solved it with: s = Search(using='default').from_dict({'query': {'query_string': {'default_field': 'doctext', "query": searchstr}}, 'highlight': {'fields': {'doctext': {'fragment_size': 200}}}}) But your answer is also working.
I am using response.hits.total.value for total hits. There is a way to get the hits inside a doc. Can you give me some hint how to use it? I tried but doesn't work. innerHits

Your Answer

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