| 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 |
"context" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func TestSuggestBuildURL(t *testing.T) { |
| 13 |
client := setupTestClient(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Indices []string |
| 17 |
Expected string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
[]string{}, |
| 21 |
"/_suggest", |
| 22 |
}, |
| 23 |
{ |
| 24 |
[]string{"index1"}, |
| 25 |
"/index1/_suggest", |
| 26 |
}, |
| 27 |
{ |
| 28 |
[]string{"index1", "index2"}, |
| 29 |
"/index1%2Cindex2/_suggest", |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
for i, test := range tests { |
| 34 |
path, _, err := client.Suggest().Index(test.Indices...).buildURL() |
| 35 |
if err != nil { |
| 36 |
t.Errorf("case #%d: %v", i+1, err) |
| 37 |
continue |
| 38 |
} |
| 39 |
if path != test.Expected { |
| 40 |
t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestSuggestService(t *testing.T) { |
| 46 |
client := setupTestClientAndCreateIndex(t) |
| 47 |
// client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 48 |
|
| 49 |
tweet1 := tweet{ |
| 50 |
User: "olivere", |
| 51 |
Message: "Welcome to Golang and Elasticsearch.", |
| 52 |
Tags: []string{"golang", "elasticsearch"}, |
| 53 |
Location: "48.1333,11.5667", // lat,lon |
| 54 |
Suggest: NewSuggestField(). |
| 55 |
Input("Welcome to Golang and Elasticsearch.", "Golang and Elasticsearch"). |
| 56 |
Weight(0), |
| 57 |
} |
| 58 |
tweet2 := tweet{ |
| 59 |
User: "olivere", |
| 60 |
Message: "Another unrelated topic.", |
| 61 |
Tags: []string{"golang"}, |
| 62 |
Location: "48.1189,11.4289", // lat,lon |
| 63 |
Suggest: NewSuggestField(). |
| 64 |
Input("Another unrelated topic.", "Golang topic."). |
| 65 |
Weight(1), |
| 66 |
} |
| 67 |
tweet3 := tweet{ |
| 68 |
User: "sandrae", |
| 69 |
Message: "Cycling is fun.", |
| 70 |
Tags: []string{"sports", "cycling"}, |
| 71 |
Location: "47.7167,11.7167", // lat,lon |
| 72 |
Suggest: NewSuggestField(). |
| 73 |
Input("Cycling is fun."), |
| 74 |
} |
| 75 |
|
| 76 |
// Add all documents |
| 77 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
|
| 82 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 83 |
if err != nil { |
| 84 |
t.Fatal(err) |
| 85 |
} |
| 86 |
|
| 87 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 88 |
if err != nil { |
| 89 |
t.Fatal(err) |
| 90 |
} |
| 91 |
|
| 92 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 93 |
if err != nil { |
| 94 |
t.Fatal(err) |
| 95 |
} |
| 96 |
|
| 97 |
// Test _suggest endpoint |
| 98 |
termSuggesterName := "my-term-suggester" |
| 99 |
termSuggester := NewTermSuggester(termSuggesterName).Text("Goolang").Field("message") |
| 100 |
phraseSuggesterName := "my-phrase-suggester" |
| 101 |
phraseSuggester := NewPhraseSuggester(phraseSuggesterName).Text("Goolang").Field("message") |
| 102 |
completionSuggesterName := "my-completion-suggester" |
| 103 |
completionSuggester := NewCompletionSuggester(completionSuggesterName).Text("Go").Field("suggest_field") |
| 104 |
|
| 105 |
result, err := client.Suggest(). |
| 106 |
Index(testIndexName). |
| 107 |
Suggester(termSuggester). |
| 108 |
Suggester(phraseSuggester). |
| 109 |
Suggester(completionSuggester). |
| 110 |
Do(context.TODO()) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
if result == nil { |
| 115 |
t.Errorf("expected result != nil; got nil") |
| 116 |
} |
| 117 |
if len(result) != 3 { |
| 118 |
t.Errorf("expected 3 suggester results; got %d", len(result)) |
| 119 |
} |
| 120 |
|
| 121 |
termSuggestions, found := result[termSuggesterName] |
| 122 |
if !found { |
| 123 |
t.Errorf("expected to find Suggest[%s]; got false", termSuggesterName) |
| 124 |
} |
| 125 |
if termSuggestions == nil { |
| 126 |
t.Errorf("expected Suggest[%s] != nil; got nil", termSuggesterName) |
| 127 |
} |
| 128 |
if len(termSuggestions) != 1 { |
| 129 |
t.Errorf("expected 1 suggestion; got %d", len(termSuggestions)) |
| 130 |
} |
| 131 |
|
| 132 |
phraseSuggestions, found := result[phraseSuggesterName] |
| 133 |
if !found { |
| 134 |
t.Errorf("expected to find Suggest[%s]; got false", phraseSuggesterName) |
| 135 |
} |
| 136 |
if phraseSuggestions == nil { |
| 137 |
t.Errorf("expected Suggest[%s] != nil; got nil", phraseSuggesterName) |
| 138 |
} |
| 139 |
if len(phraseSuggestions) != 1 { |
| 140 |
t.Errorf("expected 1 suggestion; got %d", len(phraseSuggestions)) |
| 141 |
} |
| 142 |
|
| 143 |
completionSuggestions, found := result[completionSuggesterName] |
| 144 |
if !found { |
| 145 |
t.Errorf("expected to find Suggest[%s]; got false", completionSuggesterName) |
| 146 |
} |
| 147 |
if completionSuggestions == nil { |
| 148 |
t.Errorf("expected Suggest[%s] != nil; got nil", completionSuggesterName) |
| 149 |
} |
| 150 |
if len(completionSuggestions) != 1 { |
| 151 |
t.Errorf("expected 1 suggestion; got %d", len(completionSuggestions)) |
| 152 |
} |
| 153 |
if len(completionSuggestions[0].Options) != 2 { |
| 154 |
t.Errorf("expected 2 suggestion options; got %d", len(completionSuggestions[0].Options)) |
| 155 |
} |
| 156 |
if have, want := completionSuggestions[0].Options[0].Text, "Golang topic."; have != want { |
| 157 |
t.Errorf("expected Suggest[%s][0].Options[0].Text == %q; got %q", completionSuggesterName, want, have) |
| 158 |
} |
| 159 |
if have, want := completionSuggestions[0].Options[1].Text, "Golang and Elasticsearch"; have != want { |
| 160 |
t.Errorf("expected Suggest[%s][0].Options[1].Text == %q; got %q", completionSuggesterName, want, have) |
| 161 |
} |
| 162 |
} |