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

**URL:** <https://discuss.elastic.co/t/is-there-any-example-of-using-bucketselector-in-java-client-8-14-1/375924>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [March 14, 2025, 1:34pm UTC](https://discuss.elastic.co/t/is-there-any-example-of-using-bucketselector-in-java-client-8-14-1/375924 "2025-03-14T13:34:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheXs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thexs/32/142053_2.png) [@TheXs](https://discuss.elastic.co/u/TheXs)\
**Post date:** [March 14, 2025, 1:34pm UTC](https://discuss.elastic.co/t/is-there-any-example-of-using-bucketselector-in-java-client-8-14-1/375924/1 "2025-03-14T13:34:14Z")

</div>

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:

```auto
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!

---

<div class="post-metadata">

**Author:** ![ltrotta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ltrotta/32/124003_2.png) [@ltrotta](https://discuss.elastic.co/u/ltrotta)\
**Post date:** [March 14, 2025, 1:59pm UTC](https://discuss.elastic.co/t/is-there-any-example-of-using-bucketselector-in-java-client-8-14-1/375924/2 "2025-03-14T13:59:55Z")

</div>

Hello and welcome!

This an example from the [bucket selector documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html) page translated in the java dsl, so from the json:

```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:

```java
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:

```java
...
.script(sc -> sc.source("params.totalSales > 200")
...

```

---

<div class="post-metadata">

**Author:** ![TheXs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thexs/32/142053_2.png) [@TheXs](https://discuss.elastic.co/u/TheXs)\
**Post date:** [March 14, 2025, 3:33pm UTC](https://discuss.elastic.co/t/is-there-any-example-of-using-bucketselector-in-java-client-8-14-1/375924/4 "2025-03-14T15:33:05Z")

</div>

Thank you so much! Itrotta ! MANY thanks for that 😊
