Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variable width histogram aggregation #1648

Open
wants to merge 1 commit into
base: release-branch.v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions search_aggs_bucket_variable_width_histogram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

// VariableWidthHistogramAggregation is a multi-bucket values source based aggregation
// that can be applied on numeric values extracted from the documents.
// It dynamically builds buckets over the values given a target number of buckets.
// See: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-aggregations-bucket-variablewidthhistogram-aggregation.html
type VariableWidthHistogramAggregation struct {
field string
subAggregations map[string]Aggregation
meta map[string]interface{}

buckets *int64
initialBuffer *int64
shardSize *int64
}

func NewVariableWidthHistogramAggregation() *VariableWidthHistogramAggregation {
return &VariableWidthHistogramAggregation{
subAggregations: make(map[string]Aggregation),
}
}

func (a *VariableWidthHistogramAggregation) Field(field string) *VariableWidthHistogramAggregation {
a.field = field
return a
}

func (a *VariableWidthHistogramAggregation) Buckets(buckets int64) *VariableWidthHistogramAggregation {
a.buckets = &buckets
return a
}

func (a *VariableWidthHistogramAggregation) InitialBuffer(initialBuffer int64) *VariableWidthHistogramAggregation {
a.initialBuffer = &initialBuffer
return a
}

func (a *VariableWidthHistogramAggregation) ShardSize(shardSize int64) *VariableWidthHistogramAggregation {
a.shardSize = &shardSize
return a
}

func (a *VariableWidthHistogramAggregation) SubAggregation(name string, subAggregation Aggregation) *VariableWidthHistogramAggregation {
a.subAggregations[name] = subAggregation
return a
}

// Meta sets the meta data to be included in the aggregation response.
func (a *VariableWidthHistogramAggregation) Meta(metaData map[string]interface{}) *VariableWidthHistogramAggregation {
a.meta = metaData
return a
}

func (a *VariableWidthHistogramAggregation) Source() (interface{}, error) {
// Example:
// {
// "aggs" : {
// "prices" : {
// "variable_width_histogram" : {
// "field" : "price",
// "buckets" : 2
// }
// }
// }
// }
//
// This method returns only the { "variable_width_histogram" : { ... } } part.

source := make(map[string]interface{})
opts := make(map[string]interface{})
source["variable_width_histogram"] = opts

// ValuesSourceAggregationBuilder
if a.field != "" {
opts["field"] = a.field
}

if a.buckets != nil {
opts["buckets"] = *a.buckets
}
if a.initialBuffer != nil {
opts["initial_buffer"] = *a.initialBuffer
}
if a.shardSize != nil {
opts["shard_size"] = *a.shardSize
}

// AggregationBuilder (SubAggregations)
if len(a.subAggregations) > 0 {
aggsMap := make(map[string]interface{})
source["aggregations"] = aggsMap
for name, aggregate := range a.subAggregations {
src, err := aggregate.Source()
if err != nil {
return nil, err
}
aggsMap[name] = src
}
}

// Add Meta data if available
if len(a.meta) > 0 {
source["meta"] = a.meta
}

return source, nil
}
44 changes: 44 additions & 0 deletions search_aggs_bucket_variable_width_histogram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
"encoding/json"
"testing"
)

func TestVariableWidthHistogramAggregation(t *testing.T) {
agg := NewVariableWidthHistogramAggregation().Field("price").Buckets(50)
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"variable_width_histogram":{"buckets":50,"field":"price"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestVariableWidthHistogramAggregationWithMetaData(t *testing.T) {
agg := NewVariableWidthHistogramAggregation().Field("price").InitialBuffer(100).ShardSize(500).Meta(map[string]interface{}{"name": "Oliver"})
src, err := agg.Source()
if err != nil {
t.Fatal(err)
}
data, err := json.Marshal(src)
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"meta":{"name":"Oliver"},"variable_width_histogram":{"field":"price","initial_buffer":100,"shard_size":500}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}