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.
attr_reader :buckets_path, :script, :gap_policy

# @param [String] name The name used by Elasticsearch to identify the aggregation.
# @param [Hash,String] buckets_path Path(s) to the metrics in parent aggs.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Missing space after the comma:

Suggested change
# @param [Hash,String] buckets_path Path(s) to the metrics in parent aggs.
# @param [Hash, String] buckets_path Path(s) to the metrics in parent aggs.

self.class.new(
name,
buckets_path: buckets_path.is_a?(Hash) ? buckets_path.dup : buckets_path,
script: script, # Script is immutable-ish, ok to reuse
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please take care of the linter warning on this line.


let(:name) { 'only_slow_tests' }
let(:buckets_path) { { total: 'total_duration_ms' } }
let(:script) do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Please add an empty line between lines 519 and 520

JayAPI::Elasticsearch::QueryBuilder::Script
)
end
let(:gap_policy) { nil }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Please add an empty line between lines 524 and 525


it 'creates the BucketSelector instance with the expected parameters' do
expect(JayAPI::Elasticsearch::QueryBuilder::Aggregations::BucketSelector)
.to receive(:new).with(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please reformat this to avoid the RSpec/ExampleLength linter warning:

Suggested change
.to receive(:new).with(
.to receive(:new).with(name, buckets_path: buckets_path, script: script, gap_policy: gap_policy)


it 'creates the BucketSelector instance with the expected parameters' do
expect(JayAPI::Elasticsearch::QueryBuilder::Aggregations::BucketSelector)
.to receive(:new).with(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Likewise here, please reflow the code to avoid the linter warning.

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