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

守給你的承諾、 提交于 2021-01-21 09:12:40

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/60849887/log-json-queries-built-through-elasticsearch-high-level-java-client-for-debuggin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!