Skip to content

Commit

Permalink
bug fixes after merge conflict resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Thejas-bhat committed Jan 2, 2025
1 parent dbed957 commit 52e318d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 29 deletions.
58 changes: 32 additions & 26 deletions index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package bleve

import (
"context"
"fmt"
"sync"
"time"

Expand Down Expand Up @@ -191,10 +192,11 @@ func (i *indexAliasImpl) SearchInContext(ctx context.Context, req *SearchRequest
// indicates that this index alias is set as an Index
// in another alias, so we need to do a preSearch search
// and NOT a real search
bm25PreSearch, _ := isBM25Enabled(req, i.mapping)
flags := &preSearchFlags{
knn: requestHasKNN(req),
synonyms: !isMatchNoneQuery(req.Query),
bm25: true, // TODO Just force setting it to true to test
bm25: bm25PreSearch,
}
return preSearchDataSearch(ctx, req, flags, i.indexes...)
}
Expand Down Expand Up @@ -234,7 +236,7 @@ func (i *indexAliasImpl) SearchInContext(ctx context.Context, req *SearchRequest
// - the request requires preSearch
var preSearchDuration time.Duration
var sr *SearchResult
flags, err := preSearchRequired(req, i.mapping)
flags, err := preSearchRequired(ctx, req, i.mapping)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -574,7 +576,7 @@ type asyncSearchResult struct {
type preSearchFlags struct {
knn bool
synonyms bool
bm25 bool // needs presearch for this too
bm25 bool // needs presearch for this too
}

func isBM25Enabled(req *SearchRequest, m mapping.IndexMapping) (bool, query.FieldSet) {
Expand All @@ -600,7 +602,7 @@ func isBM25Enabled(req *SearchRequest, m mapping.IndexMapping) (bool, query.Fiel

// preSearchRequired checks if preSearch is required and returns the presearch flags struct
// indicating which preSearch is required
func preSearchRequired(ctx context.Context, req *SearchRequest, m mapping.IndexMapping) (*preSearchFlags, error){
func preSearchRequired(ctx context.Context, req *SearchRequest, m mapping.IndexMapping) (*preSearchFlags, error) {
// Check for KNN query
knn := requestHasKNN(req)
var synonyms bool
Expand Down Expand Up @@ -631,12 +633,12 @@ func preSearchRequired(ctx context.Context, req *SearchRequest, m mapping.IndexM
}
}
}

if knn || synonyms || bm25 {
return &preSearchFlags{
knn: knn,
synonyms: synonyms,
bm25: bm25,
bm25: bm25,
}, nil
}
return nil, nil
Expand All @@ -646,7 +648,7 @@ func preSearch(ctx context.Context, req *SearchRequest, flags *preSearchFlags, i
// create a dummy request with a match none query
// since we only care about the preSearchData in PreSearch
var dummyQuery = req.Query
if !flags.bm25 || !flags.synonyms {
if !flags.bm25 && !flags.synonyms {
// create a dummy request with a match none query
// since we only care about the preSearchData in PreSearch
dummyQuery = query.NewMatchNoneQuery()
Expand Down Expand Up @@ -728,7 +730,9 @@ func constructSynonymPreSearchData(rv map[string]map[string]interface{}, sr *Sea
for _, index := range indexes {
rv[index.Name()][search.SynonymPreSearchDataKey] = sr.SynonymResult
}
return rv
}

func constructBM25PreSearchData(rv map[string]map[string]interface{}, sr *SearchResult, indexes []Index) map[string]map[string]interface{} {
bmStats := sr.BM25Stats
if bmStats != nil {
Expand All @@ -744,27 +748,27 @@ func constructBM25PreSearchData(rv map[string]map[string]interface{}, sr *Search

func constructPreSearchData(req *SearchRequest, flags *preSearchFlags,
preSearchResult *SearchResult, indexes []Index) (map[string]map[string]interface{}, error) {
if flags == nil || preSearchResult == nil {
return nil, fmt.Errorf("invalid input, flags: %v, preSearchResult: %v", flags, preSearchResult)
}
mergedOut := make(map[string]map[string]interface{}, len(indexes))
for _, index := range indexes {
mergedOut[index.Name()] = make(map[string]interface{})
}
var err error
if flags.knn {
mergedOut, err = constructKnnPreSearchData(mergedOut, preSearchResult, indexes)
if err != nil {
return nil, err
}
}
if flags.synonyms {
mergedOut = constructSynonymPreSearchData(mergedOut, preSearchResult, indexes)
if flags.bm25 {
mergedOut = constructBM25PreSearchData(mergedOut, preSearchResult, indexes)
if flags == nil || preSearchResult == nil {
return nil, fmt.Errorf("invalid input, flags: %v, preSearchResult: %v", flags, preSearchResult)
}
mergedOut := make(map[string]map[string]interface{}, len(indexes))
for _, index := range indexes {
mergedOut[index.Name()] = make(map[string]interface{})
}
var err error
if flags.knn {
mergedOut, err = constructKnnPreSearchData(mergedOut, preSearchResult, indexes)
if err != nil {
return nil, err
}
return mergedOut, nil
}
if flags.synonyms {
mergedOut = constructSynonymPreSearchData(mergedOut, preSearchResult, indexes)
}
if flags.bm25 {
mergedOut = constructBM25PreSearchData(mergedOut, preSearchResult, indexes)
}
return mergedOut, nil
}

func preSearchDataSearch(ctx context.Context, req *SearchRequest, flags *preSearchFlags, indexes ...Index) (*SearchResult, error) {
Expand Down Expand Up @@ -871,6 +875,8 @@ func redistributePreSearchData(req *SearchRequest, indexes []Index) (map[string]
if fts, ok := req.PreSearchData[search.SynonymPreSearchDataKey].(search.FieldTermSynonymMap); ok {
for _, index := range indexes {
rv[index.Name()][search.SynonymPreSearchDataKey] = fts
}
}

if bm25Data, ok := req.PreSearchData[search.BM25PreSearchDataKey].(*search.BM25Stats); ok {
for _, index := range indexes {
Expand Down
1 change: 1 addition & 0 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/blevesearch/bleve/v2/search/collector"
"github.com/blevesearch/bleve/v2/search/facet"
"github.com/blevesearch/bleve/v2/search/highlight"
"github.com/blevesearch/bleve/v2/search/query"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/geo/s2"
Expand Down
6 changes: 4 additions & 2 deletions pre_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package bleve

import "github.com/blevesearch/bleve/v2/search"
import (
"github.com/blevesearch/bleve/v2/search"
)

// A preSearchResultProcessor processes the data in
// the preSearch result from multiple
Expand Down Expand Up @@ -94,7 +96,7 @@ func newBM25PreSearchResultProcessor() *bm25PreSearchResultProcessor {
// TODO How will this work for queries other than term queries?
func (b *bm25PreSearchResultProcessor) add(sr *SearchResult, indexName string) {
if sr.BM25Stats != nil {
b.docCount += (sr.BM25Stats.DocCount)
b.docCount += sr.BM25Stats.DocCount
for field, cardinality := range sr.BM25Stats.FieldCardinality {
b.fieldCardinality[field] += cardinality
}
Expand Down
2 changes: 1 addition & 1 deletion search/searcher/search_term.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func NewSynonymSearcher(ctx context.Context, indexReader index.IndexReader, term
if err != nil {
return nil, err
}
return newTermSearcherFromReader(indexReader, reader, term, field, boostVal, options)
return newTermSearcherFromReader(ctx, indexReader, reader, term, field, boostVal, options)
}
// create a searcher for the term itself
termSearcher, err := createTermSearcher(term, boost)
Expand Down

0 comments on commit 52e318d

Please sign in to comment.