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

SimpleText[Float|Byte]VectorValues::scorer should return null when the vector values is empty #13444

Merged
merged 2 commits into from
May 31, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ public float[] vectorValue(int targetOrd) throws IOException {

@Override
public VectorScorer scorer(float[] target) {
if (size() == 0) {
return null;
}
OffHeapFloatVectorValues values = this.copy();
return new VectorScorer() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,9 @@ public float[] vectorValue(int targetOrd) throws IOException {

@Override
public VectorScorer scorer(float[] target) {
if (size == 0) {
return null;
}
OffHeapFloatVectorValues values = this.copy();
return new VectorScorer() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,9 @@ public void testSortedIndexBytes() throws Exception {
public void testByteVectorScorerIteration() {
// unimplemented
}

@Override
public void testEmptyByteVectorData() {
// unimplemented
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,9 @@ public void testSortedIndexBytes() throws Exception {
public void testByteVectorScorerIteration() {
// unimplemented
}

@Override
public void testEmptyByteVectorData() {
// unimplemented
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public void testSortedIndexBytes() throws Exception {
public void testByteVectorScorerIteration() {
// unimplemented
}

@Override
public void testEmptyByteVectorData() {
// unimplemented
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ public int advance(int target) throws IOException {

@Override
public VectorScorer scorer(float[] target) {
if (size() == 0) {
return null;
}
SimpleTextFloatVectorValues simpleTextFloatVectorValues =
new SimpleTextFloatVectorValues(this);
return new VectorScorer() {
Expand Down Expand Up @@ -470,6 +473,9 @@ public int advance(int target) throws IOException {

@Override
public VectorScorer scorer(byte[] target) {
if (size() == 0) {
return null;
}
SimpleTextByteVectorValues simpleTextByteVectorValues = new SimpleTextByteVectorValues(this);
return new VectorScorer() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.lucene.tests.index;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.lucene.index.VectorSimilarityFunction.DOT_PRODUCT;
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -838,6 +839,58 @@ public void testByteVectorScorerIteration() throws Exception {
}
}

public void testEmptyFloatVectorData() throws Exception {
try (Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
var doc1 = new Document();
doc1.add(new StringField("id", "0", Field.Store.NO));
doc1.add(new KnnFloatVectorField("v", new float[] {2, 3, 5, 6}, DOT_PRODUCT));
w.addDocument(doc1);

var doc2 = new Document();
doc2.add(new StringField("id", "1", Field.Store.NO));
w.addDocument(doc2);

w.deleteDocuments(new Term("id", Integer.toString(0)));
w.commit();
w.forceMerge(1);

try (DirectoryReader reader = DirectoryReader.open(w)) {
LeafReader r = getOnlyLeafReader(reader);
FloatVectorValues values = r.getFloatVectorValues("v");
assertNotNull(values);
assertEquals(0, values.size());
assertNull(values.scorer(new float[] {2, 3, 5, 6}));
}
}
}

public void testEmptyByteVectorData() throws Exception {
try (Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig())) {
var doc1 = new Document();
doc1.add(new StringField("id", "0", Field.Store.NO));
doc1.add(new KnnByteVectorField("v", new byte[] {2, 3, 5, 6}, DOT_PRODUCT));
w.addDocument(doc1);

var doc2 = new Document();
doc2.add(new StringField("id", "1", Field.Store.NO));
w.addDocument(doc2);

w.deleteDocuments(new Term("id", Integer.toString(0)));
w.commit();
w.forceMerge(1);

try (DirectoryReader reader = DirectoryReader.open(w)) {
LeafReader r = getOnlyLeafReader(reader);
ByteVectorValues values = r.getByteVectorValues("v");
assertNotNull(values);
assertEquals(0, values.size());
assertNull(values.scorer(new byte[] {2, 3, 5, 6}));
}
}
}

protected VectorSimilarityFunction randomSimilarity() {
return VectorSimilarityFunction.values()[
random().nextInt(VectorSimilarityFunction.values().length)];
Expand Down