'Elasticsearch query serialization returns empty string
I want to extract the JSON representation of an Elasticsearch Query, which was built by the Java client API (Java API Client 8.0.0). The following code shows my try:
Query x1 = new Query(new TermQuery.Builder().field("f1").value(v -> v.stringValue("val1")).build());
Query x2 = new Query(new TermQuery.Builder().field("f2").value(v -> v.stringValue("val2")).build());
Query query = new Query(new BoolQuery.Builder().must(List.of(x1, x2)).build());
try (final var out = new ByteArrayOutputStream()) {
final JsonGenerator generator = Json.createGenerator(out);
query.serialize(generator, new JacksonJsonpMapper());
return out.toString(StandardCharsets.UTF_8);
}
I'm using the org.glassfish.jakarta-json:2.0.1
package to provide the JsonGenerator
implementation.
The returned string is empty. What am I doing wrong here?
Solution 1:[1]
It's a little late but if anyone is still looking, you need to close the JsonGenerator
and then you will have the JSON output.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jsaulou |