search_aggs_geo_distance_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 TestGeoDistanceAggregation(t *testing.T) {
  10. agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
  11. agg = agg.AddRange(nil, 100)
  12. agg = agg.AddRange(100, 300)
  13. agg = agg.AddRange(300, nil)
  14. data, err := json.Marshal(agg.Source())
  15. if err != nil {
  16. t.Fatalf("marshaling to JSON failed: %v", err)
  17. }
  18. got := string(data)
  19. expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}`
  20. if got != expected {
  21. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  22. }
  23. }
  24. func TestGeoDistanceAggregationWithUnbounded(t *testing.T) {
  25. agg := NewGeoDistanceAggregation().Field("location").Point("52.3760, 4.894")
  26. agg = agg.AddUnboundedFrom(100)
  27. agg = agg.AddRange(100, 300)
  28. agg = agg.AddUnboundedTo(300)
  29. data, err := json.Marshal(agg.Source())
  30. if err != nil {
  31. t.Fatalf("marshaling to JSON failed: %v", err)
  32. }
  33. got := string(data)
  34. expected := `{"geo_distance":{"field":"location","origin":"52.3760, 4.894","ranges":[{"to":100},{"from":100,"to":300},{"from":300}]}}`
  35. if got != expected {
  36. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  37. }
  38. }