Skip to content
Merged
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
33 changes: 33 additions & 0 deletions es/aggregation_terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ func TermsAgg(field string) termsAggType {
}
}

// Missing sets a default value to use for documents that do not contain the field.
//
// Example usage:
//
// agg := es.TermsAgg("category").Missing("unknown")
// // Documents without "category" will be assigned "unknown".
//
// Parameters:
// - missing: The value to use when a document lacks the field.
//
// Returns:
//
// An es.termsAggType object with the "missing" field set.
func (terms termsAggType) Missing(missing any) termsAggType {
return terms.putInTheField("missing", missing)
}

// Script sets a script to compute dynamic bucket values instead of using a field.
//
// Example usage:
//
// agg := es.TermsAgg("category").Script(es.ScriptSource("doc['category'].value + '_modified'", ScriptLanguage.Painless))
//
// Parameters:
// - script: A script for computing dynamic term values.
//
// Returns:
//
// An es.termsAggType object with the "script" field set.
func (terms termsAggType) Script(script scriptType) termsAggType {
return terms.putInTheField("script", script)
}

// Size sets the number of term buckets to return.
//
// Example usage:
Expand Down
44 changes: 44 additions & 0 deletions es/aggregation_terms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
CollectMode "github.com/Trendyol/es-query-builder/es/enums/collect-mode"
ExecutionHint "github.com/Trendyol/es-query-builder/es/enums/execution-hint"
Order "github.com/Trendyol/es-query-builder/es/enums/sort/order"
ScriptLanguage "github.com/Trendyol/es-query-builder/es/enums/script-language"

"github.com/Trendyol/es-query-builder/es"
"github.com/Trendyol/es-query-builder/test/assert"
Expand Down Expand Up @@ -39,6 +40,49 @@ func Test_TermsAgg_should_create_json_with_terms_field_inside(t *testing.T) {
assert.Equal(t, "{\"terms\":{\"field\":\"price\"}}", bodyJSON)
}

func Test_TermsAgg_should_have_Missing_method(t *testing.T) {
t.Parallel()
// Given
a := es.TermsAgg("price")

// When Then
assert.NotNil(t, a.Missing)
}

func Test_Missing_should_add_missing_field_into_TermsAgg(t *testing.T) {
t.Parallel()
// Given
a := es.TermsAgg("price").
Missing("missing_name")

// When Then
assert.NotNil(t, a)
bodyJSON := assert.MarshalWithoutError(t, a)
assert.Equal(t, "{\"terms\":{\"field\":\"price\",\"missing\":\"missing_name\"}}", bodyJSON)
}

func Test_TermsAgg_should_have_Script_method(t *testing.T) {
t.Parallel()
// Given
a := es.TermsAgg("price")

// When Then
assert.NotNil(t, a.Script)
}

func Test_Script_should_add_script_field_into_TermsAgg(t *testing.T) {
t.Parallel()
// Given
a := es.TermsAgg("price").
Script(es.ScriptID("id_12345", ScriptLanguage.Painless))

// When Then
assert.NotNil(t, a)
bodyJSON := assert.MarshalWithoutError(t, a)
// nolint:golint,lll
assert.Equal(t, "{\"terms\":{\"field\":\"price\",\"script\":{\"id\":\"id_12345\",\"lang\":\"painless\"}}}", bodyJSON)
}

func Test_TermsAgg_should_have_ShowTermDocCountError_method(t *testing.T) {
t.Parallel()
// Given
Expand Down