| 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 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestGet(t *testing.T) { |
| 14 |
client := setupTestClientAndCreateIndex(t) |
| 15 |
|
| 16 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 17 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 18 |
if err != nil { |
| 19 |
t.Fatal(err) |
| 20 |
} |
| 21 |
|
| 22 |
// Get document 1 |
| 23 |
res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").Do(context.TODO()) |
| 24 |
if err != nil { |
| 25 |
t.Fatal(err) |
| 26 |
} |
| 27 |
if res.Found != true { |
| 28 |
t.Errorf("expected Found = true; got %v", res.Found) |
| 29 |
} |
| 30 |
if res.Source == nil { |
| 31 |
t.Errorf("expected Source != nil; got %v", res.Source) |
| 32 |
} |
| 33 |
|
| 34 |
// Get non existent document 99 |
| 35 |
res, err = client.Get().Index(testIndexName).Type("tweet").Id("99").Do(context.TODO()) |
| 36 |
if err == nil { |
| 37 |
t.Fatalf("expected error; got: %v", err) |
| 38 |
} |
| 39 |
if !IsNotFound(err) { |
| 40 |
t.Errorf("expected NotFound error; got: %v", err) |
| 41 |
} |
| 42 |
if res != nil { |
| 43 |
t.Errorf("expected no response; got: %v", res) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
func TestGetWithSourceFiltering(t *testing.T) { |
| 48 |
client := setupTestClientAndCreateIndex(t) // , SetTraceLog(log.New(os.Stdout, "", 0))) |
| 49 |
|
| 50 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 51 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 52 |
if err != nil { |
| 53 |
t.Fatal(err) |
| 54 |
} |
| 55 |
|
| 56 |
// Get document 1, without source |
| 57 |
res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").FetchSource(false).Do(context.TODO()) |
| 58 |
if err != nil { |
| 59 |
t.Fatal(err) |
| 60 |
} |
| 61 |
if res.Found != true { |
| 62 |
t.Errorf("expected Found = true; got %v", res.Found) |
| 63 |
} |
| 64 |
if res.Source != nil { |
| 65 |
t.Errorf("expected Source == nil; got %v", res.Source) |
| 66 |
} |
| 67 |
|
| 68 |
// Get document 1, exclude Message field |
| 69 |
fsc := NewFetchSourceContext(true).Exclude("message") |
| 70 |
res, err = client.Get().Index(testIndexName).Type("tweet").Id("1").FetchSourceContext(fsc).Do(context.TODO()) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
if res.Found != true { |
| 75 |
t.Errorf("expected Found = true; got %v", res.Found) |
| 76 |
} |
| 77 |
if res.Source == nil { |
| 78 |
t.Errorf("expected Source != nil; got %v", res.Source) |
| 79 |
} |
| 80 |
var tw tweet |
| 81 |
err = json.Unmarshal(*res.Source, &tw) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
if tw.User != "olivere" { |
| 86 |
t.Errorf("expected user %q; got: %q", "olivere", tw.User) |
| 87 |
} |
| 88 |
if tw.Message != "" { |
| 89 |
t.Errorf("expected message %q; got: %q", "", tw.Message) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func TestGetWithFields(t *testing.T) { |
| 94 |
client := setupTestClientAndCreateIndex(t) //, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 95 |
|
| 96 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 97 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 98 |
if err != nil { |
| 99 |
t.Fatal(err) |
| 100 |
} |
| 101 |
|
| 102 |
// Get document 1, specifying fields |
| 103 |
res, err := client.Get().Index(testIndexName).Type("tweet").Id("1").StoredFields("message").Do(context.TODO()) |
| 104 |
if err != nil { |
| 105 |
t.Fatal(err) |
| 106 |
} |
| 107 |
if res.Found != true { |
| 108 |
t.Errorf("expected Found = true; got: %v", res.Found) |
| 109 |
} |
| 110 |
|
| 111 |
// We must NOT have the "user" field |
| 112 |
_, ok := res.Fields["user"] |
| 113 |
if ok { |
| 114 |
t.Fatalf("expected no field %q in document", "user") |
| 115 |
} |
| 116 |
|
| 117 |
// We must have the "message" field |
| 118 |
messageField, ok := res.Fields["message"] |
| 119 |
if !ok { |
| 120 |
t.Fatalf("expected field %q in document", "message") |
| 121 |
} |
| 122 |
|
| 123 |
// Depending on the version of elasticsearch the message field will be returned |
| 124 |
// as a string or a slice of strings. This test works in both cases. |
| 125 |
|
| 126 |
messageString, ok := messageField.(string) |
| 127 |
if !ok { |
| 128 |
messageArray, ok := messageField.([]interface{}) |
| 129 |
if !ok { |
| 130 |
t.Fatalf("expected field %q to be a string or a slice of strings; got: %T", "message", messageField) |
| 131 |
} else { |
| 132 |
messageString, ok = messageArray[0].(string) |
| 133 |
if !ok { |
| 134 |
t.Fatalf("expected field %q to be a string or a slice of strings; got: %T", "message", messageField) |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if messageString != tweet1.Message { |
| 140 |
t.Errorf("expected message %q; got: %q", tweet1.Message, messageString) |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
func TestGetValidate(t *testing.T) { |
| 145 |
// Mitigate against http://stackoverflow.com/questions/27491738/elasticsearch-go-index-failures-no-feature-for-name |
| 146 |
client := setupTestClientAndCreateIndex(t) |
| 147 |
|
| 148 |
if _, err := client.Get().Do(context.TODO()); err == nil { |
| 149 |
t.Fatal("expected Get to fail") |
| 150 |
} |
| 151 |
if _, err := client.Get().Index(testIndexName).Do(context.TODO()); err == nil { |
| 152 |
t.Fatal("expected Get to fail") |
| 153 |
} |
| 154 |
if _, err := client.Get().Type("tweet").Do(context.TODO()); err == nil { |
| 155 |
t.Fatal("expected Get to fail") |
| 156 |
} |
| 157 |
if _, err := client.Get().Id("1").Do(context.TODO()); err == nil { |
| 158 |
t.Fatal("expected Get to fail") |
| 159 |
} |
| 160 |
if _, err := client.Get().Index(testIndexName).Type("tweet").Do(context.TODO()); err == nil { |
| 161 |
t.Fatal("expected Get to fail") |
| 162 |
} |
| 163 |
if _, err := client.Get().Type("tweet").Id("1").Do(context.TODO()); err == nil { |
| 164 |
t.Fatal("expected Get to fail") |
| 165 |
} |
| 166 |
} |