Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use the thesaurus datatype to search for equivalent terms #2090

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
}

// -----------------------------------------------------------------------------
// 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
}
Loading