| 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 TestIndicesSegments(t *testing.T) { |
| 13 |
client := setupTestClientAndCreateIndex(t) |
| 14 |
|
| 15 |
tests := []struct { |
| 16 |
Indices []string |
| 17 |
Expected string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
[]string{}, |
| 21 |
"/_segments", |
| 22 |
}, |
| 23 |
{ |
| 24 |
[]string{"index1"}, |
| 25 |
"/index1/_segments", |
| 26 |
}, |
| 27 |
{ |
| 28 |
[]string{"index1", "index2"}, |
| 29 |
"/index1%2Cindex2/_segments", |
| 30 |
}, |
| 31 |
} |
| 32 |
|
| 33 |
for i, test := range tests { |
| 34 |
path, _, err := client.IndexSegments().Index(test.Indices...).buildURL() |
| 35 |
if err != nil { |
| 36 |
t.Errorf("case #%d: %v", i+1, err) |
| 37 |
} |
| 38 |
if path != test.Expected { |
| 39 |
t.Errorf("case #%d: expected %q; got: %q", i+1, test.Expected, path) |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestIndexSegments(t *testing.T) { |
| 45 |
client := setupTestClientAndCreateIndexAndAddDocs(t) |
| 46 |
//client := setupTestClientAndCreateIndexAndAddDocs(t, SetTraceLog(log.New(os.Stdout, "", 0))) |
| 47 |
|
| 48 |
segments, err := client.IndexSegments(testIndexName).Pretty(true).Human(true).Do(context.TODO()) |
| 49 |
if err != nil { |
| 50 |
t.Fatalf("expected no error; got: %v", err) |
| 51 |
} |
| 52 |
if segments == nil { |
| 53 |
t.Fatalf("expected response; got: %v", segments) |
| 54 |
} |
| 55 |
indices, found := segments.Indices[testIndexName] |
| 56 |
if !found { |
| 57 |
t.Fatalf("expected index information about index %v; got: %v", testIndexName, found) |
| 58 |
} |
| 59 |
shards, found := indices.Shards["0"] |
| 60 |
if !found { |
| 61 |
t.Fatalf("expected shard information about index %v", testIndexName) |
| 62 |
} |
| 63 |
if shards == nil { |
| 64 |
t.Fatalf("expected shard information to be != nil for index %v", testIndexName) |
| 65 |
} |
| 66 |
shard := shards[0] |
| 67 |
if shard == nil { |
| 68 |
t.Fatalf("expected shard information to be != nil for shard 0 in index %v", testIndexName) |
| 69 |
} |
| 70 |
if shard.Routing == nil { |
| 71 |
t.Fatalf("expected shard routing information to be != nil for index %v", testIndexName) |
| 72 |
} |
| 73 |
segmentDetail, found := shard.Segments["_0"] |
| 74 |
if !found { |
| 75 |
t.Fatalf("expected segment detail to be != nil for index %v", testIndexName) |
| 76 |
} |
| 77 |
if segmentDetail == nil { |
| 78 |
t.Fatalf("expected segment detail to be != nil for index %v", testIndexName) |
| 79 |
} |
| 80 |
if segmentDetail.NumDocs == 0 { |
| 81 |
t.Fatal("expected segment to contain >= 1 docs") |
| 82 |
} |
| 83 |
} |