Apache Solr open-source search software
SOLR-17130: edismax MatchAllDocsQuery (*:*) Optimization (#2218)
Kevin Risdendf686ddc0cac
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 9146a0c84ab..cebce8dc85e 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -274,6 +274,8 @@ Optimizations* SOLR-17801: Use TotalHitCountCollector to collect count when no rows needed (Kevin Risden)+* SOLR-17130: edismax MatchAllDocsQuery (*:*) Optimization (Kevin Risden)+Bug Fixes---------------------* SOLR-17629: If SQLHandler failed to open the underlying stream (e.g. Solr returns an error; could be user/syntax problem),diff --git a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.javaindex 1f815ddffa4..5264cc61257 100644--- a/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java+++ b/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java@@ -186,10 +186,19 @@ public class ExtendedDismaxQParser extends QParser {query.add(f, BooleanClause.Occur.SHOULD);}- //- // create a boosted query (scores multiplied by boosts)- //Query topQuery = QueryUtils.build(query, this);++ // If topQuery is a boolean query, unwrap the boolean query to check if it is just+ // a MatchAllDocsQuery. Using MatchAllDocsQuery by itself enables later optimizations+ BooleanQuery topQueryBoolean = (BooleanQuery) topQuery;+ if (topQueryBoolean.clauses().size() == 1) {+ Query onlyQuery = topQueryBoolean.clauses().get(0).getQuery();+ if (onlyQuery instanceof MatchAllDocsQuery) {+ topQuery = onlyQuery;+ }+ }++ // create a boosted query (scores multiplied by boosts)List<ValueSource> boosts = getMultiplicativeBoosts();if (boosts.size() > 1) {ValueSource prod = new ProductFloatFunction(boosts.toArray(new ValueSource[0]));diff --git a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.javaindex d6f20f0e49a..71e9e426c59 100644--- a/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java+++ b/solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java@@ -25,6 +25,7 @@ import static org.apache.solr.util.QueryMatchers.termQuery;import static org.hamcrest.Matchers.allOf;import static org.hamcrest.Matchers.anyOf;import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.isA;import java.util.Arrays;import java.util.HashSet;@@ -41,6 +42,7 @@ import org.apache.lucene.search.BooleanQuery;import org.apache.lucene.search.BoostQuery;import org.apache.lucene.search.DisjunctionMaxQuery;import org.apache.lucene.search.FuzzyQuery;+import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.search.Query;import org.apache.lucene.search.TermQuery;import org.apache.solr.SolrTestCaseJ4;@@ -143,6 +145,18 @@ public class TestExtendedDismaxParser extends SolrTestCaseJ4 {}}+ @Test+ public void testMatchAllDocs() throws Exception {+ for (String sow : Arrays.asList("true", "false")) {+ for (String q : Arrays.asList("*:*", "*")) {+ try (SolrQueryRequest req = req("sow", sow, "qf", "id")) {+ QParser qParser = QParser.getParser(q, "edismax", req);+ assertThat(qParser.getQuery(), isA(MatchAllDocsQuery.class));+ }+ }+ }+ }+public void testTrailingOperators() throws Exception {for (String sow : Arrays.asList("true", "false")) {// really just test that exceptions aren't thrown by