| 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 |
_ "net/http" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestClearScroll(t *testing.T) { |
| 14 |
client := setupTestClientAndCreateIndex(t) |
| 15 |
// client := setupTestClientAndCreateIndex(t, SetTraceLog(log.New(os.Stdout, "", log.LstdFlags))) |
| 16 |
|
| 17 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 18 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 19 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 20 |
|
| 21 |
// Add all documents |
| 22 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 23 |
if err != nil { |
| 24 |
t.Fatal(err) |
| 25 |
} |
| 26 |
|
| 27 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 28 |
if err != nil { |
| 29 |
t.Fatal(err) |
| 30 |
} |
| 31 |
|
| 32 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 33 |
if err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
|
| 37 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 38 |
if err != nil { |
| 39 |
t.Fatal(err) |
| 40 |
} |
| 41 |
|
| 42 |
// Match all should return all documents |
| 43 |
res, err := client.Scroll(testIndexName).Size(1).Do(context.TODO()) |
| 44 |
if err != nil { |
| 45 |
t.Fatal(err) |
| 46 |
} |
| 47 |
if res == nil { |
| 48 |
t.Fatal("expected results != nil; got nil") |
| 49 |
} |
| 50 |
if res.ScrollId == "" { |
| 51 |
t.Fatalf("expected scrollId in results; got %q", res.ScrollId) |
| 52 |
} |
| 53 |
|
| 54 |
// Search should succeed |
| 55 |
_, err = client.Scroll(testIndexName).Size(1).ScrollId(res.ScrollId).Do(context.TODO()) |
| 56 |
if err != nil { |
| 57 |
t.Fatal(err) |
| 58 |
} |
| 59 |
|
| 60 |
// Clear scroll id |
| 61 |
clearScrollRes, err := client.ClearScroll().ScrollId(res.ScrollId).Do(context.TODO()) |
| 62 |
if err != nil { |
| 63 |
t.Fatal(err) |
| 64 |
} |
| 65 |
if clearScrollRes == nil { |
| 66 |
t.Fatal("expected results != nil; got nil") |
| 67 |
} |
| 68 |
|
| 69 |
// Search result should fail |
| 70 |
_, err = client.Scroll(testIndexName).Size(1).ScrollId(res.ScrollId).Do(context.TODO()) |
| 71 |
if err == nil { |
| 72 |
t.Fatalf("expected scroll to fail") |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
func TestClearScrollValidate(t *testing.T) { |
| 77 |
client := setupTestClient(t) |
| 78 |
|
| 79 |
// No scroll id -> fail with error |
| 80 |
res, err := NewClearScrollService(client).Do(context.TODO()) |
| 81 |
if err == nil { |
| 82 |
t.Fatalf("expected ClearScroll to fail without scroll ids") |
| 83 |
} |
| 84 |
if res != nil { |
| 85 |
t.Fatalf("expected result to be nil; got: %v", res) |
| 86 |
} |
| 87 |
} |