Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CascadingRadium committed Dec 9, 2024
1 parent 59c3193 commit cad9e79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
10 changes: 8 additions & 2 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ func (i *indexImpl) SearchInContext(ctx context.Context, req *SearchRequest) (sr
}

var knnHits []*search.DocumentMatch
var skipKNNCollector bool

var fts search.FieldTermSynonymMap
var skipSynonymCollector bool

var ok bool
if req.PreSearchData != nil {
for k, v := range req.PreSearchData {
Expand All @@ -530,25 +534,27 @@ func (i *indexImpl) SearchInContext(ctx context.Context, req *SearchRequest) (sr
if !ok {
return nil, fmt.Errorf("knn preSearchData must be of type []*search.DocumentMatch")
}
skipKNNCollector = true
}
case search.SynonymPreSearchDataKey:
if v != nil {
fts, ok = v.(search.FieldTermSynonymMap)
if !ok {
return nil, fmt.Errorf("synonym preSearchData must be of type search.FieldTermSynonymMap")
}
skipSynonymCollector = true
}
}
}
}
if knnHits == nil && requestHasKNN(req) {
if !skipKNNCollector && requestHasKNN(req) {
knnHits, err = i.runKnnCollector(ctx, req, indexReader, false)
if err != nil {
return nil, err
}
}

if fts == nil {
if !skipSynonymCollector {
if synMap, ok := i.m.(mapping.SynonymMapping); ok && synMap.SynonymCount() > 0 {
if synReader, ok := indexReader.(index.ThesaurusReader); ok {
fts, err = query.ExtractSynonyms(ctx, synMap, synReader, req.Query, fts)
Expand Down
50 changes: 26 additions & 24 deletions search/searcher/search_phrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,34 +164,36 @@ func NewMultiPhraseSearcher(ctx context.Context, indexReader index.IndexReader,
}
}

if fts, ok := ctx.Value(search.FieldTermSynonymMapKey).(search.FieldTermSynonymMap); ok {
if ts, exists := fts[field]; exists {
if fuzzinessEnabled {
for term, fuzzyTerms := range fuzzyTermMatches {
fuzzySynonymTerms := make([]string, 0, len(fuzzyTerms))
if s, found := ts[term]; found {
fuzzySynonymTerms = append(fuzzySynonymTerms, s...)
}
for _, fuzzyTerm := range fuzzyTerms {
if fuzzyTerm == term {
continue
}
if s, found := ts[fuzzyTerm]; found {
if ctx != nil {
if fts, ok := ctx.Value(search.FieldTermSynonymMapKey).(search.FieldTermSynonymMap); ok {
if ts, exists := fts[field]; exists {
if fuzzinessEnabled {
for term, fuzzyTerms := range fuzzyTermMatches {
fuzzySynonymTerms := make([]string, 0, len(fuzzyTerms))
if s, found := ts[term]; found {
fuzzySynonymTerms = append(fuzzySynonymTerms, s...)
}
for _, fuzzyTerm := range fuzzyTerms {
if fuzzyTerm == term {
continue
}
if s, found := ts[fuzzyTerm]; found {
fuzzySynonymTerms = append(fuzzySynonymTerms, s...)
}
}
if len(fuzzySynonymTerms) > 0 {
fuzzyTermMatches[term] = append(fuzzyTermMatches[term], fuzzySynonymTerms...)
}
}
if len(fuzzySynonymTerms) > 0 {
fuzzyTermMatches[term] = append(fuzzyTermMatches[term], fuzzySynonymTerms...)
}
}
} else {
for _, termPos := range terms {
for _, term := range termPos {
if s, found := ts[term]; found {
if fuzzyTermMatches == nil {
fuzzyTermMatches = make(map[string][]string)
} else {
for _, termPos := range terms {
for _, term := range termPos {
if s, found := ts[term]; found {
if fuzzyTermMatches == nil {
fuzzyTermMatches = make(map[string][]string)
}
fuzzyTermMatches[term] = s
}
fuzzyTermMatches[term] = s
}
}
}
Expand Down

0 comments on commit cad9e79

Please sign in to comment.