Skip to content

Commit

Permalink
Adding alias support for elastic mapping caching in Segments API
Browse files Browse the repository at this point in the history
If alias is used for index name on elastic server, Segments API couldn't
get mapping information for that segment. If such case occurs, db_elastic.go
tries to get available aliases for requested index as a fallback
and use latest index (alphabetically) to get mapping information.
  • Loading branch information
rootpd committed May 13, 2019
1 parent 4a99731 commit 39999ee
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Beam/go/model/db_elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"math"
"sort"
"strconv"
"time"

Expand Down Expand Up @@ -503,10 +504,27 @@ func (eDB *ElasticDB) cacheFieldMapping(index string) (map[string]string, error)
return nil, errors.Wrap(err, fmt.Sprintf("unable to get field mappings for index: %s", index))
}

// read mapping info of index
root, ok := result[index].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid index provided, no mapping data available: %s", index)
// there's no such index, but there might be an alias
aliases, err := eDB.Client.Aliases().Index(index).Do(eDB.Context)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unable to get aliases for index: %s", index))
}

indices := aliases.IndicesByAlias(index)
if len(indices) == 0 {
return nil, fmt.Errorf("invalid index provided, no mapping data available: %s", index)
}

sort.Strings(indices)
root, ok = result[indices[len(indices)-1]].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid alias provided, no mapping data available: %s", index)
}
}

mappings, ok := root["mappings"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("\"mappings\" field not present within field mapping response")
Expand Down

0 comments on commit 39999ee

Please sign in to comment.