Skip to content

Conversation

@sheputis
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: the previous DSL had no way to express post-aggregation filtering, forcing either client-side filtering or dropping to raw JSON. This feature enables pipeline filtering directly through the DSL.

…ilder

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.
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.

2 participants