Skip to content

Commit

Permalink
Adding filter to the toString() method of KnnFloatVectorQuery (apache…
Browse files Browse the repository at this point in the history
…#13990)

* Adding filter to toString() of KnnFloatVectorQuery when it's present (addresses apache#13983)

* addressing review comments

* adding knnbytevectorquery

* unit test improvements

* tidy

* adding changes entry for the bug fix
  • Loading branch information
viswanathk authored Nov 18, 2024
1 parent 4400d55 commit a991a9a
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Bug Fixes
* GITHUB#13944: Ensure deterministic order of clauses for `DisjunctionMaxQuery#toString`. (Laurent Jakubina)
* GITHUB#13841: Improve Tessellatorlogic when two holes share the same vertex with the polygon which was failing
in valid polygons. (Ignacio Vera)
* GITHUB#13990: Added filter to the toString() method of Knn[Float|Byte]VectorQuery
and DiversifyingChildren[Float|Byte]KnnVectorQuery. (Viswanath Kuchibhotla)

Build
---------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ abstract class AbstractKnnVectorQuery extends Query {

protected final String field;
protected final int k;
private final Query filter;
protected final Query filter;

public AbstractKnnVectorQuery(String field, int k, Query filter) {
this.field = Objects.requireNonNull(field, "field");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ VectorScorer createVectorScorer(LeafReaderContext context, FieldInfo fi) throws

@Override
public String toString(String field) {
return getClass().getSimpleName() + ":" + this.field + "[" + target[0] + ",...][" + k + "]";
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName() + ":");
buffer.append(this.field + "[" + target[0] + ",...]");
buffer.append("[" + k + "]");
if (this.filter != null) {
buffer.append("[" + this.filter + "]");
}
return buffer.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,14 @@ VectorScorer createVectorScorer(LeafReaderContext context, FieldInfo fi) throws

@Override
public String toString(String field) {
return getClass().getSimpleName() + ":" + this.field + "[" + target[0] + ",...][" + k + "]";
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName() + ":");
buffer.append(this.field + "[" + target[0] + ",...]");
buffer.append("[" + k + "]");
if (this.filter != null) {
buffer.append("[" + this.filter + "]");
}
return buffer.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.TestVectorUtil;
Expand Down Expand Up @@ -78,6 +79,11 @@ public void testToString() throws IOException {
assertEquals("KnnByteVectorQuery:field[0,...][10]", query.toString("ignored"));

assertDocScoreQueryToString(query.rewrite(newSearcher(reader)));

// test with filter
Query filter = new TermQuery(new Term("id", "text"));
query = getKnnVectorQuery("field", new float[] {0, 1}, 10, filter);
assertEquals("KnnByteVectorQuery:field[0,...][10][id:text]", query.toString("ignored"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.store.Directory;
import org.apache.lucene.tests.index.RandomIndexWriter;
Expand Down Expand Up @@ -77,6 +78,11 @@ public void testToString() throws IOException {
assertEquals("KnnFloatVectorQuery:field[0.0,...][10]", query.toString("ignored"));

assertDocScoreQueryToString(query.rewrite(newSearcher(reader)));

// test with filter
Query filter = new TermQuery(new Term("id", "text"));
query = getKnnVectorQuery("field", new float[] {0.0f, 1.0f}, 10, filter);
assertEquals("KnnFloatVectorQuery:field[0.0,...][10][id:text]", query.toString("ignored"));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,14 @@ protected TopDocs approximateSearch(

@Override
public String toString(String field) {
return getClass().getSimpleName() + ":" + this.field + "[" + query[0] + ",...][" + k + "]";
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName() + ":");
buffer.append(this.field + "[" + query[0] + ",...]");
buffer.append("[" + k + "]");
if (this.filter != null) {
buffer.append("[" + this.filter + "]");
}
return buffer.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,14 @@ protected TopDocs approximateSearch(

@Override
public String toString(String field) {
return getClass().getSimpleName() + ":" + this.field + "[" + query[0] + ",...][" + k + "]";
StringBuilder buffer = new StringBuilder();
buffer.append(getClass().getSimpleName() + ":");
buffer.append(this.field + "[" + query[0] + ",...]");
buffer.append("[" + k + "]");
if (this.filter != null) {
buffer.append("[" + this.filter + "]");
}
return buffer.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;

public class TestParentBlockJoinByteKnnVectorQuery extends ParentBlockJoinKnnVectorQueryTestCase {
Expand Down Expand Up @@ -81,6 +83,20 @@ d, new IndexWriterConfig().setMergePolicy(newMergePolicy(random(), false)))) {
}
}

public void testToString() {
// test without filter
Query query = getParentJoinKnnQuery("field", new float[] {0, 1}, null, 10, null);
assertEquals(
"DiversifyingChildrenByteKnnVectorQuery:field[0,...][10]", query.toString("ignored"));

// test with filter
Query filter = new TermQuery(new Term("id", "text"));
query = getParentJoinKnnQuery("field", new float[] {0, 1}, filter, 10, null);
assertEquals(
"DiversifyingChildrenByteKnnVectorQuery:field[0,...][10][id:text]",
query.toString("ignored"));
}

private static byte[] fromFloat(float[] queryVector) {
byte[] query = new byte[queryVector.length];
for (int i = 0; i < queryVector.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;

public class TestParentBlockJoinFloatKnnVectorQuery extends ParentBlockJoinKnnVectorQueryTestCase {
Expand Down Expand Up @@ -110,6 +112,20 @@ d, new IndexWriterConfig().setMergePolicy(newMergePolicy(random(), false)))) {
}
}

public void testToString() {
// test without filter
Query query = getParentJoinKnnQuery("field", new float[] {0, 1}, null, 10, null);
assertEquals(
"DiversifyingChildrenFloatKnnVectorQuery:field[0.0,...][10]", query.toString("ignored"));

// test with filter
Query filter = new TermQuery(new Term("id", "text"));
query = getParentJoinKnnQuery("field", new float[] {0.0f, 1.0f}, filter, 10, null);
assertEquals(
"DiversifyingChildrenFloatKnnVectorQuery:field[0.0,...][10][id:text]",
query.toString("ignored"));
}

@Override
Field getKnnVectorField(String name, float[] vector) {
return new KnnFloatVectorField(name, vector);
Expand Down

0 comments on commit a991a9a

Please sign in to comment.