| 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 |
|
| 5 |
package elastic |
| 6 |
|
| 7 |
import ( |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestSuggestField(t *testing.T) { |
| 13 |
field := NewSuggestField(). |
| 14 |
Input("Welcome to Golang and Elasticsearch.", "Golang and Elasticsearch"). |
| 15 |
Weight(1). |
| 16 |
ContextQuery( |
| 17 |
NewSuggesterCategoryMapping("color").FieldName("color_field").DefaultValues("red", "green", "blue"), |
| 18 |
NewSuggesterGeoMapping("location").Precision("5m").Neighbors(true).DefaultLocations(GeoPointFromLatLon(52.516275, 13.377704)), |
| 19 |
) |
| 20 |
data, err := json.Marshal(field) |
| 21 |
if err != nil { |
| 22 |
t.Fatalf("marshaling to JSON failed: %v", err) |
| 23 |
} |
| 24 |
got := string(data) |
| 25 |
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}` |
| 26 |
if got != expected { |
| 27 |
t.Errorf("expected\n%s\n,got:\n%s", expected, got) |
| 28 |
} |
| 29 |
} |