| 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 |
"encoding/json" |
| 10 |
_ "net/http" |
| 11 |
"testing" |
| 12 |
) |
| 13 |
|
| 14 |
func TestMultiSearch(t *testing.T) { |
| 15 |
client := setupTestClientAndCreateIndex(t) |
| 16 |
|
| 17 |
tweet1 := tweet{ |
| 18 |
User: "olivere", |
| 19 |
Message: "Welcome to Golang and Elasticsearch.", |
| 20 |
Tags: []string{"golang", "elasticsearch"}, |
| 21 |
} |
| 22 |
tweet2 := tweet{ |
| 23 |
User: "olivere", |
| 24 |
Message: "Another unrelated topic.", |
| 25 |
Tags: []string{"golang"}, |
| 26 |
} |
| 27 |
tweet3 := tweet{ |
| 28 |
User: "sandrae", |
| 29 |
Message: "Cycling is fun.", |
| 30 |
Tags: []string{"sports", "cycling"}, |
| 31 |
} |
| 32 |
|
| 33 |
// Add all documents |
| 34 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
|
| 44 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
|
| 49 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
|
| 54 |
// Spawn two search queries with one roundtrip |
| 55 |
q1 := NewMatchAllQuery() |
| 56 |
q2 := NewTermQuery("tags", "golang") |
| 57 |
|
| 58 |
sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2). |
| 59 |
Source(NewSearchSource().Query(q1).Size(10)) |
| 60 |
sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet"). |
| 61 |
Source(NewSearchSource().Query(q2)) |
| 62 |
|
| 63 |
searchResult, err := client.MultiSearch(). |
| 64 |
Add(sreq1, sreq2). |
| 65 |
Do(context.TODO()) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
if searchResult.Responses == nil { |
| 70 |
t.Fatal("expected responses != nil; got nil") |
| 71 |
} |
| 72 |
if len(searchResult.Responses) != 2 { |
| 73 |
t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses)) |
| 74 |
} |
| 75 |
|
| 76 |
sres := searchResult.Responses[0] |
| 77 |
if sres.Hits == nil { |
| 78 |
t.Errorf("expected Hits != nil; got nil") |
| 79 |
} |
| 80 |
if sres.Hits.TotalHits != 3 { |
| 81 |
t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits) |
| 82 |
} |
| 83 |
if len(sres.Hits.Hits) != 3 { |
| 84 |
t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits)) |
| 85 |
} |
| 86 |
for _, hit := range sres.Hits.Hits { |
| 87 |
if hit.Index != testIndexName { |
| 88 |
t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 89 |
} |
| 90 |
item := make(map[string]interface{}) |
| 91 |
err := json.Unmarshal(*hit.Source, &item) |
| 92 |
if err != nil { |
| 93 |
t.Fatal(err) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
sres = searchResult.Responses[1] |
| 98 |
if sres.Hits == nil { |
| 99 |
t.Errorf("expected Hits != nil; got nil") |
| 100 |
} |
| 101 |
if sres.Hits.TotalHits != 2 { |
| 102 |
t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits) |
| 103 |
} |
| 104 |
if len(sres.Hits.Hits) != 2 { |
| 105 |
t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits)) |
| 106 |
} |
| 107 |
for _, hit := range sres.Hits.Hits { |
| 108 |
if hit.Index != testIndexName { |
| 109 |
t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 110 |
} |
| 111 |
item := make(map[string]interface{}) |
| 112 |
err := json.Unmarshal(*hit.Source, &item) |
| 113 |
if err != nil { |
| 114 |
t.Fatal(err) |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestMultiSearchWithStrings(t *testing.T) { |
| 120 |
client := setupTestClientAndCreateIndex(t) |
| 121 |
// client := setupTestClientAndCreateIndexAndLog(t) |
| 122 |
|
| 123 |
tweet1 := tweet{ |
| 124 |
User: "olivere", |
| 125 |
Message: "Welcome to Golang and Elasticsearch.", |
| 126 |
Tags: []string{"golang", "elasticsearch"}, |
| 127 |
} |
| 128 |
tweet2 := tweet{ |
| 129 |
User: "olivere", |
| 130 |
Message: "Another unrelated topic.", |
| 131 |
Tags: []string{"golang"}, |
| 132 |
} |
| 133 |
tweet3 := tweet{ |
| 134 |
User: "sandrae", |
| 135 |
Message: "Cycling is fun.", |
| 136 |
Tags: []string{"sports", "cycling"}, |
| 137 |
} |
| 138 |
|
| 139 |
// Add all documents |
| 140 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 141 |
if err != nil { |
| 142 |
t.Fatal(err) |
| 143 |
} |
| 144 |
|
| 145 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 146 |
if err != nil { |
| 147 |
t.Fatal(err) |
| 148 |
} |
| 149 |
|
| 150 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 151 |
if err != nil { |
| 152 |
t.Fatal(err) |
| 153 |
} |
| 154 |
|
| 155 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 156 |
if err != nil { |
| 157 |
t.Fatal(err) |
| 158 |
} |
| 159 |
|
| 160 |
// Spawn two search queries with one roundtrip |
| 161 |
sreq1 := NewSearchRequest().Index(testIndexName, testIndexName2). |
| 162 |
Source(`{"query":{"match_all":{}}}`) |
| 163 |
sreq2 := NewSearchRequest().Index(testIndexName).Type("tweet"). |
| 164 |
Source(`{"query":{"term":{"tags":"golang"}}}`) |
| 165 |
|
| 166 |
searchResult, err := client.MultiSearch(). |
| 167 |
Add(sreq1, sreq2). |
| 168 |
Do(context.TODO()) |
| 169 |
if err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
if searchResult.Responses == nil { |
| 173 |
t.Fatal("expected responses != nil; got nil") |
| 174 |
} |
| 175 |
if len(searchResult.Responses) != 2 { |
| 176 |
t.Fatalf("expected 2 responses; got %d", len(searchResult.Responses)) |
| 177 |
} |
| 178 |
|
| 179 |
sres := searchResult.Responses[0] |
| 180 |
if sres.Hits == nil { |
| 181 |
t.Errorf("expected Hits != nil; got nil") |
| 182 |
} |
| 183 |
if sres.Hits.TotalHits != 3 { |
| 184 |
t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits) |
| 185 |
} |
| 186 |
if len(sres.Hits.Hits) != 3 { |
| 187 |
t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits)) |
| 188 |
} |
| 189 |
for _, hit := range sres.Hits.Hits { |
| 190 |
if hit.Index != testIndexName { |
| 191 |
t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 192 |
} |
| 193 |
item := make(map[string]interface{}) |
| 194 |
err := json.Unmarshal(*hit.Source, &item) |
| 195 |
if err != nil { |
| 196 |
t.Fatal(err) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
sres = searchResult.Responses[1] |
| 201 |
if sres.Hits == nil { |
| 202 |
t.Errorf("expected Hits != nil; got nil") |
| 203 |
} |
| 204 |
if sres.Hits.TotalHits != 2 { |
| 205 |
t.Errorf("expected Hits.TotalHits = %d; got %d", 2, sres.Hits.TotalHits) |
| 206 |
} |
| 207 |
if len(sres.Hits.Hits) != 2 { |
| 208 |
t.Errorf("expected len(Hits.Hits) = %d; got %d", 2, len(sres.Hits.Hits)) |
| 209 |
} |
| 210 |
for _, hit := range sres.Hits.Hits { |
| 211 |
if hit.Index != testIndexName { |
| 212 |
t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 213 |
} |
| 214 |
item := make(map[string]interface{}) |
| 215 |
err := json.Unmarshal(*hit.Source, &item) |
| 216 |
if err != nil { |
| 217 |
t.Fatal(err) |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
func TestMultiSearchWithOneRequest(t *testing.T) { |
| 223 |
client := setupTestClientAndCreateIndex(t) |
| 224 |
|
| 225 |
tweet1 := tweet{ |
| 226 |
User: "olivere", |
| 227 |
Message: "Welcome to Golang and Elasticsearch.", |
| 228 |
Tags: []string{"golang", "elasticsearch"}, |
| 229 |
} |
| 230 |
tweet2 := tweet{ |
| 231 |
User: "olivere", |
| 232 |
Message: "Another unrelated topic.", |
| 233 |
Tags: []string{"golang"}, |
| 234 |
} |
| 235 |
tweet3 := tweet{ |
| 236 |
User: "sandrae", |
| 237 |
Message: "Cycling is fun.", |
| 238 |
Tags: []string{"sports", "cycling"}, |
| 239 |
} |
| 240 |
|
| 241 |
// Add all documents |
| 242 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 243 |
if err != nil { |
| 244 |
t.Fatal(err) |
| 245 |
} |
| 246 |
|
| 247 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
|
| 252 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 253 |
if err != nil { |
| 254 |
t.Fatal(err) |
| 255 |
} |
| 256 |
|
| 257 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 258 |
if err != nil { |
| 259 |
t.Fatal(err) |
| 260 |
} |
| 261 |
|
| 262 |
// Spawn two search queries with one roundtrip |
| 263 |
query := NewMatchAllQuery() |
| 264 |
source := NewSearchSource().Query(query).Size(10) |
| 265 |
sreq := NewSearchRequest().Source(source) |
| 266 |
|
| 267 |
searchResult, err := client.MultiSearch(). |
| 268 |
Index(testIndexName). |
| 269 |
Add(sreq). |
| 270 |
Do(context.TODO()) |
| 271 |
if err != nil { |
| 272 |
t.Fatal(err) |
| 273 |
} |
| 274 |
if searchResult.Responses == nil { |
| 275 |
t.Fatal("expected responses != nil; got nil") |
| 276 |
} |
| 277 |
if len(searchResult.Responses) != 1 { |
| 278 |
t.Fatalf("expected 1 responses; got %d", len(searchResult.Responses)) |
| 279 |
} |
| 280 |
|
| 281 |
sres := searchResult.Responses[0] |
| 282 |
if sres.Hits == nil { |
| 283 |
t.Errorf("expected Hits != nil; got nil") |
| 284 |
} |
| 285 |
if sres.Hits.TotalHits != 3 { |
| 286 |
t.Errorf("expected Hits.TotalHits = %d; got %d", 3, sres.Hits.TotalHits) |
| 287 |
} |
| 288 |
if len(sres.Hits.Hits) != 3 { |
| 289 |
t.Errorf("expected len(Hits.Hits) = %d; got %d", 3, len(sres.Hits.Hits)) |
| 290 |
} |
| 291 |
for _, hit := range sres.Hits.Hits { |
| 292 |
if hit.Index != testIndexName { |
| 293 |
t.Errorf("expected Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 294 |
} |
| 295 |
item := make(map[string]interface{}) |
| 296 |
err := json.Unmarshal(*hit.Source, &item) |
| 297 |
if err != nil { |
| 298 |
t.Fatal(err) |
| 299 |
} |
| 300 |
} |
| 301 |
} |