Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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")
            .from(String.valueOf(lookupDto.getPixel_x_min())).to(String.valueOf(lookupDto.getPixel_x_max())))
            .build();

    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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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());
generator.flush();
System.out.println(writer.toString());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading