diff --git a/index_alias_impl.go b/index_alias_impl.go index 3c7cdcd32..73cc637f7 100644 --- a/index_alias_impl.go +++ b/index_alias_impl.go @@ -30,6 +30,7 @@ type indexAliasImpl struct { name string indexes []Index mutex sync.RWMutex + mapping mapping.IndexMapping open bool } @@ -360,6 +361,10 @@ func (i *indexAliasImpl) Mapping() mapping.IndexMapping { return nil } + if i.mapping != nil { + return i.mapping + } + err := i.isAliasToSingleIndex() if err != nil { return nil diff --git a/mapping/document.go b/mapping/document.go index 5d70af912..b470afaa4 100644 --- a/mapping/document.go +++ b/mapping/document.go @@ -40,11 +40,12 @@ import ( // are used. To disable this automatic handling, set // Dynamic to false. type DocumentMapping struct { - Enabled bool `json:"enabled"` - Dynamic bool `json:"dynamic"` - Properties map[string]*DocumentMapping `json:"properties,omitempty"` - Fields []*FieldMapping `json:"fields,omitempty"` - DefaultAnalyzer string `json:"default_analyzer,omitempty"` + Enabled bool `json:"enabled"` + Dynamic bool `json:"dynamic"` + Properties map[string]*DocumentMapping `json:"properties,omitempty"` + Fields []*FieldMapping `json:"fields,omitempty"` + DefaultAnalyzer string `json:"default_analyzer,omitempty"` + DefaultSynonymSource string `json:"default_synonym_source,omitempty"` // StructTagKey overrides "json" when looking for field names in struct tags StructTagKey string `json:"struct_tag_key,omitempty"` @@ -306,6 +307,11 @@ func (dm *DocumentMapping) UnmarshalJSON(data []byte) error { if err != nil { return err } + case "default_synonym_source": + err := util.UnmarshalJSON(v, &dm.DefaultSynonymSource) + if err != nil { + return err + } case "properties": err := util.UnmarshalJSON(v, &dm.Properties) if err != nil { @@ -349,6 +355,22 @@ func (dm *DocumentMapping) defaultAnalyzerName(path []string) string { return rv } +func (dm *DocumentMapping) defaultSynonymSource(path []string) string { + current := dm + rv := current.DefaultSynonymSource + for _, pathElement := range path { + var ok bool + current, ok = current.Properties[pathElement] + if !ok { + break + } + if current.DefaultSynonymSource != "" { + rv = current.DefaultSynonymSource + } + } + return rv +} + func (dm *DocumentMapping) walkDocument(data interface{}, path []string, indexes []uint64, context *walkContext) { // allow default "json" tag to be overridden structTagKey := dm.StructTagKey diff --git a/mapping/index.go b/mapping/index.go index 36b423108..feb7634a9 100644 --- a/mapping/index.go +++ b/mapping/index.go @@ -49,6 +49,7 @@ type IndexMappingImpl struct { DefaultType string `json:"default_type"` DefaultAnalyzer string `json:"default_analyzer"` DefaultDateTimeParser string `json:"default_datetime_parser"` + DefaultSynonymSource string `json:"default_synonym_source"` DefaultField string `json:"default_field"` StoreDynamic bool `json:"store_dynamic"` IndexDynamic bool `json:"index_dynamic"` @@ -268,6 +269,11 @@ func (im *IndexMappingImpl) UnmarshalJSON(data []byte) error { if err != nil { return err } + case "default_synonym_source": + err := util.UnmarshalJSON(v, &im.DefaultSynonymSource) + if err != nil { + return err + } case "default_field": err := util.UnmarshalJSON(v, &im.DefaultField) if err != nil { @@ -517,7 +523,25 @@ func (im *IndexMappingImpl) SynonymSourceForPath(path string) string { } } - return "" + // next we will try default synonym sources for the path + pathDecoded := decodePath(path) + for _, docMapping := range im.TypeMapping { + if docMapping.Enabled { + rv := docMapping.defaultSynonymSource(pathDecoded) + if rv != "" { + return rv + } + } + } + // now the default analyzer for the default mapping + if im.DefaultMapping.Enabled { + rv := im.DefaultMapping.defaultSynonymSource(pathDecoded) + if rv != "" { + return rv + } + } + + return im.DefaultSynonymSource } func (im *IndexMappingImpl) SynonymCount() int {