Skip to content

Commit

Permalink
first draft
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Oct 16, 2024
1 parent 3c77b49 commit 994fa9f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 5 deletions.
11 changes: 11 additions & 0 deletions mapping/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ func (dm *DocumentMapping) analyzerNameForPath(path string) string {
return ""
}

// synonymSourceForPath attempts to first find the field
// described by this path, then returns the analyzer
// configured for that field
func (dm *DocumentMapping) synonymSourceForPath(path string) string {
field := dm.fieldDescribedByPath(path)
if field != nil {
return field.SynonymSource
}
return ""
}

func (dm *DocumentMapping) fieldDescribedByPath(path string) *FieldMapping {
pathElements := decodePath(path)
if len(pathElements) > 1 {
Expand Down
2 changes: 2 additions & 0 deletions mapping/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ type FieldMapping struct {

// Applicable to vector fields only - optimization string
VectorIndexOptimizedFor string `json:"vector_index_optimized_for,omitempty"`

SynonymSource string `json:"synonym_source,omitempty"`
}

// NewTextFieldMapping returns a default field mapping for text
Expand Down
22 changes: 22 additions & 0 deletions mapping/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,25 @@ func (im *IndexMappingImpl) FieldMappingForPath(path string) FieldMapping {
func (im *IndexMappingImpl) DefaultSearchField() string {
return im.DefaultField
}

func (im *IndexMappingImpl) SynonymSourceForPath(path string) string {
// first we look for explicit mapping on the field
for _, docMapping := range im.TypeMapping {
synonymSource := docMapping.synonymSourceForPath(path)
if synonymSource != "" {
return synonymSource
}
}

// now try the default mapping
pathMapping, _ := im.DefaultMapping.documentMappingForPath(path)
if pathMapping != nil {
if len(pathMapping.Fields) > 0 {
if pathMapping.Fields[0].SynonymSource != "" {
return pathMapping.Fields[0].SynonymSource
}
}
}

return ""
}
73 changes: 68 additions & 5 deletions pre_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type preSearchResultProcessor interface {
finalize(*SearchResult)
}

// -----------------------------------------------------------------------------
// KNN preSearchResultProcessor for handling KNN presearch results
type knnPreSearchResultProcessor struct {
addFn func(sr *SearchResult, indexName string)
finalizeFn func(sr *SearchResult)
Expand All @@ -44,16 +46,77 @@ func (k *knnPreSearchResultProcessor) finalize(sr *SearchResult) {
}

// -----------------------------------------------------------------------------
// Synonym preSearchResultProcessor for handling Synonym presearch results
type synonymPreSearchResultProcessor struct {
addFn func(sr *SearchResult, indexName string)
finalizeFn func(sr *SearchResult)
}

func finalizePreSearchResult(req *SearchRequest, preSearchResult *SearchResult) {
if requestHasKNN(req) {
preSearchResult.Hits = finalizeKNNResults(req, preSearchResult.Hits)
func (s *synonymPreSearchResultProcessor) add(sr *SearchResult, indexName string) {
if s.addFn != nil {
s.addFn(sr, indexName)
}
}

func (s *synonymPreSearchResultProcessor) finalize(sr *SearchResult) {
if s.finalizeFn != nil {
s.finalizeFn(sr)
}
}

// -----------------------------------------------------------------------------
// Master struct that can hold any number of presearch result processors
type compositePreSearchResultProcessor struct {
presearchResultProcessors []preSearchResultProcessor
}

// Implements the add method, which forwards to all the internal processors
func (m *compositePreSearchResultProcessor) add(sr *SearchResult, indexName string) {
for _, p := range m.presearchResultProcessors {
p.add(sr, indexName)
}
}

// Implements the finalize method, which forwards to all the internal processors
func (m *compositePreSearchResultProcessor) finalize(sr *SearchResult) {
for _, p := range m.presearchResultProcessors {
p.finalize(sr)
}
}

// -----------------------------------------------------------------------------
// Function to create the appropriate preSearchResultProcessor(s)
func createPreSearchResultProcessor(req *SearchRequest) preSearchResultProcessor {
var processors []preSearchResultProcessor
// Add KNN processor if the request has KNN
if requestHasKNN(req) {
if knnProcessor := newKnnPreSearchResultProcessor(req); knnProcessor != nil {
processors = append(processors, knnProcessor)
}
}
// Add Synonym processor if the request has Synonym
if requestHasSynonym(req) {

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

undefined: requestHasSynonym

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

undefined: requestHasSynonym

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

undefined: requestHasSynonym

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

undefined: requestHasSynonym

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

undefined: requestHasSynonym

Check failure on line 98 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

undefined: requestHasSynonym
if synonymProcessor := newSynonymPreSearchResultProcessor(req); synonymProcessor != nil {

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

undefined: newSynonymPreSearchResultProcessor

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, macos-latest)

undefined: newSynonymPreSearchResultProcessor

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

undefined: newSynonymPreSearchResultProcessor

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, macos-latest)

undefined: newSynonymPreSearchResultProcessor

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, ubuntu-latest)

undefined: newSynonymPreSearchResultProcessor

Check failure on line 99 in pre_search.go

View workflow job for this annotation

GitHub Actions / test (1.22.x, macos-latest)

undefined: newSynonymPreSearchResultProcessor
processors = append(processors, synonymProcessor)
}
}
// Return based on the number of processors, optimizing for the common case of 1 processor
// If there are no processors, return nil
switch len(processors) {
case 0:
return nil
case 1:
return processors[0]
default:
return &compositePreSearchResultProcessor{
presearchResultProcessors: processors,
}
}
}

// -----------------------------------------------------------------------------
func finalizePreSearchResult(req *SearchRequest, preSearchResult *SearchResult) {
if requestHasKNN(req) {
return newKnnPreSearchResultProcessor(req)
preSearchResult.Hits = finalizeKNNResults(req, preSearchResult.Hits)
}
return &knnPreSearchResultProcessor{} // equivalent to nil
}

0 comments on commit 994fa9f

Please sign in to comment.