| 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 TestPercolate(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 14 |
|
| 15 |
// Add a document |
| 16 |
_, err := client.Index(). |
| 17 |
Index(testIndexName). |
| 18 |
Type("queries"). |
| 19 |
Id("1"). |
| 20 |
BodyJson(`{"query":{"match":{"message":"bonsai tree"}}}`). |
| 21 |
Refresh("wait_for"). |
| 22 |
Do(context.TODO()) |
| 23 |
if err != nil { |
| 24 |
t.Fatal(err) |
| 25 |
} |
| 26 |
|
| 27 |
// Percolate should return our registered query |
| 28 |
pq := NewPercolatorQuery(). |
| 29 |
Field("query"). |
| 30 |
DocumentType("doctype"). |
| 31 |
Document(doctype{Message: "A new bonsai tree in the office"}) |
| 32 |
res, err := client.Search(testIndexName).Query(pq).Do(context.TODO()) |
| 33 |
if err != nil { |
| 34 |
t.Fatal(err) |
| 35 |
} |
| 36 |
if res == nil { |
| 37 |
t.Fatal("expected results != nil; got nil") |
| 38 |
} |
| 39 |
if res.Hits == nil { |
| 40 |
t.Fatal("expected SearchResult.Hits != nil; got nil") |
| 41 |
} |
| 42 |
if got, want := res.Hits.TotalHits, int64(1); got != want { |
| 43 |
t.Fatalf("expected SearchResult.Hits.TotalHits = %d; got %d", want, got) |
| 44 |
} |
| 45 |
if got, want := len(res.Hits.Hits), 1; got != want { |
| 46 |
t.Fatalf("expected len(SearchResult.Hits.Hits) = %d; got %d", want, got) |
| 47 |
} |
| 48 |
hit := res.Hits.Hits[0] |
| 49 |
if hit.Index != testIndexName { |
| 50 |
t.Fatalf("expected SearchResult.Hits.Hit.Index = %q; got %q", testIndexName, hit.Index) |
| 51 |
} |
| 52 |
got := string(*hit.Source) |
| 53 |
expected := `{"query":{"match":{"message":"bonsai tree"}}}` |
| 54 |
if got != expected { |
| 55 |
t.Fatalf("expected\n%s\n,got:\n%s", expected, got) |
| 56 |
} |
| 57 |
} |