search_aggs_children_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. // Copyright 2012-2015 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. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestChildrenAggregation(t *testing.T) {
  10. agg := NewChildrenAggregation().Type("answer")
  11. data, err := json.Marshal(agg.Source())
  12. if err != nil {
  13. t.Fatalf("marshaling to JSON failed: %v", err)
  14. }
  15. got := string(data)
  16. expected := `{"type":"answer"}`
  17. if got != expected {
  18. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  19. }
  20. }
  21. func TestChildrenAggregationWithSubAggregation(t *testing.T) {
  22. subAgg := NewTermsAggregation().Field("owner.display_name").Size(10)
  23. agg := NewChildrenAggregation().Type("answer")
  24. agg = agg.SubAggregation("top-names", subAgg)
  25. data, err := json.Marshal(agg.Source())
  26. if err != nil {
  27. t.Fatalf("marshaling to JSON failed: %v", err)
  28. }
  29. got := string(data)
  30. expected := `{"aggregations":{"top-names":{"terms":{"field":"owner.display_name","size":10}}},"type":"answer"}`
  31. if got != expected {
  32. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  33. }
  34. }