Skip to content

Commit

Permalink
LUCENE-10408: Fix vector values iteration bug (#690)
Browse files Browse the repository at this point in the history
Now that there is special logic to handle the dense case, we need to adjust some
assertions in VectorValues#advance.
  • Loading branch information
jtibshirani committed Feb 18, 2022
1 parent 4461f2c commit fcc384f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,10 @@ public int advance(int target) {
}
}

assert ord <= size;
if (ord == size) {
doc = NO_MORE_DOCS;
} else {
if (ord < size) {
doc = ordToDocOperator.applyAsInt(ord);
} else {
doc = NO_MORE_DOCS;
}
return doc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
import org.apache.lucene.tests.util.LuceneTestCase;
Expand Down Expand Up @@ -98,6 +99,40 @@ public void testAllDocsHaveField() throws IOException {
}
}

public void testConjunction() throws IOException {
try (Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir)) {
int numDocs = atLeast(100);
int numVectors = 0;

boolean allDocsHaveVector = random().nextBoolean();
for (int i = 0; i < numDocs; ++i) {
Document doc = new Document();
if (allDocsHaveVector || random().nextBoolean()) {
doc.add(new KnnVectorField("vector", randomVector(5)));
numVectors++;
}
doc.add(new StringField("field", "value" + (i % 2), Store.NO));
iw.addDocument(doc);
}
try (IndexReader reader = iw.getReader()) {
IndexSearcher searcher = newSearcher(reader);
Occur occur = random().nextBoolean() ? Occur.MUST : Occur.FILTER;
BooleanQuery booleanQuery =
new BooleanQuery.Builder()
.add(new TermQuery(new Term("field", "value1")), occur)
.add(new KnnVectorFieldExistsQuery("vector"), Occur.FILTER)
.build();

int count = searcher.count(booleanQuery);
assertTrue(count <= numVectors);
if (allDocsHaveVector) {
assertEquals(numDocs / 2, count);
}
}
}
}

public void testFieldExistsButNoDocsHaveField() throws IOException {
try (Directory dir = newDirectory();
RandomIndexWriter iw = new RandomIndexWriter(random(), dir)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ public void testRandom() throws IOException {
}

/** Tests with random vectors and a random filter. Uses RandomIndexWriter. */
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/LUCENE-10408")
public void testRandomWithFilter() throws IOException {
int numDocs = 200;
int dimension = atLeast(5);
Expand Down

0 comments on commit fcc384f

Please sign in to comment.