-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathxpack_async_search_test.go
94 lines (88 loc) · 2.87 KB
/
xpack_async_search_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"encoding/json"
"testing"
)
func TestXPackAsyncSearchLifecycle(t *testing.T) {
//client := setupTestClientAndCreateIndexAndAddDocs(t, SetURL("http://elastic:elastic@localhost:9210"), SetTraceLog(log.New(os.Stdout, "", log.LstdFlags)))
client := setupTestClientAndCreateIndexAndAddDocs(t, SetURL("http://elastic:elastic@localhost:9210"))
// Match all should return all documents
resp, err := client.XPackAsyncSearchSubmit().
Index(testIndexName).
Query(NewMatchAllQuery()).
Size(100).
Pretty(true).
WaitForCompletionTimeout("10s"). // should be ready by then
KeepOnCompletion(true). // keep even after completion
KeepAlive("2m"). // keep for at least 2 minutes
Do(context.Background())
if err != nil {
t.Fatal(err)
}
if resp == nil {
t.Fatal("expected response, got nil")
}
if want, have := false, resp.IsRunning; want != have {
t.Errorf("expected IsRunning=%v; got %v", want, have)
}
if want, have := false, resp.IsPartial; want != have {
t.Errorf("expected IsPartial=%v; got %v", want, have)
}
if resp.ID == "" {
t.Error(`expected ID!=""`)
}
if resp.Response == nil {
t.Fatal("expected Response; got nil")
}
if want, have := int64(3), resp.Response.TotalHits(); want != have {
t.Errorf("expected TotalHits=%v; got %v", want, have)
}
for _, hit := range resp.Response.Hits.Hits {
if hit.Index != testIndexName {
t.Errorf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index)
}
item := make(map[string]interface{})
err := json.Unmarshal(hit.Source, &item)
if err != nil {
t.Fatal(err)
}
}
// Get the search results with the given ID
get, err := client.XPackAsyncSearchGet().ID(resp.ID).Pretty(true).Do(context.Background())
if err != nil {
t.Fatal(err)
}
if get == nil {
t.Fatal("expected response, got nil")
}
if want, have := false, get.IsRunning; want != have {
t.Errorf("expected IsRunning=%v; got %v", want, have)
}
if want, have := false, get.IsPartial; want != have {
t.Errorf("expected IsPartial=%v; got %v", want, have)
}
if want, have := get.ID, get.ID; want != have {
t.Errorf("expected ID!=%q; got %q", want, have)
}
if get.Response == nil {
t.Fatal("expected Response; got nil")
}
if want, have := int64(3), get.Response.TotalHits(); want != have {
t.Errorf("expected TotalHits=%v; got %v", want, have)
}
// Delete the search results with the given ID
del, err := client.XPackAsyncSearchDelete().ID(resp.ID).Do(context.Background())
if err != nil {
t.Fatal(err)
}
if del == nil {
t.Fatal("expected response, got nil")
}
if want, have := true, del.Acknowledged; want != have {
t.Errorf("expected Acknowledged=%v; got %v", want, have)
}
}