I have an issue where a nested query I am using does not filter correctly when using open search.
I am using this combination of libraries
api group: ‘org.opensearch.client’, name: ‘opensearch-java’, version: “2.11.0”
api group: ‘org.opensearch.client’, name: ‘opensearch-rest-client’, version: “2.14.0”
Running against an open search cluster on aws.
"aggs": {
"events.2024-09-25": {
"nested": {
"path": "accommodationOnlyPrices"
},
"aggs": {
"inner": {
"filter": {
"term": {
"accommodationOnlyPrices.targetDate": "2024-09-25"
}
},
"aggs": {
"minPrice": {
"min": {
"field": "accommodationOnlyPrices.price"
}
}
}
}
}
}
},
The above query correctly returns aggregations for my nested data;
My Java equivalent code is as follows:
private static void generateCarouselAggregation(Map<String, Aggregation> aggregationMap, AggregatedRequest aggregatedRequest, String path) {
for (int x=-3; x<3; x++) {
var targetDate = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDate.now().plusDays(x));
var filteredAggregation = new Aggregation.Builder().nested(n -> n.path(path))
.aggregations("inner", new Aggregation.Builder()
.filter(f -> f.term(t -> t.field(path + ".targetDate").value(FieldValue.of(targetDate))))
.aggregations("minPrice", Aggregation.of(a -> a.min(xz -> xz.field(path + ".price"))))
.build()
).build();
aggregationMap.put("carousel-" + targetDate, filteredAggregation);
}
}
To me they appear to be the same, but the Jave version does not work, yet the json version does.
The mapping looks like this in case it helps:
"mappings": {
"date_detection": true,
"properties": {
"accommodationFeatures": {
"type": "text"
},
"accommodationOnlyPrices": {
"type": "nested",
"properties": {
"price": {
"type": "integer"
},
"targetDate": {
"type": "date"
}
}
},
...