Skip to content

Commit 1aea68a

Browse files
Feature/terms aggs missing features (#56)
* feature: add missing and script for termsAggType * chore: format import on aggregation_terms_test.go * chore: fix typo
1 parent 78316f2 commit 1aea68a

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

es/aggregation_terms.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,39 @@ func TermsAgg(field string) termsAggType {
2929
}
3030
}
3131

32+
// Missing sets a default value to use for documents that do not contain the field.
33+
//
34+
// Example usage:
35+
//
36+
// agg := es.TermsAgg("category").Missing("unknown")
37+
// // Documents without "category" will be assigned "unknown".
38+
//
39+
// Parameters:
40+
// - missing: The value to use when a document lacks the field.
41+
//
42+
// Returns:
43+
//
44+
// An es.termsAggType object with the "missing" field set.
45+
func (terms termsAggType) Missing(missing any) termsAggType {
46+
return terms.putInTheField("missing", missing)
47+
}
48+
49+
// Script sets a script to compute dynamic bucket values instead of using a field.
50+
//
51+
// Example usage:
52+
//
53+
// agg := es.TermsAgg("category").Script(es.ScriptSource("doc['category'].value + '_modified'", ScriptLanguage.Painless))
54+
//
55+
// Parameters:
56+
// - script: A script for computing dynamic term values.
57+
//
58+
// Returns:
59+
//
60+
// An es.termsAggType object with the "script" field set.
61+
func (terms termsAggType) Script(script scriptType) termsAggType {
62+
return terms.putInTheField("script", script)
63+
}
64+
3265
// Size sets the number of term buckets to return.
3366
//
3467
// Example usage:

es/aggregation_terms_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
CollectMode "github.com/Trendyol/es-query-builder/es/enums/collect-mode"
77
ExecutionHint "github.com/Trendyol/es-query-builder/es/enums/execution-hint"
88
Order "github.com/Trendyol/es-query-builder/es/enums/sort/order"
9+
ScriptLanguage "github.com/Trendyol/es-query-builder/es/enums/script-language"
910

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

43+
func Test_TermsAgg_should_have_Missing_method(t *testing.T) {
44+
t.Parallel()
45+
// Given
46+
a := es.TermsAgg("price")
47+
48+
// When Then
49+
assert.NotNil(t, a.Missing)
50+
}
51+
52+
func Test_Missing_should_add_missing_field_into_TermsAgg(t *testing.T) {
53+
t.Parallel()
54+
// Given
55+
a := es.TermsAgg("price").
56+
Missing("missing_name")
57+
58+
// When Then
59+
assert.NotNil(t, a)
60+
bodyJSON := assert.MarshalWithoutError(t, a)
61+
assert.Equal(t, "{\"terms\":{\"field\":\"price\",\"missing\":\"missing_name\"}}", bodyJSON)
62+
}
63+
64+
func Test_TermsAgg_should_have_Script_method(t *testing.T) {
65+
t.Parallel()
66+
// Given
67+
a := es.TermsAgg("price")
68+
69+
// When Then
70+
assert.NotNil(t, a.Script)
71+
}
72+
73+
func Test_Script_should_add_script_field_into_TermsAgg(t *testing.T) {
74+
t.Parallel()
75+
// Given
76+
a := es.TermsAgg("price").
77+
Script(es.ScriptID("id_12345", ScriptLanguage.Painless))
78+
79+
// When Then
80+
assert.NotNil(t, a)
81+
bodyJSON := assert.MarshalWithoutError(t, a)
82+
// nolint:golint,lll
83+
assert.Equal(t, "{\"terms\":{\"field\":\"price\",\"script\":{\"id\":\"id_12345\",\"lang\":\"painless\"}}}", bodyJSON)
84+
}
85+
4286
func Test_TermsAgg_should_have_ShowTermDocCountError_method(t *testing.T) {
4387
t.Parallel()
4488
// Given

0 commit comments

Comments
 (0)