Debian Codegraph @

search_aggs_bucket_children_test.go

raw
1 // Copyright 2012-present Oliver Eilhard. All rights reserved.
2 // Use of this source code is governed by a MIT-license.
3 // See http://olivere.mit-license.org/license.txt for details.
4
5 package elastic
6
7 import (
8 "encoding/json"
9 "testing"
10 )
11
12 func TestChildrenAggregation(t *testing.T) {
13 agg := NewChildrenAggregation().Type("answer")
14 src, err := agg.Source()
15 if err != nil {
16 t.Fatal(err)
17 }
18 data, err := json.Marshal(src)
19 if err != nil {
20 t.Fatalf("marshaling to JSON failed: %v", err)
21 }
22 got := string(data)
23 expected := `{"children":{"type":"answer"}}`
24 if got != expected {
25 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
26 }
27 }
28
29 func TestChildrenAggregationWithSubAggregation(t *testing.T) {
30 subAgg := NewTermsAggregation().Field("owner.display_name").Size(10)
31 agg := NewChildrenAggregation().Type("answer")
32 agg = agg.SubAggregation("top-names", subAgg)
33 src, err := agg.Source()
34 if err != nil {
35 t.Fatal(err)
36 }
37 data, err := json.Marshal(src)
38 if err != nil {
39 t.Fatalf("marshaling to JSON failed: %v", err)
40 }
41 got := string(data)
42 expected := `{"aggregations":{"top-names":{"terms":{"field":"owner.display_name","size":10}}},"children":{"type":"answer"}}`
43 if got != expected {
44 t.Errorf("expected\n%s\n,got:\n%s", expected, got)
45 }
46 }