Search code examples
javaelasticsearchelasticsearch-7elasticsearch-java-api

How to get the json representation of the ElasticSearch query/request that was built with Java API Client (7.16)?


A new Java API Client was released in the 7.16 version of ES and the Java Rest Client was deprecated. There was the ability to convert a query to JSON in the deprecated client. It was convenient for debugging/tuning/profiling of the query in Kibana.

Unfortunately, I don't see any mention of this in the new client documentation. So my question is:

Is it possible to get a JSON representation of the query that was constructed via the Java API Client? It can be either some utility class or log config of the client that prints outgoing requests.


Solution

  • It looks like this feature will be directly supported in an upcoming version: https://discuss.elastic.co/t/elaticsearch-java-client-output-dsl/300952

    In the meantime, there is a workaround given in the above discussion, but it does not quite work. Here is a snippet that works for me:

    StringWriter writer = new StringWriter();
    JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer);
    request.serialize(generator, new JacksonJsonpMapper());
    generator.flush();
    return writer.toString();