Test
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search_queries_geo_distance_test.go 1.8KB

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