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.

geo_point_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "context"
  7. "encoding/json"
  8. "testing"
  9. )
  10. func TestGeoPointSource(t *testing.T) {
  11. pt := GeoPoint{Lat: 40, Lon: -70}
  12. data, err := json.Marshal(pt.Source())
  13. if err != nil {
  14. t.Fatalf("marshaling to JSON failed: %v", err)
  15. }
  16. got := string(data)
  17. expected := `{"lat":40,"lon":-70}`
  18. if got != expected {
  19. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  20. }
  21. }
  22. func TestGeoPointMarshalJSON(t *testing.T) {
  23. pt := GeoPoint{Lat: 40, Lon: -70}
  24. data, err := json.Marshal(pt)
  25. if err != nil {
  26. t.Fatalf("marshaling to JSON failed: %v", err)
  27. }
  28. got := string(data)
  29. expected := `{"lat":40,"lon":-70}`
  30. if got != expected {
  31. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  32. }
  33. }
  34. func TestGeoPointIndexAndSearch(t *testing.T) {
  35. client := setupTestClient(t) // , SetTraceLog(log.New(os.Stdout, "", 0)))
  36. // Create index
  37. mapping := `
  38. {
  39. "settings":{
  40. "number_of_shards":1,
  41. "number_of_replicas":0
  42. },
  43. "mappings":{
  44. "doc":{
  45. "properties":{
  46. "name":{
  47. "type":"keyword"
  48. },
  49. "location":{
  50. "type":"geo_point"
  51. }
  52. }
  53. }
  54. }
  55. }
  56. `
  57. createIndex, err := client.CreateIndex(testIndexName).Body(mapping).Do(context.TODO())
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if createIndex == nil {
  62. t.Errorf("expected result to be != nil; got: %v", createIndex)
  63. }
  64. // Add document
  65. type City struct {
  66. Name string `json:"name"`
  67. Location *GeoPoint `json:"location"`
  68. }
  69. munich := &City{
  70. Name: "München",
  71. Location: GeoPointFromLatLon(48.137154, 11.576124),
  72. }
  73. _, err = client.Index().Index(testIndexName).Type("doc").Id("1").BodyJson(&munich).Do(context.TODO())
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. // Flush
  78. _, err = client.Flush().Index(testIndexName).Do(context.TODO())
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. // Get document
  83. q := NewGeoDistanceQuery("location")
  84. q = q.GeoPoint(GeoPointFromLatLon(48, 11))
  85. q = q.Distance("50km")
  86. res, err := client.
  87. Search(testIndexName).
  88. Type("doc").
  89. Query(q).
  90. Do(context.TODO())
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. if want, have := int64(1), res.TotalHits(); want != have {
  95. t.Fatalf("TotalHits: want %d, have %d", want, have)
  96. }
  97. var doc City
  98. if err := json.Unmarshal(*res.Hits.Hits[0].Source, &doc); err != nil {
  99. t.Fatal(err)
  100. }
  101. if want, have := munich.Name, doc.Name; want != have {
  102. t.Fatalf("Name: want %q, have %q", want, have)
  103. }
  104. if want, have := munich.Location.Lat, doc.Location.Lat; want != have {
  105. t.Fatalf("Lat: want %v, have %v", want, have)
  106. }
  107. if want, have := munich.Location.Lon, doc.Location.Lon; want != have {
  108. t.Fatalf("Lon: want %v, have %v", want, have)
  109. }
  110. }