suggest_field_test.go 1.1 KB

1234567891011121314151617181920212223242526272829
  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 TestSuggestField(t *testing.T) {
  10. field := NewSuggestField().
  11. Input("Welcome to Golang and Elasticsearch.", "Golang and Elasticsearch").
  12. Weight(1).
  13. ContextQuery(
  14. NewSuggesterCategoryMapping("color").FieldName("color_field").DefaultValues("red", "green", "blue"),
  15. NewSuggesterGeoMapping("location").Precision("5m").Neighbors(true).DefaultLocations(GeoPointFromLatLon(52.516275, 13.377704)),
  16. )
  17. data, err := json.Marshal(field)
  18. if err != nil {
  19. t.Fatalf("marshaling to JSON failed: %v", err)
  20. }
  21. got := string(data)
  22. expected := `{"context":{"color":{"default":["red","green","blue"],"path":"color_field","type":"category"},"location":{"default":{"lat":52.516275,"lon":13.377704},"neighbors":true,"precision":["5m"],"type":"geo"}},"input":["Welcome to Golang and Elasticsearch.","Golang and Elasticsearch"],"weight":1}`
  23. if got != expected {
  24. t.Errorf("expected\n%s\n,got:\n%s", expected, got)
  25. }
  26. }