Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable query logging in Spring-data-elasticsearch

Tags:

I use spring-data-elasticsearch framework to get query result from elasticsearch server, the java code like this:

SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery()).withSearchType(SearchType.COUNT)
.addAggregation(new MinBuilder("min_createDate").field("createDate"))
.build();

List<Entity> list = template.queryForList(searchQuery, Entity.class);

While how can I know the raw http query sent to elasticssearch server? How can I enable the logging, I tried add log4j, but it seems the spring-data-elasticsearch doesn't log the query.

like image 568
fudy Avatar asked Jun 07 '15 09:06

fudy


People also ask

Why ElasticsearchTemplate is deprecated?

The ElasticsearchTemplate class is deprecated as it uses the TransportClient to access Elasticsearch, which itself is deprecated since Elasticsearch version 7. + Users should switch to ElasticsearchRestTemplate or ReactiveElasticsearchTemplate .

How do I print an Elasticsearch query in Java?

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.

What is Spring data Elasticsearch?

The Spring Data Elasticsearch project provides integration with the Elasticsearch search engine. Key functional areas of Spring Data Elasticsearch are a POJO centric model for interacting with a Elastichsearch Documents and easily writing a Repository style data access layer.

Does Elasticsearch use spring?

We'll learn how to index, search, and query Elasticsearch in a Spring application using Spring Data Elasticsearch. Spring Data Elasticseach is a Spring module that implements Spring Data, thus offering a way to interact with the popular open-source, Lucene-based search engine.


2 Answers

After digging through the spring data code i found this helpful little logger called "tracer" (name not very unique)

By setting the following in application.properties

logging.level.tracer=TRACE

It will print out a full curl statement for the request along with full JSON the response from Elasticsearch.

like image 138
abenab Avatar answered Sep 19 '22 20:09

abenab


This one is quite old, but I'd still like to share the solution that worked for me. To log Spring Data Elasticsearch queries executed through the Repository, you need to enable DEBUG logging for the package org.springframework.data.elasticsearch.core.*, e.g. as follows:

logging:
  level:
    org:
      springframework:
        data:
          elasticsearch:
            core: DEBUG

After that, queries will appear in logs:

{
  "from" : 0,
  "size" : 1,
  "query" : {
    "bool" : {
      "should" : [ {
        "query_string" : {
          "query" : "John Doe",
          "fields" : [ "entityName" ],
          "default_operator" : "and"
        }
      }, {
        "query_string" : {
          "query" : "John Doe",
          "fields" : [ "alias" ],
          "default_operator" : "and"
        }
      } ]
    }
  },
  "post_filter" : {
    "bool" : { }
  }
}

One would expect an elegant solution similar to JPA, but it seems that it doesn't simply exist.

Tested with Spring Boot 1.4.0 and Spring Data Elasticsearch 1.7.3.

like image 36
Vladimir Salin Avatar answered Sep 18 '22 20:09

Vladimir Salin