| 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 TestMultiTermVectorsValidateAndBuildURL(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Index string |
| 17 |
Type string |
| 18 |
Expected string |
| 19 |
ExpectValidateFailure bool |
| 20 |
}{ |
| 21 |
// #0: No index, no type |
| 22 |
{ |
| 23 |
"", |
| 24 |
"", |
| 25 |
"/_mtermvectors", |
| 26 |
false, |
| 27 |
}, |
| 28 |
// #1: Index only |
| 29 |
{ |
| 30 |
"twitter", |
| 31 |
"", |
| 32 |
"/twitter/_mtermvectors", |
| 33 |
false, |
| 34 |
}, |
| 35 |
// #2: Type without index |
| 36 |
{ |
| 37 |
"", |
| 38 |
"tweet", |
| 39 |
"", |
| 40 |
true, |
| 41 |
}, |
| 42 |
// #3: Both index and type |
| 43 |
{ |
| 44 |
"twitter", |
| 45 |
"tweet", |
| 46 |
"/twitter/tweet/_mtermvectors", |
| 47 |
false, |
| 48 |
}, |
| 49 |
} |
| 50 |
|
| 51 |
for i, test := range tests { |
| 52 |
builder := client.MultiTermVectors().Index(test.Index).Type(test.Type) |
| 53 |
// Validate |
| 54 |
err := builder.Validate() |
| 55 |
if err != nil { |
| 56 |
if !test.ExpectValidateFailure { |
| 57 |
t.Errorf("#%d: expected no error, got: %v", i, err) |
| 58 |
continue |
| 59 |
} |
| 60 |
} else { |
| 61 |
if test.ExpectValidateFailure { |
| 62 |
t.Errorf("#%d: expected error, got: nil", i) |
| 63 |
continue |
| 64 |
} |
| 65 |
// Build |
| 66 |
path, _, err := builder.buildURL() |
| 67 |
if err != nil { |
| 68 |
t.Errorf("#%d: expected no error, got: %v", i, err) |
| 69 |
continue |
| 70 |
} |
| 71 |
if path != test.Expected { |
| 72 |
t.Errorf("#%d: expected %q; got: %q", i, test.Expected, path) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestMultiTermVectorsWithIds(t *testing.T) { |
| 79 |
client := setupTestClientAndCreateIndex(t) |
| 80 |
|
| 81 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 82 |
tweet2 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 83 |
tweet3 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 84 |
|
| 85 |
_, err := client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 86 |
if err != nil { |
| 87 |
t.Fatal(err) |
| 88 |
} |
| 89 |
|
| 90 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 91 |
if err != nil { |
| 92 |
t.Fatal(err) |
| 93 |
} |
| 94 |
|
| 95 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 96 |
if err != nil { |
| 97 |
t.Fatal(err) |
| 98 |
} |
| 99 |
|
| 100 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 101 |
if err != nil { |
| 102 |
t.Fatal(err) |
| 103 |
} |
| 104 |
|
| 105 |
// Count documents |
| 106 |
count, err := client.Count(testIndexName).Do(context.TODO()) |
| 107 |
if err != nil { |
| 108 |
t.Fatal(err) |
| 109 |
} |
| 110 |
if count != 3 { |
| 111 |
t.Errorf("expected Count = %d; got %d", 3, count) |
| 112 |
} |
| 113 |
|
| 114 |
// MultiTermVectors by specifying ID by 1 and 3 |
| 115 |
field := "Message" |
| 116 |
res, err := client.MultiTermVectors(). |
| 117 |
Index(testIndexName). |
| 118 |
Type("tweet"). |
| 119 |
Add(NewMultiTermvectorItem().Index(testIndexName).Type("tweet").Id("1").Fields(field)). |
| 120 |
Add(NewMultiTermvectorItem().Index(testIndexName).Type("tweet").Id("3").Fields(field)). |
| 121 |
Do(context.TODO()) |
| 122 |
if err != nil { |
| 123 |
t.Fatal(err) |
| 124 |
} |
| 125 |
if res == nil { |
| 126 |
t.Fatal("expected to return information and statistics") |
| 127 |
} |
| 128 |
if res.Docs == nil { |
| 129 |
t.Fatal("expected result docs to be != nil; got nil") |
| 130 |
} |
| 131 |
if len(res.Docs) != 2 { |
| 132 |
t.Fatalf("expected to have 2 docs; got %d", len(res.Docs)) |
| 133 |
} |
| 134 |
} |