@@ -4,9 +4,11 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "regexp"
78
89 log "github.com/sirupsen/logrus"
910
11+ "github.com/appbaseio/arc/middleware/classify"
1012 "github.com/appbaseio/arc/util"
1113 es7 "github.com/olivere/elastic/v7"
1214)
@@ -114,11 +116,18 @@ func reindex(ctx context.Context, sourceIndex string, config *reindexConfig, wai
114116
115117 if destinationIndex == "" {
116118 // Fetch all the aliases of old index
117- aliases , err := aliasesOf (ctx , sourceIndex )
119+ alias , err := aliasesOf (ctx , sourceIndex )
120+
121+ var aliases = []string {}
118122 if err != nil {
119123 return nil , fmt .Errorf (`error fetching aliases of index "%s": %v` , sourceIndex , err )
120124 }
121- aliases = append (aliases , sourceIndex )
125+
126+ if alias == "" {
127+ aliases = append (aliases , sourceIndex )
128+ } else {
129+ aliases = append (aliases , alias )
130+ }
122131
123132 // Delete old index
124133 err = deleteIndex (ctx , sourceIndex )
@@ -210,22 +219,28 @@ func settingsOf(ctx context.Context, indexName string) (map[string]interface{},
210219 return settings , nil
211220}
212221
213- func aliasesOf (ctx context.Context , indexName string ) ([] string , error ) {
222+ func aliasesOf (ctx context.Context , indexName string ) (string , error ) {
214223 response , err := util .GetClient7 ().CatAliases ().
215224 Pretty (true ).
216225 Do (ctx )
217226 if err != nil {
218- return nil , err
227+ return "" , err
219228 }
220229
221- var aliases []string
230+ var alias = ""
231+
232+ // set alias for original index name only.
233+ regex := ".*reindexed_[0-9]+"
234+ r , _ := regexp .Compile (regex )
235+
222236 for _ , row := range response {
223- if row .Index == indexName {
224- aliases = append (aliases , row .Alias )
237+ // r.MatchString(indexName) this condition is added to handle existing alias which are created incorrectly
238+ if row .Index == indexName && r .MatchString (indexName ) {
239+ alias = row .Alias
225240 }
226241 }
227242
228- return aliases , nil
243+ return alias , nil
229244}
230245
231246func createIndex (ctx context.Context , indexName string , body map [string ]interface {}) error {
@@ -276,6 +291,8 @@ func setAlias(ctx context.Context, indexName string, aliases ...string) error {
276291 return fmt .Errorf (`unable to set aliases "%v" for index "%s"` , aliases , indexName )
277292 }
278293
294+ // We only have one alias per index.
295+ classify .SetIndexAlias (indexName , aliases [0 ])
279296 return nil
280297}
281298
@@ -288,3 +305,52 @@ func getIndicesByAlias(ctx context.Context, alias string) ([]string, error) {
288305 }
289306 return response .IndicesByAlias (alias ), nil
290307}
308+
309+ func getAliasedIndices (ctx context.Context ) ([]AliasedIndices , error ) {
310+ var indicesList []AliasedIndices
311+ indices , err := util .GetClient7 ().CatIndices ().
312+ Do (ctx )
313+ if err != nil {
314+ return indicesList , err
315+ }
316+
317+ aliases , err := util .GetClient7 ().CatAliases ().
318+ Pretty (true ).
319+ Do (ctx )
320+ if err != nil {
321+ return indicesList , err
322+ }
323+
324+ for _ , index := range indices {
325+ var indexStruct = AliasedIndices {
326+ Health : index .Health ,
327+ Status : index .Status ,
328+ Index : index .Index ,
329+ UUID : index .UUID ,
330+ Pri : index .Pri ,
331+ Rep : index .Rep ,
332+ DocsCount : index .DocsCount ,
333+ DocsDeleted : index .DocsDeleted ,
334+ StoreSize : index .StoreSize ,
335+ PriStoreSize : index .PriStoreSize ,
336+ }
337+ var alias string
338+ regex := ".*reindexed_[0-9]+"
339+ r , _ := regexp .Compile (regex )
340+
341+ for _ , row := range aliases {
342+ if row .Index == index .Index && r .MatchString (index .Index ) {
343+ alias = row .Alias
344+ break
345+ }
346+ }
347+ if err == nil && alias != "" {
348+ indexStruct .Alias = alias
349+ }
350+
351+ indicesList = append (indicesList , indexStruct )
352+
353+ }
354+
355+ return indicesList , nil
356+ }
0 commit comments