Skip to content

Commit

Permalink
feature: add aggs support (terms, multi_terms and nested aggs)
Browse files Browse the repository at this point in the history
  • Loading branch information
GokselKUCUKSAHIN committed Apr 14, 2024
1 parent 821dc43 commit 2075948
Showing 1 changed file with 70 additions and 7 deletions.
77 changes: 70 additions & 7 deletions es/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type nestedType Object

type aggsType Object

type aggTermType Object

func unsafeIsNil(x any) bool {
return (*[2]uintptr)(unsafe.Pointer(&x))[1] == 0
}
Expand Down Expand Up @@ -452,22 +454,83 @@ func (n nestedType) ScoreMode(scoreMode ScoreMode.ScoreMode) nestedType {
return n.putInNested("score_mode", scoreMode)
}

// AGGS
func AggTerm(field string) aggTermType {
return aggTermType{
"field": field,
}
}

func (aggTerm aggTermType) Missing(missing string) aggTermType {
aggTerm["missing"] = missing
return aggTerm
}

func Agg() aggsType {
func AggTerms() aggsType {
return aggsType{
"terms": Object{
"field": "my-field",
},
"terms": Object{},
}
}

func AggMultiTerms() aggsType {
return aggsType{
"multi_terms": Object{},
}
}

func AggCustom(agg Object) aggsType {
return aggsType(agg)
}

func (agg aggsType) putInTheField(key string, value any) aggsType {
for field := range agg {
if fieldObject, ok := agg[field].(Object); ok {
fieldObject[key] = value
}
}
return agg
}

func (agg aggsType) Aggs(name string, nestedAgg aggsType) aggsType {
agg["aggs"] = Object{
name: nestedAgg,
}
return agg
}

func (agg aggsType) Field(field string) aggsType {
return agg.putInTheField("field", field)
}

func (agg aggsType) Size(size int) aggsType {
return agg.putInTheField("size", size)
}

func (agg aggsType) Order(field string, order Order.Order) aggsType {
return agg.putInTheField("order",
Object{
field: order,
},
)
}

func (agg aggsType) Include(include string) aggsType {
return agg.putInTheField("include", include)
}

func (agg aggsType) Exclude(exclude string) aggsType {
return agg.putInTheField("exclude", exclude)
}

func (agg aggsType) Terms(terms ...aggTermType) aggsType {
return agg.putInTheField("terms", terms)
}

func (o Object) AddAggs(aggName string, agg aggsType) Object {
func (o Object) Aggs(name string, agg aggsType) Object {
aggs, exists := o["aggs"]
if !exists {
aggs = Object{}
}
aggs.(Object)[aggName] = agg
aggs.(Object)[name] = agg
o["aggs"] = aggs
return o
}

0 comments on commit 2075948

Please sign in to comment.