| 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 TestTermSuggester(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 16 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 17 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 18 |
|
| 19 |
// Add all documents |
| 20 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
} |
| 24 |
|
| 25 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 26 |
if err != nil { |
| 27 |
t.Fatal(err) |
| 28 |
} |
| 29 |
|
| 30 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 31 |
if err != nil { |
| 32 |
t.Fatal(err) |
| 33 |
} |
| 34 |
|
| 35 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 36 |
if err != nil { |
| 37 |
t.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
// Match all should return all documents |
| 41 |
tsName := "my-suggestions" |
| 42 |
ts := NewTermSuggester(tsName) |
| 43 |
ts = ts.Text("Goolang") |
| 44 |
ts = ts.Field("message") |
| 45 |
|
| 46 |
searchResult, err := client.Search(). |
| 47 |
Index(testIndexName). |
| 48 |
Query(NewMatchAllQuery()). |
| 49 |
Suggester(ts). |
| 50 |
Do(context.TODO()) |
| 51 |
if err != nil { |
| 52 |
t.Fatal(err) |
| 53 |
} |
| 54 |
if searchResult.Suggest == nil { |
| 55 |
t.Errorf("expected SearchResult.Suggest != nil; got nil") |
| 56 |
} |
| 57 |
mySuggestions, found := searchResult.Suggest[tsName] |
| 58 |
if !found { |
| 59 |
t.Errorf("expected to find SearchResult.Suggest[%s]; got false", tsName) |
| 60 |
} |
| 61 |
if mySuggestions == nil { |
| 62 |
t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", tsName) |
| 63 |
} |
| 64 |
|
| 65 |
if len(mySuggestions) != 1 { |
| 66 |
t.Errorf("expected 1 suggestion; got %d", len(mySuggestions)) |
| 67 |
} |
| 68 |
mySuggestion := mySuggestions[0] |
| 69 |
if mySuggestion.Text != "goolang" { |
| 70 |
t.Errorf("expected Text = 'goolang'; got %s", mySuggestion.Text) |
| 71 |
} |
| 72 |
if mySuggestion.Offset != 0 { |
| 73 |
t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset) |
| 74 |
} |
| 75 |
if mySuggestion.Length != 7 { |
| 76 |
t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length) |
| 77 |
} |
| 78 |
if len(mySuggestion.Options) != 1 { |
| 79 |
t.Errorf("expected 1 option; got %d", len(mySuggestion.Options)) |
| 80 |
} |
| 81 |
myOption := mySuggestion.Options[0] |
| 82 |
if myOption.Text != "golang" { |
| 83 |
t.Errorf("expected Text = 'golang'; got %s", myOption.Text) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestPhraseSuggester(t *testing.T) { |
| 88 |
client := setupTestClientAndCreateIndex(t) |
| 89 |
|
| 90 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 91 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 92 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 93 |
|
| 94 |
// Add all documents |
| 95 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 101 |
if err != nil { |
| 102 |
t.Fatal(err) |
| 103 |
} |
| 104 |
|
| 105 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
|
| 110 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 111 |
if err != nil { |
| 112 |
t.Fatal(err) |
| 113 |
} |
| 114 |
|
| 115 |
// Match all should return all documents |
| 116 |
phraseSuggesterName := "my-suggestions" |
| 117 |
ps := NewPhraseSuggester(phraseSuggesterName) |
| 118 |
ps = ps.Text("Goolang") |
| 119 |
ps = ps.Field("message") |
| 120 |
|
| 121 |
searchResult, err := client.Search(). |
| 122 |
Index(testIndexName). |
| 123 |
Query(NewMatchAllQuery()). |
| 124 |
Suggester(ps). |
| 125 |
Do(context.TODO()) |
| 126 |
if err != nil { |
| 127 |
t.Fatal(err) |
| 128 |
} |
| 129 |
if searchResult.Suggest == nil { |
| 130 |
t.Errorf("expected SearchResult.Suggest != nil; got nil") |
| 131 |
} |
| 132 |
mySuggestions, found := searchResult.Suggest[phraseSuggesterName] |
| 133 |
if !found { |
| 134 |
t.Errorf("expected to find SearchResult.Suggest[%s]; got false", phraseSuggesterName) |
| 135 |
} |
| 136 |
if mySuggestions == nil { |
| 137 |
t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", phraseSuggesterName) |
| 138 |
} |
| 139 |
|
| 140 |
if len(mySuggestions) != 1 { |
| 141 |
t.Errorf("expected 1 suggestion; got %d", len(mySuggestions)) |
| 142 |
} |
| 143 |
mySuggestion := mySuggestions[0] |
| 144 |
if mySuggestion.Text != "Goolang" { |
| 145 |
t.Errorf("expected Text = 'Goolang'; got %s", mySuggestion.Text) |
| 146 |
} |
| 147 |
if mySuggestion.Offset != 0 { |
| 148 |
t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset) |
| 149 |
} |
| 150 |
if mySuggestion.Length != 7 { |
| 151 |
t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
func TestCompletionSuggester(t *testing.T) { |
| 156 |
client := setupTestClientAndCreateIndex(t) // , SetTraceLog(log.New(os.Stdout, "", 0))) |
| 157 |
|
| 158 |
tweet1 := tweet{ |
| 159 |
User: "olivere", |
| 160 |
Message: "Welcome to Golang and Elasticsearch.", |
| 161 |
Suggest: NewSuggestField("Golang", "Elasticsearch"), |
| 162 |
} |
| 163 |
tweet2 := tweet{ |
| 164 |
User: "olivere", |
| 165 |
Message: "Another unrelated topic.", |
| 166 |
Suggest: NewSuggestField("Another unrelated topic."), |
| 167 |
} |
| 168 |
tweet3 := tweet{ |
| 169 |
User: "sandrae", |
| 170 |
Message: "Cycling is fun.", |
| 171 |
Suggest: NewSuggestField("Cycling is fun."), |
| 172 |
} |
| 173 |
|
| 174 |
// Add all documents |
| 175 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 176 |
if err != nil { |
| 177 |
t.Fatal(err) |
| 178 |
} |
| 179 |
|
| 180 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 181 |
if err != nil { |
| 182 |
t.Fatal(err) |
| 183 |
} |
| 184 |
|
| 185 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 186 |
if err != nil { |
| 187 |
t.Fatal(err) |
| 188 |
} |
| 189 |
|
| 190 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 191 |
if err != nil { |
| 192 |
t.Fatal(err) |
| 193 |
} |
| 194 |
|
| 195 |
// Match all should return all documents |
| 196 |
suggesterName := "my-suggestions" |
| 197 |
cs := NewCompletionSuggester(suggesterName) |
| 198 |
cs = cs.Text("Golang") |
| 199 |
cs = cs.Field("suggest_field") |
| 200 |
|
| 201 |
searchResult, err := client.Search(). |
| 202 |
Index(testIndexName). |
| 203 |
Query(NewMatchAllQuery()). |
| 204 |
Suggester(cs). |
| 205 |
Do(context.TODO()) |
| 206 |
if err != nil { |
| 207 |
t.Fatal(err) |
| 208 |
} |
| 209 |
if searchResult.Suggest == nil { |
| 210 |
t.Errorf("expected SearchResult.Suggest != nil; got nil") |
| 211 |
} |
| 212 |
mySuggestions, found := searchResult.Suggest[suggesterName] |
| 213 |
if !found { |
| 214 |
t.Errorf("expected to find SearchResult.Suggest[%s]; got false", suggesterName) |
| 215 |
} |
| 216 |
if mySuggestions == nil { |
| 217 |
t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", suggesterName) |
| 218 |
} |
| 219 |
|
| 220 |
if len(mySuggestions) != 1 { |
| 221 |
t.Errorf("expected 1 suggestion; got %d", len(mySuggestions)) |
| 222 |
} |
| 223 |
mySuggestion := mySuggestions[0] |
| 224 |
if mySuggestion.Text != "Golang" { |
| 225 |
t.Errorf("expected Text = 'Golang'; got %s", mySuggestion.Text) |
| 226 |
} |
| 227 |
if mySuggestion.Offset != 0 { |
| 228 |
t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset) |
| 229 |
} |
| 230 |
if mySuggestion.Length != 6 { |
| 231 |
t.Errorf("expected Length = %d; got %d", 7, mySuggestion.Length) |
| 232 |
} |
| 233 |
if len(mySuggestion.Options) != 1 { |
| 234 |
t.Errorf("expected 1 option; got %d", len(mySuggestion.Options)) |
| 235 |
} |
| 236 |
myOption := mySuggestion.Options[0] |
| 237 |
if myOption.Text != "Golang" { |
| 238 |
t.Errorf("expected Text = 'Golang'; got %s", myOption.Text) |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
func TestContextSuggester(t *testing.T) { |
| 243 |
client := setupTestClientAndCreateIndex(t) // , SetTraceLog(log.New(os.Stdout, "", 0))) |
| 244 |
|
| 245 |
// TODO make a nice way of creating tweets, as currently the context fields are unsupported as part of the suggestion fields |
| 246 |
tweet1 := ` |
| 247 |
{ |
| 248 |
"user":"olivere", |
| 249 |
"message":"Welcome to Golang and Elasticsearch.", |
| 250 |
"retweets":0, |
| 251 |
"created":"0001-01-01T00:00:00Z", |
| 252 |
"suggest_field":{ |
| 253 |
"input":[ |
| 254 |
"Golang", |
| 255 |
"Elasticsearch" |
| 256 |
], |
| 257 |
"contexts":{ |
| 258 |
"user_name": ["olivere"] |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
` |
| 263 |
tweet2 := ` |
| 264 |
{ |
| 265 |
"user":"sandrae", |
| 266 |
"message":"I like golfing", |
| 267 |
"retweets":0, |
| 268 |
"created":"0001-01-01T00:00:00Z", |
| 269 |
"suggest_field":{ |
| 270 |
"input":[ |
| 271 |
"Golfing" |
| 272 |
], |
| 273 |
"contexts":{ |
| 274 |
"user_name": ["sandrae"] |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
` |
| 279 |
|
| 280 |
// Add all documents |
| 281 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyString(tweet1).Do(context.TODO()) |
| 282 |
if err != nil { |
| 283 |
t.Fatal(err) |
| 284 |
} |
| 285 |
|
| 286 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyString(tweet2).Do(context.TODO()) |
| 287 |
if err != nil { |
| 288 |
t.Fatal(err) |
| 289 |
} |
| 290 |
|
| 291 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 292 |
if err != nil { |
| 293 |
t.Fatal(err) |
| 294 |
} |
| 295 |
|
| 296 |
suggesterName := "my-suggestions" |
| 297 |
cs := NewContextSuggester(suggesterName) |
| 298 |
cs = cs.Prefix("Gol") |
| 299 |
cs = cs.Field("suggest_field") |
| 300 |
cs = cs.ContextQueries( |
| 301 |
NewSuggesterCategoryQuery("user_name", "olivere"), |
| 302 |
) |
| 303 |
|
| 304 |
searchResult, err := client.Search(). |
| 305 |
Index(testIndexName). |
| 306 |
Suggester(cs). |
| 307 |
Do(context.TODO()) |
| 308 |
if err != nil { |
| 309 |
t.Fatal(err) |
| 310 |
} |
| 311 |
if searchResult.Suggest == nil { |
| 312 |
t.Errorf("expected SearchResult.Suggest != nil; got nil") |
| 313 |
} |
| 314 |
mySuggestions, found := searchResult.Suggest[suggesterName] |
| 315 |
if !found { |
| 316 |
t.Errorf("expected to find SearchResult.Suggest[%s]; got false", suggesterName) |
| 317 |
} |
| 318 |
if mySuggestions == nil { |
| 319 |
t.Errorf("expected SearchResult.Suggest[%s] != nil; got nil", suggesterName) |
| 320 |
} |
| 321 |
|
| 322 |
// sandra's tweet is not returned because of the user_name context |
| 323 |
if len(mySuggestions) != 1 { |
| 324 |
t.Errorf("expected 1 suggestion; got %d", len(mySuggestions)) |
| 325 |
} |
| 326 |
mySuggestion := mySuggestions[0] |
| 327 |
if mySuggestion.Text != "Gol" { |
| 328 |
t.Errorf("expected Text = 'Gol'; got %s", mySuggestion.Text) |
| 329 |
} |
| 330 |
if mySuggestion.Offset != 0 { |
| 331 |
t.Errorf("expected Offset = %d; got %d", 0, mySuggestion.Offset) |
| 332 |
} |
| 333 |
if mySuggestion.Length != 3 { |
| 334 |
t.Errorf("expected Length = %d; got %d", 3, mySuggestion.Length) |
| 335 |
} |
| 336 |
if len(mySuggestion.Options) != 1 { |
| 337 |
t.Errorf("expected 1 option; got %d", len(mySuggestion.Options)) |
| 338 |
} |
| 339 |
myOption := mySuggestion.Options[0] |
| 340 |
if myOption.Text != "Golang" { |
| 341 |
t.Errorf("expected Text = 'Golang'; got %s", myOption.Text) |
| 342 |
} |
| 343 |
if myOption.Id != "1" { |
| 344 |
t.Errorf("expected Id = '1'; got %s", myOption.Id) |
| 345 |
} |
| 346 |
} |