From e189b1942c8130d473a1a4128c3d9ef5edfaa564 Mon Sep 17 00:00:00 2001
From: sunty <1172534965@qq.com>
Date: 星期五, 23 十月 2020 10:58:30 +0800
Subject: [PATCH] fix

---
 EsApi.go |   71 +++++++++++++++++++++++++++++++++--
 1 files changed, 67 insertions(+), 4 deletions(-)

diff --git a/EsApi.go b/EsApi.go
index 5d2ce5f..96348c7 100644
--- a/EsApi.go
+++ b/EsApi.go
@@ -257,8 +257,8 @@
 	if err != nil {
 		return nil, err
 	}
-	if len(source) == 0{
-		return source,nil
+	if len(source) == 0 {
+		return source, nil
 	}
 	faceSource := make([]map[string]interface{}, 0)
 	for index, info := range source {
@@ -358,8 +358,8 @@
 	if err != nil {
 		return nil, err
 	}
-	if len(source) == 0{
-		return source,nil
+	if len(source) == 0 {
+		return source, nil
 	}
 	faceSource := make([]map[string]interface{}, 0)
 	for index, info := range source {
@@ -1735,3 +1735,66 @@
 	}
 	return result, nil
 }
+
+type ShardInfo struct {
+	ShardIndex string `json:"shardIndex"` //鍒嗙墖鎵�灞炵储寮曞悕绉�
+	ShardNum   int    `json:"shardNum"`   //鍒嗙墖鍙�
+	ShardRole  string `json:"shardRole"`  //鍒嗙墖瑙掕壊(涓诲垎鐗囷細primary 鍓湰鍒嗙墖锛歳eplica)
+	ShardState string `json:"shardState"` //鍒嗙墖鐘舵��(鍚敤锛歋TARTED 鏈惎鐢細UNASSIGNED)
+	ShardDocs  int    `json:"shardDocs"`  //鍒嗙墖宸蹭繚瀛樻枃妗f暟
+	ShardStore string `json:"shardStore"` //鍒嗙墖褰撳墠瀛樺偍鏁版嵁澶у皬
+	ShardIp    string `json:"shardIp"`    //鍒嗙墖鎵�鍦ㄨ妭鐐筰p
+	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 := inx[3]
+			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
+}

--
Gitblit v1.8.0