Skip to content

Commit

Permalink
remove redundant call to strings.Split()
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Jan 24, 2024
1 parent 9ffb0ea commit 39b5139
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
29 changes: 22 additions & 7 deletions mapping/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,28 @@ func (dm *DocumentMapping) fieldDescribedByPath(path string) *FieldMapping {
return nil
}

// documentMappingForPath returns the EXACT and closest matches for a sub
// documentMappingForPathElements 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) (
func (dm *DocumentMapping) documentMappingForPathElements(pathElements []string) (
*DocumentMapping, *DocumentMapping) {
pathElements := decodePath(path)
var pathElementsCopy []string
if len(pathElements) == 0 {
pathElementsCopy = []string{""}
} else {
pathElementsCopy = pathElements
}
current := dm
OUTER:
for i, pathElement := range pathElements {
for i, pathElement := range pathElementsCopy {
if subDocMapping, exists := current.Properties[pathElement]; exists {
current = subDocMapping
continue OUTER
}

// no subDocMapping matches this pathElement
// only if this is the last element check for field name
if i == len(pathElements)-1 {
if i == len(pathElementsCopy)-1 {
for _, field := range current.Fields {
if field.Name == pathElement {
break
Expand All @@ -185,6 +190,15 @@ OUTER:
return current, current
}

// 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)
return dm.documentMappingForPathElements(pathElements)
}

// NewDocumentMapping returns a new document mapping
// with all the default values.
func NewDocumentMapping() *DocumentMapping {
Expand Down Expand Up @@ -403,9 +417,8 @@ 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, closestDocMapping := dm.documentMappingForPath(pathString)
subDocMapping, closestDocMapping := dm.documentMappingForPathElements(path)

// check to see if we even need to do further processing
if subDocMapping != nil && !subDocMapping.Enabled {
Expand All @@ -417,6 +430,8 @@ func (dm *DocumentMapping) processProperty(property interface{}, path []string,
// cannot do anything with the zero value
return
}

pathString := encodePath(path)
propertyType := propertyValue.Type()
switch propertyType.Kind() {
case reflect.String:
Expand Down
24 changes: 19 additions & 5 deletions search_knn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"sort"
"strconv"
"sync"
"testing"

"github.com/blevesearch/bleve/v2/analysis/lang/en"
Expand Down Expand Up @@ -284,11 +285,24 @@ func createMultipleSegmentsIndex(documents []map[string]interface{}, index Index
}
prevCutoff += docsPerBatch[i]
}
for _, batch := range batches {
err = index.Batch(batch)
if err != nil {
return err
}
errMutex := sync.Mutex{}
var errors []error
wg := sync.WaitGroup{}
wg.Add(len(batches))
for i, batch := range batches {
go func(ix int, batchx *Batch) {
defer wg.Done()
err := index.Batch(batchx)
if err != nil {
errMutex.Lock()
errors = append(errors, err)
errMutex.Unlock()
}
}(i, batch)
}
wg.Wait()
if len(errors) > 0 {
return errors[0]
}
return nil
}
Expand Down

0 comments on commit 39b5139

Please sign in to comment.