suggester_context_geo_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 TestSuggesterGeoMapping(t *testing.T) {
  10. q := NewSuggesterGeoMapping("location").
  11. Precision("1km", "5m").
  12. Neighbors(true).
  13. FieldName("pin").
  14. DefaultLocations(GeoPointFromLatLon(0.0, 0.0))
  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 := `{"location":{"default":{"lat":0,"lon":0},"neighbors":true,"path":"pin","precision":["1km","5m"],"type":"geo"}}`
  25. if got != expected {
  26. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  27. }
  28. }
  29. func TestSuggesterGeoQuery(t *testing.T) {
  30. q := NewSuggesterGeoQuery("location", GeoPointFromLatLon(11.5, 62.71)).Precision("1km")
  31. src, err := q.Source()
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. data, err := json.Marshal(src)
  36. if err != nil {
  37. t.Fatalf("marshaling to JSON failed: %v", err)
  38. }
  39. got := string(data)
  40. expected := `{"location":{"precision":"1km","value":{"lat":11.5,"lon":62.71}}}`
  41. if got != expected {
  42. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  43. }
  44. }