Skip to content

Commit

Permalink
Features: Filter Aggregations, Nested Aggregations, Multi Match Queri…
Browse files Browse the repository at this point in the history
…es, Highlights (#12)

* Add support for multi_match queries

* Add support for highlights

* Add support for nested aggregations and filtered aggregations

* Update README

* Fix formatting
  • Loading branch information
cchamplin authored Mar 14, 2021
1 parent 259a3cd commit fc16427
Show file tree
Hide file tree
Showing 11 changed files with 953 additions and 6 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {
).
Size(20).
Run(
es,
es,
es.Search.WithContext(context.TODO()),
es.Search.WithIndex("test"),
)
Expand Down Expand Up @@ -124,6 +124,7 @@ The following queries are currently supported:
| `"match_phrase_prefix"` | `MatchPhrasePrefix()` |
| `"match_all"` | `MatchAll()` |
| `"match_none"` | `MatchNone()` |
| `"multi_match"` | `MultiMatch()` |
| `"exists"` | `Exists()` |
| `"fuzzy"` | `Fuzzy()` |
| `"ids"` | `IDs()` |
Expand Down Expand Up @@ -158,6 +159,23 @@ The following aggregations are currently supported:
| `"top_hits"` | `TopHits()` |
| `"terms"` | `TermsAgg()` |

### Supported Top Level Options

The following top level options are currently supported:

| ElasticSearch DSL | `esquery.Search` Function |
| ------------------------|--------------------------------------- |
| `"highlight"` | `Highlight()` |
| `"explain"` | `Explain()` |
| `"from"` | `From()` |
| `"postFilter"` | `PostFilter()` |
| `"query"` | `Query()` |
| `"aggs"` | `Aggs()` |
| `"size"` | `Size()` |
| `"sort"` | `Sort()` |
| `"source"` | `SourceIncludes(), SourceExcludes()` |
| `"timeout"` | `Timeout()` |

#### Custom Queries and Aggregations

To execute an arbitrary query or aggregation (including those not yet supported by the library), use the `CustomQuery()` or `CustomAgg()` functions, respectively. Both accept any `map[string]interface{}` value.
Expand Down
34 changes: 34 additions & 0 deletions aggregations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,39 @@ func TestAggregations(t *testing.T) {
},
},
},
{
"a complex, multi-aggregation, nested",
Aggregate(
NestedAgg("categories","categories").
Aggs(TermsAgg("type","outdoors")),
FilterAgg("filtered",
Term("type", "t-shirt")),
),
map[string]interface{}{
"aggs": map[string]interface{}{
"categories": map[string]interface{}{
"nested": map[string]interface{}{
"path": "categories",
},
"aggs": map[string]interface{} {
"type": map[string]interface{} {
"terms": map[string]interface{} {
"field": "outdoors",
},
},
},
},
"filtered": map[string]interface{}{
"filter": map[string]interface{}{
"term": map[string]interface{}{
"type": map[string]interface{} {
"value": "t-shirt",
},
},
},
},
},
},
},
})
}
49 changes: 49 additions & 0 deletions aggs_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package esquery

type FilterAggregation struct {
name string
filter Mappable
aggs []Aggregation
}

// Filter creates a new aggregation of type "filter". The method name includes
// the "Agg" suffix to prevent conflict with the "filter" query.
func FilterAgg(name string, filter Mappable) *FilterAggregation {
return &FilterAggregation{
name: name,
filter: filter,
}
}

// Name returns the name of the aggregation.
func (agg *FilterAggregation) Name() string {
return agg.name
}

// Filter sets the filter items
func (agg *FilterAggregation) Filter(filter Mappable) *FilterAggregation {
agg.filter = filter
return agg
}

// Aggs sets sub-aggregations for the aggregation.
func (agg *FilterAggregation) Aggs(aggs ...Aggregation) *FilterAggregation {
agg.aggs = aggs
return agg
}

func (agg *FilterAggregation) Map() map[string]interface{} {
outerMap := map[string]interface{}{
"filter": agg.filter.Map(),
}

if len(agg.aggs) > 0 {
subAggs := make(map[string]map[string]interface{})
for _, sub := range agg.aggs {
subAggs[sub.Name()] = sub.Map()
}
outerMap["aggs"] = subAggs
}

return outerMap
}
42 changes: 42 additions & 0 deletions aggs_filter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package esquery

import "testing"

func TestFilterAggs(t *testing.T) {
runMapTests(t, []mapTest{
{
"filter agg: simple",
FilterAgg("filtered", Term("type", "t-shirt")),
map[string]interface{}{
"filter": map[string]interface{}{
"term": map[string]interface{}{
"type": map[string]interface{}{
"value": "t-shirt",
},
},
},
},
},
{
"filter agg: with aggs",
FilterAgg("filtered", Term("type", "t-shirt")).
Aggs(Avg("avg_price", "price")),
map[string]interface{}{
"filter": map[string]interface{}{
"term": map[string]interface{}{
"type": map[string]interface{}{
"value": "t-shirt",
},
},
},
"aggs": map[string]interface{}{
"avg_price": map[string]interface{}{
"avg": map[string]interface{}{
"field": "price",
},
},
},
},
},
})
}
53 changes: 53 additions & 0 deletions aggs_nested.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package esquery

type NestedAggregation struct {
name string
path string
aggs []Aggregation
}

// NestedAgg creates a new aggregation of type "nested". The method name includes
// the "Agg" suffix to prevent conflict with the "nested" query.
func NestedAgg(name string, path string) *NestedAggregation {
return &NestedAggregation{
name: name,
path: path,
}
}

// Name returns the name of the aggregation.
func (agg *NestedAggregation) Name() string {
return agg.name
}

// NumberOfFragments sets the aggregations path
func (agg *NestedAggregation) Path(p string) *NestedAggregation {
agg.path = p
return agg
}

// Aggs sets sub-aggregations for the aggregation.
func (agg *NestedAggregation) Aggs(aggs ...Aggregation) *NestedAggregation {
agg.aggs = aggs
return agg
}

func (agg *NestedAggregation) Map() map[string]interface{} {
innerMap := map[string]interface{}{
"path": agg.path,
}

outerMap := map[string]interface{}{
"nested": innerMap,
}

if len(agg.aggs) > 0 {
subAggs := make(map[string]map[string]interface{})
for _, sub := range agg.aggs {
subAggs[sub.Name()] = sub.Map()
}
outerMap["aggs"] = subAggs
}

return outerMap
}
34 changes: 34 additions & 0 deletions aggs_nested_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package esquery

import "testing"

func TestNestedAggs(t *testing.T) {
runMapTests(t, []mapTest{
{
"nested agg: simple",
NestedAgg("simple", "categories"),
map[string]interface{}{
"nested": map[string]interface{}{
"path": "categories",
},
},
},
{
"nested agg: with aggs",
NestedAgg("more_nested", "authors").
Aggs(TermsAgg("authors", "name")),
map[string]interface{}{
"nested": map[string]interface{}{
"path": "authors",
},
"aggs": map[string]interface{}{
"authors": map[string]interface{}{
"terms": map[string]interface{}{
"field": "name",
},
},
},
},
},
})
}
Loading

0 comments on commit fc16427

Please sign in to comment.