简体   繁体   English

如何在 Spring-data-elasticsearch 中启用查询日志记录

[英]How to enable query logging in Spring-data-elasticsearch

I use spring-data-elasticsearch framework to get query result from elasticsearch server, the java code like this:我使用 spring-data-elasticsearch 框架从 elasticsearch 服务器获取查询结果,java 代码如下:

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?虽然我怎么知道发送到 elasticssearch 服务器的原始 http 查询? How can I enable the logging, I tried add log4j, but it seems the spring-data-elasticsearch doesn't log the query.如何启用日志记录,我尝试添加 log4j,但似乎 spring-data-elasticsearch 没有记录查询。

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.* , eg as follows:要记录通过存储库执行的 Spring Data Elasticsearch 查询,您需要为包org.springframework.data.elasticsearch.core.*启用DEBUG日志记录,例如如下:

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.人们会期待一种类似于 JPA 的优雅解决方案,但它似乎并不简单存在。

Tested with Spring Boot 1.4.0 and Spring Data Elasticsearch 1.7.3.使用 Spring Boot 1.4.0 和 Spring Data Elasticsearch 1.7.3 进行测试。

If you are using spring boot you can set the following in your application.properties:如果您使用的是 spring boot,您可以在 application.properties 中设置以下内容:

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

After digging through the spring data code i found this helpful little logger called "tracer" (name not very unique)在挖掘了 spring 数据代码后,我发现了这个有用的小记录器,称为“tracer”(名称不是很独特)

By setting the following in application.properties通过在 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.它将打印出请求的完整 curl 语句以及来自 Elasticsearch 的完整 JSON 响应。

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.我没有 Spring Data Elasticsearch 的答案,但是在 ES 本身中,您可以提高慢查询日志记录的默认设置并查看慢日志中的所有查询。 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. 1ms是您可以设置的最小值。

I encountered the same problem, In the ElasticsearchTemplate only a few method have log debug level, Eg:我遇到了同样的问题,在 ElasticsearchTemplate 中只有少数方法具有日志调试级别,例如:

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());
}

这适用于 Spring Boot 2.3.3.RELEASE

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

Just to add my two cents to @AndreiStefan: Now you can set 0ms instead of 1ms .只是为了向@AndreiStefan 添加我的两分钱:现在您可以设置0ms而不是1ms It seems that some very fast queries can be captured using this method.使用这种方法似乎可以捕获一些非常快速的查询。

Simply do:简单地做:

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

@2280258 is correct, and here comes the official doc: @2280258 是正确的,官方文档来了:

<logger name="org.springframework.data.elasticsearch.client.WIRE" level="trace"/>

Here is the reason: in org.springframework.data.elasticsearch.client.ClientLogger , spring data elasticsearch creates a logger named "org.springframework.data.elasticsearch.client.WIRE": Here is the reason: in org.springframework.data.elasticsearch.client.ClientLogger , spring data elasticsearch creates a logger named "org.springframework.data.elasticsearch.client.WIRE":

    private static final Logger WIRE_LOGGER = LoggerFactory
            .getLogger("org.springframework.data.elasticsearch.client.WIRE");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM