Given an instance of BoolQuery, how can I find out the JSON string that will be sent to the ES server?
Note that I am using ES7.
I tried toString(), but it returns something like "co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery@3cec79d3".
I see that there is a serialize() method, but for the life of me, I can't figure out how to construct the two arguments (JsonGenerator and JsonpMapper).
I tried Amit's suggestion of getting the json from the SearchRequest object's source, but it does not seem to work. For example in the sample code below, the last line raises a NullPointerException because sReq.source() returns null.
SearchRequest sReq = SearchRequest.of(s -> s
.index("products")
.query(q -> q
.match(t -> t
.field("name")
.query("hello")
)
)
);
String sReqJson = sReq.source().toString();
CodePudding user response:
You must be having the search request object if you are using Elasticsearch 7 client, you can use searchRequest.source().toString()
to get the search JSON on it.
CodePudding user response:
I figured it out:
StringWriter writer = new StringWriter();
JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer);
boolQuery.serialize(generator, new JacksonJsonpMapper());
generator.flush();
String json = writer.toString();