Skip to content

Commit

Permalink
MB-57394: Consolidate documentMappingForPath & closestDocMapping (#1832)
Browse files Browse the repository at this point in the history
+ This is to half the number of times we iterate through the
  document mappings while mapping a document.
  • Loading branch information
abhinavdangeti committed Jun 15, 2023
1 parent c4a521b commit 63e7a2f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 30 deletions.
35 changes: 8 additions & 27 deletions mapping/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ func (dm *DocumentMapping) fieldDescribedByPath(path string) *FieldMapping {
return nil
}

// documentMappingForPath only returns EXACT matches for a sub document
// or for an explicitly mapped field, if you want to find the
// closest document mapping to a field not explicitly mapped
// use closestDocMapping
func (dm *DocumentMapping) documentMappingForPath(path string) *DocumentMapping {
// documentMappingForPath returns the EXACT and closest matches for a sub
// document or for an explicitly mapped field; the closest most specific
// document mapping could be one that matches part of the provided path.
func (dm *DocumentMapping) documentMappingForPath(path string) (
*DocumentMapping, *DocumentMapping) {
pathElements := decodePath(path)
current := dm
OUTER:
Expand All @@ -165,27 +165,9 @@ OUTER:
}
}

return nil
return nil, current
}
return current
}

// closestDocMapping findest the most specific document mapping that matches
// part of the provided path
func (dm *DocumentMapping) closestDocMapping(path string) *DocumentMapping {
pathElements := decodePath(path)
current := dm
OUTER:
for _, pathElement := range pathElements {
for name, subDocMapping := range current.Properties {
if name == pathElement {
current = subDocMapping
continue OUTER
}
}
break
}
return current
return current, current
}

// NewDocumentMapping returns a new document mapping
Expand Down Expand Up @@ -408,8 +390,7 @@ func (dm *DocumentMapping) walkDocument(data interface{}, path []string, indexes
func (dm *DocumentMapping) processProperty(property interface{}, path []string, indexes []uint64, context *walkContext) {
pathString := encodePath(path)
// look to see if there is a mapping for this field
subDocMapping := dm.documentMappingForPath(pathString)
closestDocMapping := dm.closestDocMapping(pathString)
subDocMapping, closestDocMapping := dm.documentMappingForPath(pathString)

// check to see if we even need to do further processing
if subDocMapping != nil && !subDocMapping.Enabled {
Expand Down
6 changes: 3 additions & 3 deletions mapping/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (im *IndexMappingImpl) MapDocument(doc *document.Document, data interface{}
docMapping.walkDocument(data, []string{}, []uint64{}, walkContext)

// see if the _all field was disabled
allMapping := docMapping.documentMappingForPath("_all")
allMapping, _ := docMapping.documentMappingForPath("_all")
if allMapping == nil || allMapping.Enabled {
field := document.NewCompositeFieldWithIndexingOptions("_all", true, []string{}, walkContext.excludedFromAll, index.IndexField|index.IncludeTermVectors)
doc.AddField(field)
Expand Down Expand Up @@ -366,7 +366,7 @@ func (im *IndexMappingImpl) AnalyzerNameForPath(path string) string {
}

// now try the default mapping
pathMapping := im.DefaultMapping.documentMappingForPath(path)
pathMapping, _ := im.DefaultMapping.documentMappingForPath(path)
if pathMapping != nil {
if len(pathMapping.Fields) > 0 {
if pathMapping.Fields[0].Analyzer != "" {
Expand Down Expand Up @@ -421,7 +421,7 @@ func (im *IndexMappingImpl) datetimeParserNameForPath(path string) string {

// first we look for explicit mapping on the field
for _, docMapping := range im.TypeMapping {
pathMapping := docMapping.documentMappingForPath(path)
pathMapping, _ := docMapping.documentMappingForPath(path)
if pathMapping != nil {
if len(pathMapping.Fields) > 0 {
if pathMapping.Fields[0].Analyzer != "" {
Expand Down

0 comments on commit 63e7a2f

Please sign in to comment.