Skip to content

Commit

Permalink
Changed filter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahil Buddharaju committed Dec 30, 2024
1 parent d57fdea commit 008919a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/opensearch/knn/index/query/KNNQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opensearch.index.query.QueryBuilder;
import org.opensearch.index.query.QueryRewriteContext;
import org.opensearch.index.query.QueryShardContext;
import org.opensearch.index.query.RangeQueryBuilder;
import org.opensearch.knn.index.engine.KNNMethodConfigContext;
import org.opensearch.knn.index.engine.model.QueryContext;
import org.opensearch.knn.index.engine.qframe.QuantizationConfig;
Expand Down Expand Up @@ -660,15 +661,22 @@ public String getWriteableName() {
return NAME;
}


@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
// rewrite filter query if it exists to avoid runtime errors in next steps of query phase
QueryBuilder rewrittenFilter;
if (Objects.nonNull(filter)) {
filter = filter.rewrite(queryShardContext);
rewrittenFilter = filter.rewrite(queryShardContext);
if (rewrittenFilter != filter) {
KNNQueryBuilder newKNNQuery = new KNNQueryBuilder(this.fieldName, this.vector, this.k, this.maxDistance, this.minScore,
this.methodParameters, rewrittenFilter, this.ignoreUnmapped, this.rescoreContext, this.expandNested);
return newKNNQuery;
}
}
return super.doRewrite(queryShardContext);
}


@Getter
@AllArgsConstructor
private static class QueryConfigFromMapping {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/opensearch/knn/index/FaissIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,8 @@ public void testQueryWithFilter_whenNonExistingFieldUsedInFilter_thenSuccessful(
Map<String, Object> mappingMap = xContentBuilderToMap(builder);
String mapping = builder.toString();

createKnnIndex(INDEX_NAME, mapping);
createIndex(INDEX_NAME, Settings.builder().put("number_of_shards", 2).put("number_of_replicas", 1).put("index.knn", true).build());
putMappingRequest(INDEX_NAME, mapping);

Float[] vector = new Float[] { 2.0f, 4.5f, 6.5f };

Expand Down
4 changes: 3 additions & 1 deletion src/test/java/org/opensearch/knn/index/LuceneEngineIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.common.Nullable;
import org.opensearch.common.settings.Settings;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.index.query.QueryBuilder;
Expand Down Expand Up @@ -304,7 +305,8 @@ public void testQueryWithFilter_whenNonExistingFieldUsedInFilter_thenSuccessful(
Map<String, Object> mappingMap = xContentBuilderToMap(builder);
String mapping = builder.toString();

createKnnIndex(INDEX_NAME, mapping);
createIndex(INDEX_NAME, Settings.builder().put("number_of_shards", 2).put("number_of_replicas", 1).put("index.knn", true).build());
putMappingRequest(INDEX_NAME, mapping);

Float[] vector = new Float[] { 2.0f, 4.5f, 6.5f };

Expand Down

0 comments on commit 008919a

Please sign in to comment.