This is an automated email from the ASF dual-hosted git repository. abenedetti pushed a commit to branch jira/SOLR-16567 in repository https://gitbox.apache.org/repos/asf/solr.git
commit 84d9e71d6442ca7854a5e48ee002f59e635926c2 Author: Alessandro Benedetti <[email protected]> AuthorDate: Wed Dec 21 16:57:08 2022 +0100 workaround to improve pre-filtering support for Knn search --- solr/core/src/java/org/apache/solr/search/neural/KnnQParser.java | 8 ++++---- .../src/test/org/apache/solr/search/neural/KnnQParserTest.java | 2 +- .../modules/query-guide/pages/dense-vector-search.adoc | 7 +++++++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/search/neural/KnnQParser.java b/solr/core/src/java/org/apache/solr/search/neural/KnnQParser.java index 6f3dc4956a2..c4c68d2527d 100644 --- a/solr/core/src/java/org/apache/solr/search/neural/KnnQParser.java +++ b/solr/core/src/java/org/apache/solr/search/neural/KnnQParser.java @@ -82,10 +82,10 @@ public class KnnQParser extends QParser { float[] parsedVectorToSearch = parseVector(vectorToSearch, denseVectorType.getDimension()); return denseVectorType.getKnnVectorQuery( - schemaField.getName(), parsedVectorToSearch, topK, buildPreFilter()); + schemaField.getName(), parsedVectorToSearch, topK, getFilterQuery()); } - private Query buildPreFilter() throws SolrException { + private Query getFilterQuery() throws SolrException { boolean isSubQuery = recurseCount != 0; if (!isFilter() && !isSubQuery) { String[] filterQueries = req.getParams().getParams(CommonParams.FQ); @@ -93,7 +93,7 @@ public class KnnQParser extends QParser { List<Query> preFilters; try { - preFilters = acceptPreFiltersOnly(QueryUtils.parseFilterQueries(req, true)); + preFilters = excludePostFilters(QueryUtils.parseFilterQueries(req, true)); } catch (SyntaxError e) { throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } @@ -122,7 +122,7 @@ public class KnnQParser extends QParser { * @param filters filter queries in the knn query request * @return the list of pre-filters */ - private List<Query> acceptPreFiltersOnly(List<Query> filters) { + private List<Query> excludePostFilters(List<Query> filters) { return filters.stream() .filter( q -> { diff --git a/solr/core/src/test/org/apache/solr/search/neural/KnnQParserTest.java b/solr/core/src/test/org/apache/solr/search/neural/KnnQParserTest.java index 2debf1b5314..65098a92f70 100644 --- a/solr/core/src/test/org/apache/solr/search/neural/KnnQParserTest.java +++ b/solr/core/src/test/org/apache/solr/search/neural/KnnQParserTest.java @@ -366,7 +366,7 @@ public class KnnQParserTest extends SolrTestCaseJ4 { "fq", "id:(3 4 9 2)", "fq", - "{!frange l=0.99}$q", + "{!frange cache=false l=0.99}$q", "fl", "id"), "//result[@numFound='2']", diff --git a/solr/solr-ref-guide/modules/query-guide/pages/dense-vector-search.adoc b/solr/solr-ref-guide/modules/query-guide/pages/dense-vector-search.adoc index 1484ab84ed7..b9e6106ffec 100644 --- a/solr/solr-ref-guide/modules/query-guide/pages/dense-vector-search.adoc +++ b/solr/solr-ref-guide/modules/query-guide/pages/dense-vector-search.adoc @@ -287,6 +287,13 @@ In &q={!knn f=vector topK=10}[1.0, 2.0, 3.0, 4.0]&fq=id:(1 2 3) The results are prefiltered by the fq=id:(1 2 3) and then only the documents from this subset are considered as candidates for the topK knn retrieval. + +If you want to run some of the filter queries as post-filters you can follow the standard approach for post-filtering in Apache Solr, using the cache and cost local parameters. + +e.g. + +[source,text] +&q={!knn f=vector topK=10}[1.0, 2.0, 3.0, 4.0]&fq={!frange cache=false l=0.99}$q ====
