Class: OpenSearch::DSL::Search::Aggregations::BucketSort

Inherits:
Object
  • Object
show all
Includes:
BaseAggregationComponent
Defined in:
lib/opensearch/dsl/search/aggregations/pipeline/bucket_sort.rb

Overview

A parent pipeline aggregation which sorts the buckets of its parent multi-bucket aggregation.

Examples:

Passing the options as a Hash


aggregation :sales_bucket_filter do
  bucket_sort gap_policy: 'insert_zero'
end

Passing the options as a block


aggregation :sales_bucket_sort do
  bucket_sort do
    sort do
      by :total_sales, order: 'desc'
    end
    size 3
  end
end

Instance Method Summary collapse

Methods included from BaseAggregationComponent

included

Instance Method Details

#sort(*args, &block) ⇒ Sort, Hash

Add a sort clause to the search definition.

Examples:


bucket_sort do
  sort do
    by :total_sales, order: 'desc'
  end
end

Returns:

  • (Sort, Hash)

    The sort definition.

Since:

  • 0.1.9



67
68
69
70
71
72
73
74
# File 'lib/opensearch/dsl/search/aggregations/pipeline/bucket_sort.rb', line 67

def sort(*args, &block)
  if !args.empty? || block
    @sort = Sort.new(*args, &block)
    self
  else
    @sort
  end
end

#to_hashHash

Get a hash representation of the aggregation.

client.search(body: s.to_hash)

Examples:


s = search do
  aggregation do
    bucket_sort do
      sort do
        by :total_sales, order: 'desc'
      end
    end
  end
end

Returns:

  • (Hash)

    The hash representation of the aggregation.

Since:

  • 0.1.9



95
96
97
98
99
100
101
102
# File 'lib/opensearch/dsl/search/aggregations/pipeline/bucket_sort.rb', line 95

def to_hash
  call

  @hash[:aggregations] = @aggregations.to_hash if @aggregations

  @hash[name].merge!(sort: @sort.to_hash) if @sort
  @hash
end