search_aggs_nested_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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 TestNestedAggregation(t *testing.T) {
  10. agg := NewNestedAggregation().Path("resellers")
  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 := `{"nested":{"path":"resellers"}}`
  17. if got != expected {
  18. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  19. }
  20. }
  21. func TestNestedAggregationWithSubAggregation(t *testing.T) {
  22. minPriceAgg := NewMinAggregation().Field("resellers.price")
  23. agg := NewNestedAggregation().Path("resellers").SubAggregation("min_price", minPriceAgg)
  24. data, err := json.Marshal(agg.Source())
  25. if err != nil {
  26. t.Fatalf("marshaling to JSON failed: %v", err)
  27. }
  28. got := string(data)
  29. expected := `{"aggregations":{"min_price":{"min":{"field":"resellers.price"}}},"nested":{"path":"resellers"}}`
  30. if got != expected {
  31. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  32. }
  33. }