forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch_aggs_filter_test.go
36 lines (33 loc) · 1.1 KB
/
search_aggs_filter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package elastic
import (
"encoding/json"
"testing"
)
func TestFilterAggregation(t *testing.T) {
filter := NewRangeFilter("stock").Gt(0)
agg := NewFilterAggregation().Filter(filter)
data, err := json.Marshal(agg.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"filter":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}
func TestFilterAggregationWithSubAggregation(t *testing.T) {
avgPriceAgg := NewAvgAggregation().Field("price")
filter := NewRangeFilter("stock").Gt(0)
agg := NewFilterAggregation().Filter(filter).
SubAggregation("avg_price", avgPriceAgg)
data, err := json.Marshal(agg.Source())
if err != nil {
t.Fatalf("marshaling to JSON failed: %v", err)
}
got := string(data)
expected := `{"aggregations":{"avg_price":{"avg":{"field":"price"}}},"filter":{"range":{"stock":{"from":0,"include_lower":false,"include_upper":true,"to":null}}}}`
if got != expected {
t.Errorf("expected\n%s\n,got:\n%s", expected, got)
}
}