diff options
author | Christopher Speller <crspeller@gmail.com> | 2017-11-13 09:09:58 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-13 09:09:58 -0800 |
commit | 1329aa51b605cb54ba9aae3a82a0a87b881fb7b3 (patch) | |
tree | 93cbf354ab894a560fc2cef8ef685d681b4ff889 /vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go | |
parent | 7304a61ef597970be3031b14e652fb3a4df44304 (diff) | |
download | chat-1329aa51b605cb54ba9aae3a82a0a87b881fb7b3.tar.gz chat-1329aa51b605cb54ba9aae3a82a0a87b881fb7b3.tar.bz2 chat-1329aa51b605cb54ba9aae3a82a0a87b881fb7b3.zip |
Updating server dependancies. (#7816)
Diffstat (limited to 'vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go')
-rw-r--r-- | vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go b/vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go index 78232e9d6..a555e3462 100644 --- a/vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go +++ b/vendor/gopkg.in/olivere/elastic.v5/search_suggester_test.go @@ -238,3 +238,109 @@ func TestCompletionSuggester(t *testing.T) { t.Errorf("expected Text = 'Golang'; got %s", myOption.Text) } } + +func TestContextSuggester(t *testing.T) { + client := setupTestClientAndCreateIndex(t) // , SetTraceLog(log.New(os.Stdout, "", 0))) + + // TODO make a nice way of creating tweets, as currently the context fields are unsupported as part of the suggestion fields + tweet1 := ` + { + "user":"olivere", + "message":"Welcome to Golang and Elasticsearch.", + "retweets":0, + "created":"0001-01-01T00:00:00Z", + "suggest_field":{ + "input":[ + "Golang", + "Elasticsearch" + ], + "contexts":{ + "user_name": ["olivere"] + } + } + } + ` + tweet2 := ` + { + "user":"sandrae", + "message":"I like golfing", + "retweets":0, + "created":"0001-01-01T00:00:00Z", + "suggest_field":{ + "input":[ + "Golfing" + ], + "contexts":{ + "user_name": ["sandrae"] + } + } + } + ` + + // Add all documents + _, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyString(tweet1).Do(context.TODO()) + if err != nil { + t.Fatal(err) + } + + _, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyString(tweet2).Do(context.TODO()) + if err != nil { + t.Fatal(err) + } + + _, err = client.Flush().Index(testIndexName).Do(context.TODO()) + if err != nil { + t.Fatal(err) + } + + suggesterName := "my-suggestions" + cs := NewContextSuggester(suggesterName) + cs = cs.Prefix("Gol") + cs = cs.Field("suggest_field") + cs = cs.ContextQueries( + NewSuggesterCategoryQuery("user_name", "olivere"), + ) + + searchResult, err := client.Search(). + Index(testIndexName). + Suggester(cs). + Do(context.TODO()) + if err != nil { + t.Fatal(err) + } + if searchResult.Suggest == nil { + t.Errorf("expected SearchResult.Suggest != nil; got nil") + } + mySuggestions, found := searchResult.Suggest[suggesterName] + if !found { + t.Errorf("expected to find SearchResult.Suggest[%s]; got false", suggesterName) + } + if mySuggestions == nil { + t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", suggesterName) + } + + // sandra's tweet is not returned because of the user_name context + if len(mySuggestions) != 1 { + t.Errorf("expected 1 suggestion; got %d", len(mySuggestions)) + } + mySuggestion := mySuggestions[0] + if mySuggestion.Text != "Gol" { + t.Errorf("expected Text = 'Gol'; got %s", mySuggestion.Text) + } + if mySuggestion.Offset != 0 { + t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset) + } + if mySuggestion.Length != 3 { + t.Errorf("expected Length = %d; got %d", 3, mySuggestion.Length) + } + if len(mySuggestion.Options) != 1 { + t.Errorf("expected 1 option; got %d", len(mySuggestion.Options)) + } + myOption := mySuggestion.Options[0] + if myOption.Text != "Golang" { + t.Errorf("expected Text = 'Golang'; got %s", myOption.Text) + } + if myOption.Id != "1" { + t.Errorf("expected Id = '1'; got %s", myOption.Id) + } +} |