0

I would like to update an elasticsearch query with python.

From a first query to a Db I get this example of query as a string which I transform in a json file through:

query = json.loads(string_query)

example of the query:

{'bool': 
    {'must': [ 
            {'range': {'field': {'gte': lowerbound, 'lt': upperbound}}}
              ],
  'must_not': [{'match_phrase': {'field': {'query': 'word'}}}]}}

What I would like to do is to add fields to the query as:

{'bool': 
    {'must': [ 
            {'range': {'field': {'gte': lowerbound, 'lt': upperbound}}},
            {'range': {'field-2': {'gte': lowerbound, 'lt': upperbound}}}
            {'bool':  {'should': {'bool': {'must': 
                           [ 
                               {'range': {'field': {'gte': lowerbound, 'lt': upperbound}}},
                               {'range': {'field-2': {'gte': lowerbound, 'lt': upperbound}}}
                           ]}}}},
              ],
  'must_not': [{'match_phrase': {'field': {'query': 'word'}}}]}}

is it possible to exploit some libraries or it should be done by 'hand' by building a new json from the first one?

1 Answer 1

1

This is actually a python question btw. Output of json.loads is a dictionary . You can modify the way you work with a python dictionary.

For example

query["bool"]["must"] += [ < your query> ] 

You can pass this query object to requests.post() or library of your choice to submit request to ES . BTW - you may want to look at elastic client for python elasticsearch-py

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

2 Comments

thanks for the answer, I am already using the elastic client and I was wandering if it was possible to build queries also through some libraries as in java with org.elasticsearch.index.query.QueryBuilders
its slightly different model -> elasticsearch-dsl.readthedocs.io/en/latest/…

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.