Search code examples
elasticsearchloggingelasticsearch-queryresthighlevelclient

log JSON queries built through ElasticSearch High Level Java Client for debugging?


I use ElasticSearch High-Level Client Java API in my Spring Boot application. I want to log the queries built using High-Level client API for debugging purposes.

My question is what kind of settings required in my application.properties file to turn on JSON queries built from my application?

I tried out the following properties to the application.properties file. However, it does not print the JSON queries built using various query builders.

logging.level.org.elasticsearch.client=TRACE
logging.level.org.elasticsearch.client.sniffer=TRACE
logging.level.org.elasticsearch=TRACE

Solution

  • You can simply log the queries built using the rest-high level client, using the below code in your logger.

    You can also have control of which types of queries you want to log and what types of levels (TRACE, INFO, DEBUG) you want to set in specific cases.

    Code to get and log the search JSON

    SearchRequest searchRequest = new SearchRequest("employee").source(sourceBuilder);
    log.info("Search JSON query: {}", searchRequest.source().toString());
    

    The first line, is used to create a search request and second line is used to print the search JSON, Note searchRequest.source().toString()) which is used to get the search JSON string.

    Let me know if you face any issue, I do this all the time using the rest-high-level client.