Hi, there.
I am currently using 8.14.1 rest client for java, by now, I want to use bucketSelector to filter the bucket. However, I can't find any example showing how to use that api and I tried a bit but still not able to get it right , could we get more explanation on that api?co.elastic.clients.elasticsearch._types.aggregations.Aggregation.Builder#bucketSelector(co.elastic.clients.elasticsearch._types.aggregations.BucketSelectorAggregation)
And my code is like:
Aggregation.Builder userId = new Aggregation.Builder();
userId.terms(map -> map.field("userId").missing(0));
Aggregation.Builder sum = new Aggregation.Builder();
Aggregation.Builder selec = new Aggregation.Builder();
BucketSelectorAggregation.Builder selector = new BucketSelectorAggregation.Builder();
selector
.bucketsPath(bp -> bp.single("totalSpend"))
.script(
s -> s.inline(in -> in.source("params.totalSpend >= 10 && params.totalSpend < 100")));
sum.aggregations("filter", selec.build());
userId.aggregations("totalSpend", sum.build());
mapIdTermBuilder.aggregations("userId", userId.build());
search.aggregations("mapId", mapIdTermBuilder.build());
SearchRequest bucketSelectorExample = SearchRequest.of(s -> s
.index("sales")
.size(0)
.aggregations("sales_per_month", a -> a
.dateHistogram(d -> d
.field("date")
.calendarInterval(CalendarInterval.Month)
)
.aggregations(Map.of(
"total_sales",
Aggregation.of(aa -> aa.sum(su -> su.field("price"))),
"sales_bucket_filter",
Aggregation.of(aaa -> aaa
.bucketSelector(b -> b
.bucketsPath(bp -> bp
.dict(Map.of("totalSales", "total_sales"))
)
.script(Script.of(sc -> sc.inline(i -> i
.source("params.totalSales > 200"))))
)
)
)
)
)
);
If you'd like to upgrade to a more recent version of the client, from 8.15 we've simplified the syntax for script, which now is a bit more concise so it would look like this:
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.