Home > Software engineering >  ElasticSearch new Java API print created quesy
ElasticSearch new Java API print created quesy

Time:01-24

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.

CodePudding user response:

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());
  • Related