sunty
2020-09-29 69de6114e99403830f06adcf765e4fb3c563eac5
EsApi.go
@@ -1734,4 +1734,67 @@
      result = true
   }
   return result, nil
}
type ShardInfo struct {
   ShardIndex string `json:"shardIndex"`
   ShardNum   int    `json:"shardNum"`
   ShardRole  string `json:"shardRole"`
   ShardState string `json:"shardState"`
   ShardDocs  int    `json:"shardDocs"`
   ShardStore string `json:"shardStore"`
   ShardIp    string `json:"shardIp"`
   ShardNode  string `json:"shardNode"`
}
//获取索引分片信息
func GetShardsByIndex(serverIp string, serverPort string, indexName string) ([]ShardInfo, error) {
   url := "http://" + serverIp + ":" + serverPort + "/_cat/shards?v"
   buf, err := EsReq("GET", url, []byte(""))
   if err != nil {
      return nil, err
   }
   var inf = []ShardInfo{}
   res := strings.Split(string(buf), "\n")[1:]
   for _, r := range res {
      if r != "" {
         inx := strings.Fields(r)
         index := inx[0]
         shard, _ := strconv.Atoi(inx[1])
         prired := inx[2]
         if prired == "r" {
            prired = "replica"
         }
         if prired == "p" {
            prired = "primary"
         }
         state := ""
         docs := 0
         store := ""
         ip := ""
         node := ""
         if state == "STARTED" {
            docs, _ = strconv.Atoi(inx[4])
            store = inx[5]
            ip = inx[6]
            node = inx[7]
         }
         if index == indexName {
            inf = append(inf, ShardInfo{
               ShardIndex: index,
               ShardNum:   shard,
               ShardRole:  prired,
               ShardState: state,
               ShardDocs:  docs,
               ShardStore: store,
               ShardIp:    ip,
               ShardNode:  node,
            })
         }
      }
   }
   return inf, nil
}