Hello,
I’m currently migrating from Elasticsearch 7 to 8 in a Java application, and I’m running into issues when trying to perform searches using raw JSON queries.
In Elasticsearch 7, I used the Low Level REST Client like this:
final String endpoint = String.format("/%s/%s", this.alias, "_search");
final Map<String, String> params = new HashMap<>();
// add params if needed
final Request request = new Request(HttpMethod.POST, endpoint);
request.addParameters(params);
request.setJsonEntity(searchJson); // searchJson is a String
final Response response = this.clientSupplier.get().getLowLevelClient().performRequest(request);
Now with the Java client for Elasticsearch 8, I can't seem to do the same. The JSON I'm using includes features like script
inside aggregations, which can't be mapped directly to the new SearchRequest
class, but the same JSON works when sent through Postman. Here's a simplified example of the kind of query I'm trying to send:
{
"size": 0,
"query": {
"bool": {
"must": [
{ "bool": { "must": [] } },
{
"terms": {
"_acl": ["any", "000000000000000000000009", "000000000000000000000039", "eba000000005000000000003", "eba000000000000000000200", "eba000000000000000000205"],
"boost": 1.0
}
}
]
}
},
"aggs": {
"rootGroup": {
"date_histogram": {
"format": "dd/MM/yyyy",
"time_zone": "America/Sao_Paulo",
"min_doc_count": 1,
"script": "Object calculate(def doc) { ... }",
"calendar_interval": "day"
},
"aggs": {}
},
"nulo": {
"missing": {
"script": "Object calculate(def doc) { ... }"
}
}
}
}
I tried using client.search(b -> b.index(index).withJson(new StringReader(searchJson)), Map.class);
, but it fails with JsonpMappingException
because the structure doesn't map cleanly to the Java classes.
Is there a recommended way in Elasticsearch 8 Java client to send a raw JSON query string, similar to how it was done with the Low Level REST Client in version 7?
Thanks in advance!