search_facets_geo_distance_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 TestGeoDistanceFacet(t *testing.T) {
  10. f := NewGeoDistanceFacet().Field("pin.location").
  11. Point(40, -70).
  12. AddUnboundedFrom(10).
  13. AddRange(10, 20).
  14. AddRange(20, 100).
  15. AddUnboundedTo(100)
  16. data, err := json.Marshal(f.Source())
  17. if err != nil {
  18. t.Fatalf("marshaling to JSON failed: %v", err)
  19. }
  20. got := string(data)
  21. expected := `{"geo_distance":{"pin.location":[40,-70],"ranges":[{"to":10},{"from":10,"to":20},{"from":20,"to":100},{"from":100}]}}`
  22. if got != expected {
  23. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  24. }
  25. }
  26. func TestGeoDistanceFacetWithGlobals(t *testing.T) {
  27. f := NewGeoDistanceFacet().Field("pin.location").
  28. Point(40, -70).
  29. AddUnboundedFrom(10).
  30. AddRange(10, 20).
  31. AddRange(20, 100).
  32. AddUnboundedTo(100).
  33. Global(true).
  34. FacetFilter(NewTermFilter("user", "kimchy"))
  35. data, err := json.Marshal(f.Source())
  36. if err != nil {
  37. t.Fatalf("marshaling to JSON failed: %v", err)
  38. }
  39. got := string(data)
  40. expected := `{"facet_filter":{"term":{"user":"kimchy"}},"geo_distance":{"pin.location":[40,-70],"ranges":[{"to":10},{"from":10,"to":20},{"from":20,"to":100},{"from":100}]},"global":true}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }