How to enable query logging in Spring-data-elasticsearch

耗尽温柔 提交于 2019-12-01 03:04:40

I don't have an answer for Spring Data Elasticsearch, but in ES itself you can bump up the default settings for slow query logging and see all the queries in the slow log. More details about slow log here.

As to how to change the thresholds, a command like this should be used:

PUT /_settings
{
  "index.search.slowlog.threshold.query.info": "1ms"
}

1ms is kindof the smallest value you can set.

If you are using spring boot you can set the following in your application.properties:

logging.level.org.elasticsearch.index.search.slowlog.query=INFO
spring.data.elasticsearch.properties.index.search.slowlog.threshold.query.info=1ms

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.

I encountered the same problem, In the ElasticsearchTemplate only a few method have log debug level, E.g:

public <T> Page<T> queryForPage(CriteriaQuery criteriaQuery, Class<T> clazz) {
    QueryBuilder elasticsearchQuery = new CriteriaQueryProcessor().createQueryFromCriteria(criteriaQuery.getCriteria());
    QueryBuilder elasticsearchFilter = new CriteriaFilterProcessor().createFilterFromCriteria(criteriaQuery.getCriteria());
    SearchRequestBuilder searchRequestBuilder = prepareSearch(criteriaQuery, clazz);

    if (elasticsearchQuery != null) {
        searchRequestBuilder.setQuery(elasticsearchQuery);
    } else {
        searchRequestBuilder.setQuery(QueryBuilders.matchAllQuery());
    }

    if (criteriaQuery.getMinScore() > 0) {
        searchRequestBuilder.setMinScore(criteriaQuery.getMinScore());
    }

    if (elasticsearchFilter != null)
        searchRequestBuilder.setPostFilter(elasticsearchFilter);
    if (logger.isDebugEnabled()) {
        logger.debug("doSearch query:\n" + searchRequestBuilder.toString());
    }

    SearchResponse response = getSearchResponse(searchRequestBuilder
            .execute());
    return resultsMapper.mapResults(response, clazz, criteriaQuery.getPageable());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!