Skip to content

Commit

Permalink
add default synonym sources
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Nov 30, 2024
1 parent 91be472 commit 9baf914
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
5 changes: 5 additions & 0 deletions index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type indexAliasImpl struct {
name string
indexes []Index
mutex sync.RWMutex
mapping mapping.IndexMapping
open bool
}

Expand Down Expand Up @@ -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
Expand Down
32 changes: 27 additions & 5 deletions mapping/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
26 changes: 25 additions & 1 deletion mapping/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 9baf914

Please sign in to comment.