search_aggs_bucket_nested_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. package elastic
  5. import (
  6. "encoding/json"
  7. "testing"
  8. )
  9. func TestNestedAggregation(t *testing.T) {
  10. agg := NewNestedAggregation().Path("resellers")
  11. src, err := agg.Source()
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. data, err := json.Marshal(src)
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"nested":{"path":"resellers"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestNestedAggregationWithSubAggregation(t *testing.T) {
  26. minPriceAgg := NewMinAggregation().Field("resellers.price")
  27. agg := NewNestedAggregation().Path("resellers").SubAggregation("min_price", minPriceAgg)
  28. src, err := agg.Source()
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. data, err := json.Marshal(src)
  33. if err != nil {
  34. t.Fatalf("marshaling to JSON failed: %v", err)
  35. }
  36. got := string(data)
  37. expected := `{"aggregations":{"min_price":{"min":{"field":"resellers.price"}}},"nested":{"path":"resellers"}}`
  38. if got != expected {
  39. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  40. }
  41. }
  42. func TestNestedAggregationWithMetaData(t *testing.T) {
  43. agg := NewNestedAggregation().Path("resellers").Meta(map[string]interface{}{"name": "Oliver"})
  44. src, err := agg.Source()
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. data, err := json.Marshal(src)
  49. if err != nil {
  50. t.Fatalf("marshaling to JSON failed: %v", err)
  51. }
  52. got := string(data)
  53. expected := `{"meta":{"name":"Oliver"},"nested":{"path":"resellers"}}`
  54. if got != expected {
  55. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  56. }
  57. }