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

MB-64360 - Fix for no eligible filter hits in a segment #2104

Closed
wants to merge 3 commits into from
Closed
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
44 changes: 44 additions & 0 deletions index/scorch/empty_vec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) 2024 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build vectors
// +build vectors

package scorch

import segment "github.com/blevesearch/scorch_segment_api/v2"

type emptyVecPostingsIterator struct{}

func (e *emptyVecPostingsIterator) Next() (segment.VecPosting, error) {
return nil, nil
}

func (e *emptyVecPostingsIterator) Advance(uint64) (segment.VecPosting, error) {
return nil, nil
}

func (e *emptyVecPostingsIterator) Size() int {
return 0
}

func (e *emptyVecPostingsIterator) BytesRead() uint64 {
return 0
}

func (e *emptyVecPostingsIterator) ResetBytesRead(uint64) {}

func (e *emptyVecPostingsIterator) BytesWritten() uint64 { return 0 }

var anemptyVecPostingsIterator = &emptyVecPostingsIterator{}
33 changes: 19 additions & 14 deletions index/scorch/optimize_knn.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,22 @@ func (o *OptimizeVR) Finish() error {
eligibleVectorInternalIDs.And(snapshotGlobalDocNums[index])
}

eligibleLocalDocNums := make([]uint64,
eligibleVectorInternalIDs.GetCardinality())
// get the (segment-)local document numbers
for i, docNum := range eligibleVectorInternalIDs.ToArray() {
localDocNum := o.snapshot.localDocNumFromGlobal(index,
uint64(docNum))
eligibleLocalDocNums[i] = localDocNum
if eligibleVectorInternalIDs.GetCardinality() > 0 {
eligibleLocalDocNums := make([]uint64, 0,
eligibleVectorInternalIDs.GetCardinality())
// get the (segment-)local document numbers
for _, docNum := range eligibleVectorInternalIDs.ToArray() {
localDocNum := o.snapshot.localDocNumFromGlobal(index,
uint64(docNum))
eligibleLocalDocNums = append(eligibleLocalDocNums,
localDocNum)
}

if len(eligibleLocalDocNums) > 0 {
pl, err = vecIndex.SearchWithFilter(vr.vector, vr.k,
eligibleLocalDocNums, vr.searchParams)
}
}

pl, err = vecIndex.SearchWithFilter(vr.vector, vr.k,
eligibleLocalDocNums, vr.searchParams)
} else {
pl, err = vecIndex.Search(vr.vector, vr.k, vr.searchParams)
}
Expand All @@ -139,10 +144,10 @@ func (o *OptimizeVR) Finish() error {

atomic.AddUint64(&o.snapshot.parent.stats.TotKNNSearches, uint64(1))

// postings and iterators are already alloc'ed when
// IndexSnapshotVectorReader is created
vr.postings[index] = pl
vr.iterators[index] = pl.Iterator(vr.iterators[index])
if pl != nil && pl.Count() > 0 {
vr.postings[index] = pl
vr.iterators[index] = pl.Iterator(vr.iterators[index])
}
}
go vecIndex.Close()
}
Expand Down
4 changes: 4 additions & 0 deletions index/scorch/snapshot_vector_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ func (is *IndexSnapshot) VectorReaderWithFilter(ctx context.Context, vector []fl

if rv.postings == nil {
rv.postings = make([]segment_api.VecPostingsList, len(is.segment))

}
if rv.iterators == nil {
rv.iterators = make([]segment_api.VecPostingsIterator, len(is.segment))
for index := 0; index < len(is.segment); index++ {
rv.iterators[index] = anemptyVecPostingsIterator
}
}

// initialize postings and iterators within the OptimizeVR's Finish()
Expand Down
Loading