Skip to content

Conversation

@sergio-bobillier
Copy link
Collaborator

Add support for the bucket_selector pipeline aggregation to the QueryBuilder DSL. This allows filtering buckets based on computed metrics.

Example use case: group test executions by test name, compute total duration per test, and keep only tests that exceed a minimum total duration.

DSL usage:

aggs = JayAPI::Elasticsearch::QueryBuilder::Aggregations.new

aggs.terms('by_test', field: 'id_long').tap do |t|
  t.sum('total_duration_ms', field: 'duration_ms')
  t.bucket_selector(
    'only_slow_tests',
    buckets_path: { total: 'total_duration_ms' },
    script: JayAPI::Elasticsearch::QueryBuilder::Script.new(
      source: 'params.total > params.min_duration',
      params: { min_duration: 60_000 }
    )
  )
end

query = aggs.to_h

Generated JSON:

{
  "aggs": {
    "by_test": {
      "terms": { "field": "id_long" },
      "aggs": {
        "total_duration_ms": { "sum": { "field": "duration_ms" } },
        "only_slow_tests": {
          "bucket_selector": {
            "buckets_path": { "total": "total_duration_ms" },
            "script": {
              "source": "params.total > params.min_duration",
              "lang": "painless",
              "params": { "min_duration": 60000 }
            }
          }
        }
      }
    }
  }
}

Motivation: Previously there was no way to filter aggregation buckets, forcing either client-side filtering or dropping to raw JSON. This feature enables pipeline filtering directly through the DSL.

Note: This Pull Request is a repost of #50 with some fixes to the code and added documentation.

@sergio-bobillier sergio-bobillier self-assigned this Jan 27, 2026
@sergio-bobillier sergio-bobillier marked this pull request as ready for review January 27, 2026 17:15
This change adds support for Elasticsearch's `bucket_selector` pipeline
aggregation to the QueryBuilder DSL. The new aggregation allows
filtering buckets based on computed metrics (e.g. retaining only those
buckets where a sum or average exceeds a threshold), a capability not
previously exposed through the DSL.
@sergio-bobillier sergio-bobillier changed the title [JAY-726] Add bucket_selector aggregation to QueryBuilder [JAY-732] Add bucket_selector aggregation to QueryBuilder Jan 27, 2026
Copy link
Collaborator

@sheputis sheputis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double-checked in my patchsets: can confirm that it works 👍

@sergio-bobillier sergio-bobillier merged commit cc1ff1f into master Jan 28, 2026
2 checks passed
@sergio-bobillier sergio-bobillier deleted the as-bucket_selector branch January 28, 2026 09:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants