suggester_context_geo_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 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. data, err := json.Marshal(q.Source())
  16. if err != nil {
  17. t.Fatalf("marshaling to JSON failed: %v", err)
  18. }
  19. got := string(data)
  20. expected := `{"location":{"default":{"lat":0,"lon":0},"neighbors":true,"path":"pin","precision":["1km","5m"],"type":"geo"}}`
  21. if got != expected {
  22. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  23. }
  24. }
  25. func TestSuggesterGeoQuery(t *testing.T) {
  26. q := NewSuggesterGeoQuery("location", GeoPointFromLatLon(11.5, 62.71)).
  27. Precision("1km")
  28. data, err := json.Marshal(q.Source())
  29. if err != nil {
  30. t.Fatalf("marshaling to JSON failed: %v", err)
  31. }
  32. got := string(data)
  33. expected := `{"location":{"precision":"1km","value":{"lat":11.5,"lon":62.71}}}`
  34. if got != expected {
  35. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  36. }
  37. }