search_queries_geo_distance_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 TestGeoDistanceQuery(t *testing.T) {
  10. q := NewGeoDistanceQuery("pin.location")
  11. q = q.Lat(40)
  12. q = q.Lon(-70)
  13. q = q.Distance("200km")
  14. q = q.DistanceType("plane")
  15. q = q.OptimizeBbox("memory")
  16. src, err := q.Source()
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. data, err := json.Marshal(src)
  21. if err != nil {
  22. t.Fatalf("marshaling to JSON failed: %v", err)
  23. }
  24. got := string(data)
  25. expected := `{"geo_distance":{"distance":"200km","distance_type":"plane","optimize_bbox":"memory","pin.location":{"lat":40,"lon":-70}}}`
  26. if got != expected {
  27. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  28. }
  29. }
  30. func TestGeoDistanceQueryWithGeoPoint(t *testing.T) {
  31. q := NewGeoDistanceQuery("pin.location")
  32. q = q.GeoPoint(GeoPointFromLatLon(40, -70))
  33. q = q.Distance("200km")
  34. src, err := q.Source()
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. data, err := json.Marshal(src)
  39. if err != nil {
  40. t.Fatalf("marshaling to JSON failed: %v", err)
  41. }
  42. got := string(data)
  43. expected := `{"geo_distance":{"distance":"200km","pin.location":{"lat":40,"lon":-70}}}`
  44. if got != expected {
  45. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  46. }
  47. }
  48. func TestGeoDistanceQueryWithGeoHash(t *testing.T) {
  49. q := NewGeoDistanceQuery("pin.location")
  50. q = q.GeoHash("drm3btev3e86")
  51. q = q.Distance("12km")
  52. src, err := q.Source()
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. data, err := json.Marshal(src)
  57. if err != nil {
  58. t.Fatalf("marshaling to JSON failed: %v", err)
  59. }
  60. got := string(data)
  61. expected := `{"geo_distance":{"distance":"12km","pin.location":"drm3btev3e86"}}`
  62. if got != expected {
  63. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  64. }
  65. }