forked from olivere/es
/
indices.go
66 lines (56 loc) · 1.05 KB
/
indices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"regexp"
"sort"
"github.com/olivere/elastic"
)
var cmdIndices = &Command{
Run: runIndices,
Usage: "indices [<pattern>]",
Short: "list all indices",
Long: `
Prints a list of all indices matching the specified pattern.
Example:
$ es indices
master
marvel
$ es indices 'mas.*'
master
`,
ApiUrl: "http://www.elasticsearch.org/guide/reference/api/index_.html",
}
func init() {
// parse args here, if necessary
}
func runIndices(cmd *Command, args []string) {
var pattern = ""
if len(args) > 0 {
pattern = args[0]
}
// Get a client
client, err := elastic.NewClient(elastic.SetURL(esUrl))
if err != nil {
log.Fatal("%v", err)
}
indices, err := client.IndexNames()
if err != nil {
log.Fatal("%v", err)
}
// Sort by default
sort.Strings(indices)
for _, index := range indices {
if len(pattern) > 0 {
matched, err := regexp.MatchString(pattern, index)
if err != nil {
log.Fatal("invalid pattern")
}
if matched {
fmt.Println(index)
}
} else {
fmt.Println(index)
}
}
}