@@ -24,7 +24,7 @@ import (
2424//
2525// We accept a query param `wait_for_completion` which defaults to true, which when false, we don't create any aliases
2626// and delete the old index, we instead return the tasks API response.
27- func reindex (ctx context.Context , indexName string , config * reindexConfig , waitForCompletion bool ) ([]byte , error ) {
27+ func reindex (ctx context.Context , sourceIndex string , config * reindexConfig , waitForCompletion bool , destinationIndex string ) ([]byte , error ) {
2828 var err error
2929
3030 // We fetch the index name pointing to the given alias first.
@@ -34,41 +34,44 @@ func reindex(ctx context.Context, indexName string, config *reindexConfig, waitF
3434 // from the given alias. If alias name doesn't exist we get an
3535 // empty slice of indices, which means the index has never been
3636 // reindexed before.
37- indices , err := getIndicesByAlias (ctx , indexName )
37+ indices , err := getIndicesByAlias (ctx , sourceIndex )
3838 if err != nil {
3939 log .Println (err )
4040 }
4141 if len (indices ) > 1 {
42- return nil , fmt .Errorf (`multiple indices pointing to alias "%s"` , indexName )
42+ return nil , fmt .Errorf (`multiple indices pointing to alias "%s"` , sourceIndex )
4343 }
4444 if len (indices ) == 1 {
45- indexName = indices [0 ]
45+ sourceIndex = indices [0 ]
4646 }
4747
4848 // If mappings are not passed, we fetch the mappings of the old index.
4949 if config .Mappings == nil {
50- config .Mappings , err = mappingsOf (ctx , indexName )
50+ config .Mappings , err = mappingsOf (ctx , sourceIndex )
5151 if err != nil {
52- return nil , fmt .Errorf (`error fetching mappings of index "%s": %v` , indexName , err )
52+ return nil , fmt .Errorf (`error fetching mappings of index "%s": %v` , sourceIndex , err )
5353 }
5454 }
5555
5656 // If settings are not passed, we fetch the settings of the old index.
5757 if config .Settings == nil {
58- config .Settings , err = settingsOf (ctx , indexName )
58+ config .Settings , err = settingsOf (ctx , sourceIndex )
5959 if err != nil {
60- return nil , fmt .Errorf (`error fetching settings of index "%s": %v` , indexName , err )
60+ return nil , fmt .Errorf (`error fetching settings of index "%s": %v` , sourceIndex , err )
6161 }
6262 }
6363
6464 // Setup the destination index prior to running the _reindex action.
6565 body := make (map [string ]interface {})
6666 body ["mappings" ] = config .Mappings
6767 body ["settings" ] = config .Settings
68+ newIndexName := destinationIndex
69+ if destinationIndex == "" {
70+ newIndexName , err = reindexedName (sourceIndex )
71+ }
6872
69- newIndexName , err := reindexedName (indexName )
7073 if err != nil {
71- return nil , fmt .Errorf (`error generating a new index name for index "%s": %v` , indexName , err )
74+ return nil , fmt .Errorf (`error generating a new index name for index "%s": %v` , sourceIndex , err )
7275 }
7376
7477 // Create the new index.
@@ -77,9 +80,14 @@ func reindex(ctx context.Context, indexName string, config *reindexConfig, waitF
7780 return nil , err
7881 }
7982
83+ // abruptly return if action is mappings
84+ if config .Action == "mappings" {
85+ return nil , nil
86+ }
87+
8088 // Configure reindex source
8189 src := es7 .NewReindexSource ().
82- Index (indexName ).
90+ Index (sourceIndex ).
8391 Type (config .Types ... ).
8492 FetchSourceIncludeExclude (config .Include , config .Exclude )
8593
@@ -103,23 +111,24 @@ func reindex(ctx context.Context, indexName string, config *reindexConfig, waitF
103111 return nil , err
104112 }
105113
106- // Fetch all the aliases of old index
107- aliases , err := aliasesOf (ctx , indexName )
108- if err != nil {
109- return nil , fmt .Errorf (`error fetching aliases of index "%s": %v` , indexName , err )
110- }
111- aliases = append (aliases , indexName )
112-
113- // Delete old index
114- err = deleteIndex (ctx , indexName )
115- if err != nil {
116- return nil , fmt .Errorf (`error deleting index "%s": %v\n` , indexName , err )
117- }
118-
119- // Set aliases of old index to the new index.
120- err = setAlias (ctx , newIndexName , aliases ... )
121- if err != nil {
122- return nil , fmt .Errorf (`error setting alias "%s" for index "%s"` , indexName , newIndexName )
114+ if destinationIndex == "" {
115+ // Fetch all the aliases of old index
116+ aliases , err := aliasesOf (ctx , sourceIndex )
117+ if err != nil {
118+ return nil , fmt .Errorf (`error fetching aliases of index "%s": %v` , sourceIndex , err )
119+ }
120+ aliases = append (aliases , sourceIndex )
121+
122+ // Delete old index
123+ err = deleteIndex (ctx , sourceIndex )
124+ if err != nil {
125+ return nil , fmt .Errorf (`error deleting index "%s": %v\n` , sourceIndex , err )
126+ }
127+ // Set aliases of old index to the new index.
128+ err = setAlias (ctx , newIndexName , aliases ... )
129+ if err != nil {
130+ return nil , fmt .Errorf (`error setting alias "%s" for index "%s"` , sourceIndex , newIndexName )
131+ }
123132 }
124133
125134 return json .Marshal (response )
0 commit comments