Skip to content

Commit 8c486c0

Browse files
MB-57394: Consolidate documentMappingForPath & closestDocMapping
+ This is to half the number of times we iterate through the document mappings while mapping a document.
1 parent c4a521b commit 8c486c0

File tree

2 files changed

+11
-30
lines changed

2 files changed

+11
-30
lines changed

mapping/document.go

+8-27
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ func (dm *DocumentMapping) fieldDescribedByPath(path string) *FieldMapping {
140140
return nil
141141
}
142142

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

168-
return nil
168+
return nil, current
169169
}
170-
return current
171-
}
172-
173-
// closestDocMapping findest the most specific document mapping that matches
174-
// part of the provided path
175-
func (dm *DocumentMapping) closestDocMapping(path string) *DocumentMapping {
176-
pathElements := decodePath(path)
177-
current := dm
178-
OUTER:
179-
for _, pathElement := range pathElements {
180-
for name, subDocMapping := range current.Properties {
181-
if name == pathElement {
182-
current = subDocMapping
183-
continue OUTER
184-
}
185-
}
186-
break
187-
}
188-
return current
170+
return current, current
189171
}
190172

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

414395
// check to see if we even need to do further processing
415396
if subDocMapping != nil && !subDocMapping.Enabled {

mapping/index.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (im *IndexMappingImpl) MapDocument(doc *document.Document, data interface{}
326326
docMapping.walkDocument(data, []string{}, []uint64{}, walkContext)
327327

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

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

422422
// first we look for explicit mapping on the field
423423
for _, docMapping := range im.TypeMapping {
424-
pathMapping := docMapping.documentMappingForPath(path)
424+
pathMapping, _ := docMapping.documentMappingForPath(path)
425425
if pathMapping != nil {
426426
if len(pathMapping.Fields) > 0 {
427427
if pathMapping.Fields[0].Analyzer != "" {

0 commit comments

Comments
 (0)