Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle ES5.1.1 json index info #290

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion lib/catindexinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elastigo

import (
"encoding/json"
"errors"
"strconv"
"strings"
Expand Down Expand Up @@ -68,7 +69,7 @@ func (c *Conn) GetCatIndexInfo(pattern string) (catIndices []CatIndexInfo) {
args := map[string]interface{}{"bytes": "b", "h": "health,status,index,pri,rep,docs.count,docs.deleted,store.size,pri.store.size"}
indices, err := c.DoCommand("GET", "/_cat/indices/"+pattern, args, nil)
if err == nil {
indexLines := strings.Split(string(indices[:]), "\n")
indexLines := strings.Split(string(indices), "\n")
for _, index := range indexLines {
ci, _ := NewCatIndexInfo(index)
if nil != ci {
Expand All @@ -78,3 +79,55 @@ func (c *Conn) GetCatIndexInfo(pattern string) (catIndices []CatIndexInfo) {
}
return catIndices
}

// Pull all the index info from the connection, for ElasticSearch 5.1.1,
// which returns JSON in response to this query.
func (c *Conn) GetCatIndexInfoEs5(pattern string) (catIndices []CatIndexInfo) {
catIndices = make([]CatIndexInfo, 0)
//force it to only show the fileds we know about
args := map[string]interface{}{"bytes": "b", "h": "health,status,index,pri,rep,docs.count,docs.deleted,store.size,pri.store.size"}
indices, err := c.DoCommand("GET", "/_cat/indices/"+pattern, args, nil)
if err != nil {
return
}

his := []CatIndexInfoEs5{}
err = json.Unmarshal(indices, &his)
if err != nil {
return
}
for i := range his {
catIndices = append(catIndices, CatIndexInfo{
Health: his[i].Health,
Status: his[i].Status,
Name: his[i].Name,
Shards: toInt(his[i].Shards),
Replicas: toInt(his[i].Replicas),
Docs: CatIndexDocs{
Count: toInt64(his[i].DocsCount),
Deleted: toInt64(his[i].DocsDel),
},
Store: CatIndexStore{
Size: toInt64(his[i].StoreSize),
PriSize: toInt64(his[i].PriStoreSize),
},
})
}
return catIndices
}

func toInt(s string) int {
n, err := strconv.Atoi(s)
if err != nil {
return 0
}
return n
}

func toInt64(s string) int64 {
n, err := strconv.Atoi(s)
if err != nil {
return 0
}
return int64(n)
}
30 changes: 21 additions & 9 deletions lib/catresponses.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package elastigo

type CatIndexInfo struct {
Health string
Status string
Name string
Shards int
Replicas int
Docs CatIndexDocs
Store CatIndexStore
Health string `json:"health"`
Status string `json:"status"`
Name string `json:"index"`
Shards int `json:"pri"`
Replicas int `json:"rep"`
Docs CatIndexDocs `json:"docs"`
Store CatIndexStore `json:"store"`
}

type CatIndexInfoEs5 struct {
Health string `json:"health"`
Status string `json:"status"`
Name string `json:"index"`
Shards string `json:"pri"`
Replicas string `json:"rep"`
DocsCount string `json:"docs.count"`
DocsDel string `json:"docs.deleted"`
StoreSize string `json:"store.size"`
PriStoreSize string `json:"pri.store.size"`
}

type CatIndexDocs struct {
Count int64
Deleted int64
Count int64 `json:"count"`
Deleted int64 `json:"deleted"`
}

type CatIndexStore struct {
Expand Down