Skip to content

Commit

Permalink
Support partition-based terms aggregations
Browse files Browse the repository at this point in the history
Terms aggregations have changed. Besides removing the regexp flags,
they now support partition-based aggregations.

This commit updates the TermsAggregation to align features with
Elasticsearch 5.5+.

It will be available in 5.0.45 or later.

Close #573
  • Loading branch information
olivere committed Aug 1, 2017
1 parent d76752b commit edbef41
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 48 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

const (
// Version is the current version of Elastic.
Version = "5.0.44"
Version = "5.0.45"

// DefaultURL is the default endpoint of Elasticsearch on the local machine.
// It is used e.g. when initializing a new Client without a specific URL.
Expand Down
110 changes: 63 additions & 47 deletions search_aggs_bucket_terms.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,10 @@ type TermsAggregation struct {
minDocCount *int
shardMinDocCount *int
valueType string
includePattern string
includeFlags *int
excludePattern string
excludeFlags *int
includeExclude *TermsAggregationIncludeExclude
executionHint string
collectionMode string
showTermDocCountError *bool
includeTerms []string
excludeTerms []string
order []TermsOrder
}

Expand Down Expand Up @@ -91,24 +86,50 @@ func (a *TermsAggregation) ShardMinDocCount(shardMinDocCount int) *TermsAggregat
}

func (a *TermsAggregation) Include(regexp string) *TermsAggregation {
a.includePattern = regexp
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Include = regexp
return a
}

func (a *TermsAggregation) IncludeWithFlags(regexp string, flags int) *TermsAggregation {
a.includePattern = regexp
a.includeFlags = &flags
func (a *TermsAggregation) IncludeValues(values ...interface{}) *TermsAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.IncludeValues = append(a.includeExclude.IncludeValues, values...)
return a
}

func (a *TermsAggregation) Exclude(regexp string) *TermsAggregation {
a.excludePattern = regexp
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Exclude = regexp
return a
}

func (a *TermsAggregation) ExcludeValues(values ...interface{}) *TermsAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.ExcludeValues = append(a.includeExclude.ExcludeValues, values...)
return a
}

func (a *TermsAggregation) Partition(p int) *TermsAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.Partition = p
return a
}

func (a *TermsAggregation) ExcludeWithFlags(regexp string, flags int) *TermsAggregation {
a.excludePattern = regexp
a.excludeFlags = &flags
func (a *TermsAggregation) NumPartitions(n int) *TermsAggregation {
if a.includeExclude == nil {
a.includeExclude = &TermsAggregationIncludeExclude{}
}
a.includeExclude.NumPartitions = n
return a
}

Expand Down Expand Up @@ -207,16 +228,6 @@ func (a *TermsAggregation) ShowTermDocCountError(showTermDocCountError bool) *Te
return a
}

func (a *TermsAggregation) IncludeTerms(terms ...string) *TermsAggregation {
a.includeTerms = append(a.includeTerms, terms...)
return a
}

func (a *TermsAggregation) ExcludeTerms(terms ...string) *TermsAggregation {
a.excludeTerms = append(a.excludeTerms, terms...)
return a
}

func (a *TermsAggregation) Source() (interface{}, error) {
// Example:
// {
Expand Down Expand Up @@ -283,32 +294,27 @@ func (a *TermsAggregation) Source() (interface{}, error) {
}
opts["order"] = orderSlice
}
if len(a.includeTerms) > 0 {
opts["include"] = a.includeTerms
}
if a.includePattern != "" {
if a.includeFlags == nil || *a.includeFlags == 0 {
opts["include"] = a.includePattern
} else {
p := make(map[string]interface{})
p["pattern"] = a.includePattern
p["flags"] = *a.includeFlags
opts["include"] = p
// Include/Exclude
if ie := a.includeExclude; ie != nil {
// Include
if ie.Include != "" {
opts["include"] = ie.Include
} else if len(ie.IncludeValues) > 0 {
opts["include"] = ie.IncludeValues
} else if ie.NumPartitions > 0 {
inc := make(map[string]interface{})
inc["partition"] = ie.Partition
inc["num_partitions"] = ie.NumPartitions
opts["include"] = inc
}
}
if len(a.excludeTerms) > 0 {
opts["exclude"] = a.excludeTerms
}
if a.excludePattern != "" {
if a.excludeFlags == nil || *a.excludeFlags == 0 {
opts["exclude"] = a.excludePattern
} else {
p := make(map[string]interface{})
p["pattern"] = a.excludePattern
p["flags"] = *a.excludeFlags
opts["exclude"] = p
// Exclude
if ie.Exclude != "" {
opts["exclude"] = ie.Exclude
} else if len(ie.ExcludeValues) > 0 {
opts["exclude"] = ie.ExcludeValues
}
}

if a.executionHint != "" {
opts["execution_hint"] = a.executionHint
}
Expand All @@ -334,6 +340,16 @@ func (a *TermsAggregation) Source() (interface{}, error) {
return source, nil
}

// TermsAggregationIncludeExclude allows for include/exclude in a TermsAggregation.
type TermsAggregationIncludeExclude struct {
Include string
Exclude string
IncludeValues []interface{}
ExcludeValues []interface{}
Partition int
NumPartitions int
}

// TermsOrder specifies a single order field for a terms aggregation.
type TermsOrder struct {
Field string
Expand Down
51 changes: 51 additions & 0 deletions search_aggs_bucket_terms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,54 @@ func TestTermsAggregationWithMissing(t *testing.T) {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestTermsAggregationWithIncludeExclude(t *testing.T) {
agg := NewTermsAggregation().Field("tags").Include(".*sport.*").Exclude("water_.*")
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 := `{"terms":{"exclude":"water_.*","field":"tags","include":".*sport.*"}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestTermsAggregationWithIncludeExcludeValues(t *testing.T) {
agg := NewTermsAggregation().Field("make").IncludeValues("mazda", "honda").ExcludeValues("rover", "jensen")
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 := `{"terms":{"exclude":["rover","jensen"],"field":"make","include":["mazda","honda"]}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

func TestTermsAggregationWithPartitions(t *testing.T) {
agg := NewTermsAggregation().Field("account_id").Partition(0).NumPartitions(20)
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 := `{"terms":{"field":"account_id","include":{"num_partitions":20,"partition":0}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}

0 comments on commit edbef41

Please sign in to comment.