From 549bda93eaf6bdaf0db5ae6c0060da1a3906f812 Mon Sep 17 00:00:00 2001
From: neza2017 <yefu.chen@zilliz.com>
Date: Thu, 18 Feb 2021 15:58:59 +0800
Subject: [PATCH] Fix get index by name

Signed-off-by: neza2017 <yefu.chen@zilliz.com>
---
 internal/masterservice/master_service_test.go |  5 ++-
 internal/masterservice/meta_table.go          | 40 +++++--------------
 2 files changed, 13 insertions(+), 32 deletions(-)

diff --git a/internal/masterservice/master_service_test.go b/internal/masterservice/master_service_test.go
index e36929be75..7e612f947d 100644
--- a/internal/masterservice/master_service_test.go
+++ b/internal/masterservice/master_service_test.go
@@ -650,8 +650,9 @@ func TestMasterService(t *testing.T) {
 		rsp, err := core.DescribeIndex(req)
 		assert.Nil(t, err)
 		assert.Equal(t, rsp.Status.ErrorCode, commonpb.ErrorCode_SUCCESS)
-		assert.Equal(t, len(rsp.IndexDescriptions), 1)
-		assert.Equal(t, rsp.IndexDescriptions[0].IndexName, Params.DefaultIndexName)
+		assert.Equal(t, len(rsp.IndexDescriptions), 2)
+		idxNames := []string{rsp.IndexDescriptions[0].IndexName, rsp.IndexDescriptions[1].IndexName}
+		assert.ElementsMatch(t, idxNames, []string{"testColl_index_100", Params.DefaultIndexName})
 	})
 
 	t.Run("flush segment", func(t *testing.T) {
diff --git a/internal/masterservice/meta_table.go b/internal/masterservice/meta_table.go
index ce54d49daa..d187f3288c 100644
--- a/internal/masterservice/meta_table.go
+++ b/internal/masterservice/meta_table.go
@@ -701,7 +701,7 @@ func (mt *metaTable) GetNotIndexedSegments(collName string, fieldName string, id
 
 		mt.indexID2Meta[idx.IndexID] = *idxInfo
 		k2 := path.Join(IndexMetaPrefix, strconv.FormatInt(idx.IndexID, 10))
-		v2 := proto.MarshalTextString(idx)
+		v2 := proto.MarshalTextString(idxInfo)
 		meta := map[string]string{k1: v1, k2: v2}
 
 		err = mt.client.MultiSave(meta)
@@ -751,40 +751,20 @@ func (mt *metaTable) GetIndexByName(collName string, fieldName string, indexName
 	if !ok {
 		return nil, errors.Errorf("collection %s not found", collName)
 	}
-	fileSchema, err := mt.GetFieldSchema(collName, fieldName)
+	fieldSchema, err := mt.GetFieldSchema(collName, fieldName)
 	if err != nil {
 		return nil, err
 	}
 
 	rstIndex := make([]pb.IndexInfo, 0, len(collMeta.FieldIndexes))
-	existMap := map[typeutil.UniqueID]bool{}
-
-	for _, partID := range collMeta.PartitionIDs {
-		partMeta, ok := mt.partitionID2Meta[partID]
-		if ok {
-			for _, segID := range partMeta.SegmentIDs {
-				idxMeta, ok := mt.segID2IndexMeta[segID]
-				if !ok {
-					continue
-				}
-				for idxID, segMeta := range *idxMeta {
-					if segMeta.FieldID != fileSchema.FieldID {
-						continue
-					}
-					idxMeta, ok := mt.indexID2Meta[idxID]
-					if !ok {
-						continue
-					}
-					if _, ok = existMap[idxID]; ok {
-						continue
-					}
-					if indexName == "" {
-						rstIndex = append(rstIndex, idxMeta)
-					} else if idxMeta.IndexName == indexName {
-						rstIndex = append(rstIndex, idxMeta)
-					}
-					existMap[idxID] = true
-				}
+	for _, idx := range collMeta.FieldIndexes {
+		if idx.FiledID == fieldSchema.FieldID {
+			idxInfo, ok := mt.indexID2Meta[idx.IndexID]
+			if !ok {
+				return nil, errors.Errorf("index id = %d not found", idx.IndexID)
+			}
+			if indexName == "" || idxInfo.IndexName == indexName {
+				rstIndex = append(rstIndex, idxInfo)
 			}
 		}
 	}