ElasticSearch new Java API print created quesy


I trying out the new Java Client for Elastic 8.1.1.
In older versions i was able to print out the generated json query by using searchRequest.source().

I cannot find out actuallay what methode/service i can use do to so with the new client.
My code looks:

    final Query range_query = new Query.Builder().range(r -> r.field("pixel_x")

    final Query bool_query = new Query.Builder().bool(t -> t.must(range_query)).build();

    SearchRequest sc = SearchRequest.of(s -> s.query(bool_query).index(INDEX).size(100));

The SearchRequest object offers a source() method but ist value is null.

>Solution :

You can use below code for printing query with new Elastic Java Client:

Query termQuery = TermQuery.of(t -> t.field("field_name").value("search_value"))._toQuery();

StringWriter writer = new StringWriter();
JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer);
termQuery.serialize(generator, new JacksonJsonpMapper());

Leave a Reply Cancel reply