| 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 |
const ( |
| 14 |
testAliasName = "elastic-test-alias" |
| 15 |
) |
| 16 |
|
| 17 |
func TestAliasLifecycle(t *testing.T) { |
| 18 |
var err error |
| 19 |
|
| 20 |
client := setupTestClientAndCreateIndex(t) |
| 21 |
|
| 22 |
// Some tweets |
| 23 |
tweet1 := tweet{User: "olivere", Message: "Welcome to Golang and Elasticsearch."} |
| 24 |
tweet2 := tweet{User: "sandrae", Message: "Cycling is fun."} |
| 25 |
tweet3 := tweet{User: "olivere", Message: "Another unrelated topic."} |
| 26 |
|
| 27 |
// Add tweets to first index |
| 28 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("1").BodyJson(&tweet1).Do(context.TODO()) |
| 29 |
if err != nil { |
| 30 |
t.Fatal(err) |
| 31 |
} |
| 32 |
|
| 33 |
_, err = client.Index().Index(testIndexName).Type("tweet").Id("2").BodyJson(&tweet2).Do(context.TODO()) |
| 34 |
if err != nil { |
| 35 |
t.Fatal(err) |
| 36 |
} |
| 37 |
|
| 38 |
// Add tweets to second index |
| 39 |
_, err = client.Index().Index(testIndexName2).Type("tweet").Id("3").BodyJson(&tweet3).Do(context.TODO()) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
|
| 44 |
// Flush |
| 45 |
_, err = client.Flush().Index(testIndexName).Do(context.TODO()) |
| 46 |
if err != nil { |
| 47 |
t.Fatal(err) |
| 48 |
} |
| 49 |
_, err = client.Flush().Index(testIndexName2).Do(context.TODO()) |
| 50 |
if err != nil { |
| 51 |
t.Fatal(err) |
| 52 |
} |
| 53 |
|
| 54 |
// Add both indices to a new alias |
| 55 |
aliasCreate, err := client.Alias(). |
| 56 |
Add(testIndexName, testAliasName). |
| 57 |
Action(NewAliasAddAction(testAliasName).Index(testIndexName2)). |
| 58 |
//Pretty(true). |
| 59 |
Do(context.TODO()) |
| 60 |
if err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
if !aliasCreate.Acknowledged { |
| 64 |
t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasCreate.Acknowledged) |
| 65 |
} |
| 66 |
|
| 67 |
// Search should return all 3 tweets |
| 68 |
matchAll := NewMatchAllQuery() |
| 69 |
searchResult1, err := client.Search().Index(testAliasName).Query(matchAll).Do(context.TODO()) |
| 70 |
if err != nil { |
| 71 |
t.Fatal(err) |
| 72 |
} |
| 73 |
if searchResult1.Hits == nil { |
| 74 |
t.Errorf("expected SearchResult.Hits != nil; got nil") |
| 75 |
} |
| 76 |
if searchResult1.Hits.TotalHits != 3 { |
| 77 |
t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 3, searchResult1.Hits.TotalHits) |
| 78 |
} |
| 79 |
|
| 80 |
// Remove first index should remove two tweets, so should only yield 1 |
| 81 |
aliasRemove1, err := client.Alias(). |
| 82 |
Remove(testIndexName, testAliasName). |
| 83 |
//Pretty(true). |
| 84 |
Do(context.TODO()) |
| 85 |
if err != nil { |
| 86 |
t.Fatal(err) |
| 87 |
} |
| 88 |
if !aliasRemove1.Acknowledged { |
| 89 |
t.Errorf("expected AliasResult.Acknowledged %v; got %v", true, aliasRemove1.Acknowledged) |
| 90 |
} |
| 91 |
|
| 92 |
searchResult2, err := client.Search().Index(testAliasName).Query(matchAll).Do(context.TODO()) |
| 93 |
if err != nil { |
| 94 |
t.Fatal(err) |
| 95 |
} |
| 96 |
if searchResult2.Hits == nil { |
| 97 |
t.Errorf("expected SearchResult.Hits != nil; got nil") |
| 98 |
} |
| 99 |
if searchResult2.Hits.TotalHits != 1 { |
| 100 |
t.Errorf("expected SearchResult.Hits.TotalHits = %d; got %d", 1, searchResult2.Hits.TotalHits) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
func TestAliasAddAction(t *testing.T) { |
| 105 |
var tests = []struct { |
| 106 |
Action *AliasAddAction |
| 107 |
Expected string |
| 108 |
Invalid bool |
| 109 |
}{ |
| 110 |
{ |
| 111 |
Action: NewAliasAddAction("").Index(""), |
| 112 |
Invalid: true, |
| 113 |
}, |
| 114 |
{ |
| 115 |
Action: NewAliasAddAction("alias1").Index(""), |
| 116 |
Invalid: true, |
| 117 |
}, |
| 118 |
{ |
| 119 |
Action: NewAliasAddAction("").Index("index1"), |
| 120 |
Invalid: true, |
| 121 |
}, |
| 122 |
{ |
| 123 |
Action: NewAliasAddAction("alias1").Index("index1"), |
| 124 |
Expected: `{"add":{"alias":"alias1","index":"index1"}}`, |
| 125 |
}, |
| 126 |
{ |
| 127 |
Action: NewAliasAddAction("alias1").Index("index1", "index2"), |
| 128 |
Expected: `{"add":{"alias":"alias1","indices":["index1","index2"]}}`, |
| 129 |
}, |
| 130 |
{ |
| 131 |
Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1"), |
| 132 |
Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1"}}`, |
| 133 |
}, |
| 134 |
{ |
| 135 |
Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").IndexRouting("indexRouting1"), |
| 136 |
Expected: `{"add":{"alias":"alias1","index":"index1","index_routing":"indexRouting1","routing":"routing1"}}`, |
| 137 |
}, |
| 138 |
{ |
| 139 |
Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").SearchRouting("searchRouting1"), |
| 140 |
Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1","search_routing":"searchRouting1"}}`, |
| 141 |
}, |
| 142 |
{ |
| 143 |
Action: NewAliasAddAction("alias1").Index("index1").Routing("routing1").SearchRouting("searchRouting1", "searchRouting2"), |
| 144 |
Expected: `{"add":{"alias":"alias1","index":"index1","routing":"routing1","search_routing":"searchRouting1,searchRouting2"}}`, |
| 145 |
}, |
| 146 |
{ |
| 147 |
Action: NewAliasAddAction("alias1").Index("index1").Filter(NewTermQuery("user", "olivere")), |
| 148 |
Expected: `{"add":{"alias":"alias1","filter":{"term":{"user":"olivere"}},"index":"index1"}}`, |
| 149 |
}, |
| 150 |
} |
| 151 |
|
| 152 |
for i, tt := range tests { |
| 153 |
src, err := tt.Action.Source() |
| 154 |
if err != nil { |
| 155 |
if !tt.Invalid { |
| 156 |
t.Errorf("#%d: expected to succeed", i) |
| 157 |
} |
| 158 |
} else { |
| 159 |
if tt.Invalid { |
| 160 |
t.Errorf("#%d: expected to fail", i) |
| 161 |
} else { |
| 162 |
dst, err := json.Marshal(src) |
| 163 |
if err != nil { |
| 164 |
t.Fatal(err) |
| 165 |
} |
| 166 |
if want, have := tt.Expected, string(dst); want != have { |
| 167 |
t.Errorf("#%d: expected %s, got %s", i, want, have) |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
func TestAliasRemoveAction(t *testing.T) { |
| 175 |
var tests = []struct { |
| 176 |
Action *AliasRemoveAction |
| 177 |
Expected string |
| 178 |
Invalid bool |
| 179 |
}{ |
| 180 |
{ |
| 181 |
Action: NewAliasRemoveAction(""), |
| 182 |
Invalid: true, |
| 183 |
}, |
| 184 |
{ |
| 185 |
Action: NewAliasRemoveAction("alias1"), |
| 186 |
Invalid: true, |
| 187 |
}, |
| 188 |
{ |
| 189 |
Action: NewAliasRemoveAction("").Index("index1"), |
| 190 |
Invalid: true, |
| 191 |
}, |
| 192 |
{ |
| 193 |
Action: NewAliasRemoveAction("alias1").Index("index1"), |
| 194 |
Expected: `{"remove":{"alias":"alias1","index":"index1"}}`, |
| 195 |
}, |
| 196 |
{ |
| 197 |
Action: NewAliasRemoveAction("alias1").Index("index1", "index2"), |
| 198 |
Expected: `{"remove":{"alias":"alias1","indices":["index1","index2"]}}`, |
| 199 |
}, |
| 200 |
} |
| 201 |
|
| 202 |
for i, tt := range tests { |
| 203 |
src, err := tt.Action.Source() |
| 204 |
if err != nil { |
| 205 |
if !tt.Invalid { |
| 206 |
t.Errorf("#%d: expected to succeed", i) |
| 207 |
} |
| 208 |
} else { |
| 209 |
if tt.Invalid { |
| 210 |
t.Errorf("#%d: expected to fail", i) |
| 211 |
} else { |
| 212 |
dst, err := json.Marshal(src) |
| 213 |
if err != nil { |
| 214 |
t.Fatal(err) |
| 215 |
} |
| 216 |
if want, have := tt.Expected, string(dst); want != have { |
| 217 |
t.Errorf("#%d: expected %s, got %s", i, want, have) |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |