Is there any example of using bucketSelector in java client 8.14.1?

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());

Thx for any relpy!

Hello and welcome!

This an example from the bucket selector documentation page translated in the java dsl, so from the json:

POST /sales/_search
{
  "size": 0,
  "aggs": {
    "sales_per_month": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "month"
      },
      "aggs": {
        "total_sales": {
          "sum": {
            "field": "price"
          }
        },
        "sales_bucket_filter": {
          "bucket_selector": {
            "buckets_path": {
              "totalSales": "total_sales"
            },
            "script": "params.totalSales > 200"
          }
        }
      }
    }
  }
}

to the java client:

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:

...
.script(sc -> sc.source("params.totalSales > 200")
...
1 Like

Thank you so much! Itrotta ! MANY thanks for that :blush:

1 Like