DEV Community

Committed Software
Committed Software

Posted on • Originally published at committed.software on

2 1

Debugging Elasticsearch Queries in Java: how to convert queries to JSON

Code Data Programming Code Computer Programming cc zero

Elasticsearch has excellent documentation with detailed descriptions and plenty of examples. The Elasticsearch Java API has handy builder classes accessible from org.elasticsearch.index.query.QueryBuilders to craft queries and aggregations in an intuitive and fluent way that is idiomatic to Java.

Unintuitively, however, QueryBuilder and AggregationBuilder subclasses do not implement toString, making it harder to debug and analyze these Java calls than you would expect, as it is not always clear how they compare to the Elasticsearch-native JSON format used extensively in Elasticsearch’s documentation. This issue is compounded further when the construction of queries is out of your control!

One way we could view our queries in JSON format is to enable elasticseach’s Slow Log, but there is a simpler way, using the Java Elasticsearch API to achieve what we want:

import java.io.IOException;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;

...

public static void toJSON(ToXContent queryBuilder) {
  try {
    XContentBuilder builder =
        queryBuilder.toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS);
    log.info(builder.string());
  } catch (IOException e) {
    log.error("Failed to log elasticsearch query", e);
  }
}

...

This can be used for classes implementing either QueryBuilder or AggregationBuilder, as both of these extend the ToXContent interface.

We can use the same approach when using Spring Data Elasticsearch by calling NativeSearchQueryBuilder.getQuery(), NativeSearchQueryBuilder.getFilter() or NativeSearchQueryBuilder.getAggregations() and passing the resulting objects using the same approach.

Now we can see the exact query that will be sent to Elasticsearch:

{ "term" : { "user" : "Kimchy" } }

If you are using Kibana to view your indices, try copying & pasting your query across on the Discover page for further analysis.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay