I use Elasticsearch 8.12 using the JAVA API. I use the new JAVA client "co.elastic.clients.elasticsearch.ElasticsearchClient"
My application builds the query in JSON format, which looks something like :
{
"query": {
"bool": {
"must": [
{
"term": {
"officeCode.keyword": {
"value": "410"
}
}
},
{
"terms": {
"status.keyword": [
"CLOSED",
"CREDIT"
]
}
}
]
}
}
}
Then, I pass the above query as shown below:
String encodedJson = Base64.getEncoder().encodeToString(jsonQuery.getBytes());
SearchResponse<Map> searchResponse = elasticsearchClient.search(s -> s
.index(indexName)
.query(b -> b.queryString(b1 -> b1.query(encodedJson ))), Map.class);
It returns 10 results instead of 0 since there is no document which meets the criteria. I fail to understand the issue here. It looks like it completely ignores the query.
You have not created query in correct way. You are using b -> b.queryString
means it will use query string type of query which will search your entire json as query and due to that it is returning response.
You need to create java code like below if you want to pass JSON query into request:
SearchRequest searchRequest = SearchRequest.of(b -> b
.withJson(queryJson)
);
SearchResponse<Map> searchResponse = elasticsearchClient.search(searchRequest, Map.class);
You can check this documentation for more details.