Skip to content

Commit d8aafea

Browse files
finally
1 parent 281e784 commit d8aafea

4 files changed

Lines changed: 14 additions & 50 deletions

File tree

search/collector/knn.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,8 @@ type KNNCollector struct {
136136
took time.Duration
137137
results search.DocumentMatchCollection
138138
maxScore float64
139-
140-
nestedStore *collectStoreNested
141139
}
142140

143-
// NewKNNCollector creates a new KNNCollector for the given K values and size.
144141
func NewKNNCollector(kArray []int64, size int64) *KNNCollector {
145142
return &KNNCollector{
146143
knnStore: GetNewKNNCollectorStore(kArray),

search/collector/topn.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,19 @@ func newTopNCollector(size int, skip int, sort search.SortOrder, nr index.Nested
140140
// merge field term locations
141141
parent.FieldTermLocations = search.MergeFieldTermLocationsFromMatch(parent.FieldTermLocations, child)
142142
// add child's ID to parent's Descendants
143-
parent.AddDescendantID(child.IndexInternalID)
143+
// add other as descendant only if it is not the same document
144+
if !parent.IndexInternalID.Equals(child.IndexInternalID) {
145+
// Add a copy of child.IndexInternalID to descendants, because
146+
// child.IndexInternalID will be reset when 'child' is recycled.
147+
var descendantID index.IndexInternalID
148+
// first check if parent's descendants slice has capacity to reuse
149+
if len(parent.Descendants) < cap(parent.Descendants) {
150+
// reuse the buffer element at len(parent.Descendants)
151+
descendantID = parent.Descendants[:len(parent.Descendants)+1][len(parent.Descendants)]
152+
}
153+
// copy the contents of id into descendantID, allocating if needed
154+
parent.Descendants = append(parent.Descendants, index.NewIndexInternalIDFrom(descendantID, child.IndexInternalID))
155+
}
144156
return nil
145157
}
146158
hc.nestedStore = newStoreNested(nr, search.DescendantAdderCallbackFn(descAdder))

search/search.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,21 +382,6 @@ func (dm *DocumentMatch) String() string {
382382
return fmt.Sprintf("[%s-%f]", dm.ID, dm.Score)
383383
}
384384

385-
func (dm *DocumentMatch) AddDescendantID(id index.IndexInternalID) {
386-
// add other as descendant only if it is not the same document
387-
if !dm.IndexInternalID.Equals(id) {
388-
// Add a copy of id to descendants
389-
var descendantID index.IndexInternalID
390-
// first check if dm's descendants slice has capacity to reuse
391-
if len(dm.Descendants) < cap(dm.Descendants) {
392-
// reuse the buffer element at len(dm.Descendants)
393-
descendantID = dm.Descendants[:len(dm.Descendants)+1][len(dm.Descendants)]
394-
}
395-
// copy the contents of id into descendantID, allocating if needed
396-
dm.Descendants = append(dm.Descendants, index.NewIndexInternalIDFrom(descendantID, id))
397-
}
398-
}
399-
400385
type DocumentMatchCollection []*DocumentMatch
401386

402387
func (c DocumentMatchCollection) Len() int { return len(c) }

search_knn.go

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"sort"
2525

2626
"github.com/blevesearch/bleve/v2/document"
27-
"github.com/blevesearch/bleve/v2/mapping"
2827
"github.com/blevesearch/bleve/v2/search"
2928
"github.com/blevesearch/bleve/v2/search/collector"
3029
"github.com/blevesearch/bleve/v2/search/query"
@@ -452,10 +451,7 @@ func (i *indexImpl) runKnnCollector(ctx context.Context, req *SearchRequest, rea
452451
err = serr
453452
}
454453
}()
455-
knnCollector, err := i.buildKNNCollector(ctx, KNNQuery, reader, kArray, sumOfK)
456-
if err != nil {
457-
return nil, err
458-
}
454+
knnCollector := collector.NewKNNCollector(kArray, sumOfK)
459455
err = knnCollector.Collect(ctx, knnSearcher, reader)
460456
if err != nil {
461457
return nil, err
@@ -485,9 +481,6 @@ func setKnnHitsInCollector(knnHits []*search.DocumentMatch, coll *collector.TopN
485481
ftsMatch.Score += knnMatch.Score
486482
// Combine the FTS explanation with the KNN explanation, if present
487483
ftsMatch.Expl.MergeWith(knnMatch.Expl)
488-
// Add the Descendants from the KNN match to the FTS match, deduplicating them on the way
489-
// The Descendants of a DocumentMatch is always sorted, and we must maintain that invariant
490-
ftsMatch.Descendants = search.SortedUnion(ftsMatch.Descendants, knnMatch.Descendants)
491484
}
492485
coll.SetKNNHits(knnHits, search.HybridMergeCallbackFn(mergeFn))
493486
}
@@ -688,26 +681,3 @@ func (r *rescorer) restoreKnnRequest() {
688681
r.req.KNN[i].Boost = &b
689682
}
690683
}
691-
692-
func (i *indexImpl) buildKNNCollector(ctx context.Context, KNNQuery query.Query, reader index.IndexReader, kArray []int64, sumOfK int64) (*collector.KNNCollector, error) {
693-
// check if we are in nested mode
694-
if nestedMode, ok := ctx.Value(search.NestedSearchKey).(bool); ok && nestedMode {
695-
// get the nested reader from the index reader
696-
if nr, ok := reader.(index.NestedReader); ok {
697-
// check if the KNN query intersects with the nested mapping
698-
if nm, ok := i.m.(mapping.NestedMapping); ok {
699-
var fs search.FieldSet
700-
var err error
701-
fs, err = query.ExtractFields(KNNQuery, i.m, fs)
702-
if err != nil {
703-
return nil, err
704-
}
705-
if nm.IntersectsPrefix(fs) {
706-
return collector.NewNestedKNNCollector(kArray, sumOfK, nr), nil
707-
}
708-
}
709-
}
710-
}
711-
712-
return collector.NewKNNCollector(kArray, sumOfK), nil
713-
}

0 commit comments

Comments
 (0)