Elasticsearch-dsl-py: Print final Query sent to Elasticsearch

Created on 17 Aug 2016  路  4Comments  路  Source: elastic/elasticsearch-dsl-py

Is there anyway to print the final Query sent to Elasticsearch?

Thank you.

Most helpful comment

Thank you, that's very helpful.

While waiting for your answer, I hacked the following which helped to understand how the Facetted DSL query is formed:

pp = pprint.PrettyPrinter(indent=4)
logger.debug(pp.pformat(s.build_search().to_dict()))

All 4 comments

You can always ask the Search object for it's raw representation by calling the .to_dict() method. If you want to log what's going on I'd recommend that you enable logging for the elasticsearch-py library (0) by using the standard logging module. You can either choose the elasticsearch logger which logs exactly what's going on (set the log level to INFO to see all requests) or elasticsearch.trace for a more friendly representation where all calls to elasticsearch will be logged as curl commands against localhost:9200 with pretty printed json.

0 - http://elasticsearch-py.readthedocs.io/en/master/#logging

Thank you, that's very helpful.

While waiting for your answer, I hacked the following which helped to understand how the Facetted DSL query is formed:

pp = pprint.PrettyPrinter(indent=4)
logger.debug(pp.pformat(s.build_search().to_dict()))

For reference, this StackOverflow post shows how you can turn on logs dynamically.

PUT /_settings
{
  "index": {
    "search.slowlog.level": "trace",
    "search.slowlog.threshold.query.trace": "1ms"
  }
}


TL;DR you just have to set up Python logging before running the query.
Then you will find the full query in JSON in your log file.

import logging
logging.basicConfig(filename='myfile.log', level=logging.DEBUG)
# ...
res = es.search(index="my_index", body={ #...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

berinhard picture berinhard  路  3Comments

ypkkhatri picture ypkkhatri  路  4Comments

primoz-k picture primoz-k  路  4Comments

njoannin picture njoannin  路  3Comments

gabrielpjordao picture gabrielpjordao  路  3Comments