How to show elasticsearch queries in spring boot application How to show elasticsearch queries in spring boot application elasticsearch elasticsearch

How to show elasticsearch queries in spring boot application


Using Springboot 2.2.6, and RestHighLevelClient, the following worked:

logging.level.org.springframework.data.elasticsearch.client.WIRE : trace

This is also documented in springboot-data-elasticsearch

However, you need to pay attention to initialize your RestHighLevelClient bean in the same way stated in the documentation. I.e, using the ClientConfiguration builder. At first I created the bean as new RestHighLevelClient() and it didn't work.Example:

    @Bean(destroyMethod = "close")    public RestHighLevelClient restClient() {        ClientConfiguration clientConfiguration = ClientConfiguration.builder()                .connectedTo(esHost +":" + esPort).usingSsl()                .build();        RestHighLevelClient client = RestClients.create(clientConfiguration).rest();        return client;    }


I tried other answer and it didn't worked in spring boot 2.2.2 and elasticsearch 6.7.2, the below worked for me probably because I use the RestHighLevelClient, to get the request and response body I had to enable the apache http logging in the application.properties files like below

logging.level.org.elasticsearch.client=TRACElogging.level.org.apache.http=TRACE


In Spring Boot 2 you can enable query debug with the following:

logging.level.org.springframework.data.elasticsearch.core=DEBUG

And the generated log will be something like:

{  "from" : 0,  "size" : 10,  "query" : {    "bool" : {      "must" : [        {          "query_string" : {            "query" : "some string",            "fields" : [              "nome^1.0"            ],            "use_dis_max" : true,            "tie_breaker" : 0.0,            "default_operator" : "and",            "auto_generate_phrase_queries" : false,            "max_determinized_states" : 10000,            "enable_position_increments" : true,            "fuzziness" : "AUTO",            "fuzzy_prefix_length" : 0,            "fuzzy_max_expansions" : 50,            "phrase_slop" : 0,            "escape" : false,            "split_on_whitespace" : true,            "boost" : 1.0          }        }      ],      "disable_coord" : false,      "adjust_pure_negative" : true,      "boost" : 1.0    }  },  "version" : true}

Best regards.