Skip to content

Commit df686dd

Browse files
authored
SOLR-17130: edismax MatchAllDocsQuery (*:*) Optimization (#2218)
1 parent b61fdcb commit df686dd

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

solr/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ Optimizations
274274

275275
* SOLR-17801: Use TotalHitCountCollector to collect count when no rows needed (Kevin Risden)
276276

277+
* SOLR-17130: edismax MatchAllDocsQuery (*:*) Optimization (Kevin Risden)
278+
277279
Bug Fixes
278280
---------------------
279281
* SOLR-17629: If SQLHandler failed to open the underlying stream (e.g. Solr returns an error; could be user/syntax problem),

solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParser.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,19 @@ public Query parse() throws SyntaxError {
186186
query.add(f, BooleanClause.Occur.SHOULD);
187187
}
188188

189-
//
190-
// create a boosted query (scores multiplied by boosts)
191-
//
192189
Query topQuery = QueryUtils.build(query, this);
190+
191+
// If topQuery is a boolean query, unwrap the boolean query to check if it is just
192+
// a MatchAllDocsQuery. Using MatchAllDocsQuery by itself enables later optimizations
193+
BooleanQuery topQueryBoolean = (BooleanQuery) topQuery;
194+
if (topQueryBoolean.clauses().size() == 1) {
195+
Query onlyQuery = topQueryBoolean.clauses().get(0).getQuery();
196+
if (onlyQuery instanceof MatchAllDocsQuery) {
197+
topQuery = onlyQuery;
198+
}
199+
}
200+
201+
// create a boosted query (scores multiplied by boosts)
193202
List<ValueSource> boosts = getMultiplicativeBoosts();
194203
if (boosts.size() > 1) {
195204
ValueSource prod = new ProductFloatFunction(boosts.toArray(new ValueSource[0]));

solr/core/src/test/org/apache/solr/search/TestExtendedDismaxParser.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.hamcrest.Matchers.allOf;
2626
import static org.hamcrest.Matchers.anyOf;
2727
import static org.hamcrest.Matchers.containsString;
28+
import static org.hamcrest.Matchers.isA;
2829

2930
import java.util.Arrays;
3031
import java.util.HashSet;
@@ -41,6 +42,7 @@
4142
import org.apache.lucene.search.BoostQuery;
4243
import org.apache.lucene.search.DisjunctionMaxQuery;
4344
import org.apache.lucene.search.FuzzyQuery;
45+
import org.apache.lucene.search.MatchAllDocsQuery;
4446
import org.apache.lucene.search.Query;
4547
import org.apache.lucene.search.TermQuery;
4648
import org.apache.solr.SolrTestCaseJ4;
@@ -143,6 +145,18 @@ public void testSyntax() throws Exception {
143145
}
144146
}
145147

148+
@Test
149+
public void testMatchAllDocs() throws Exception {
150+
for (String sow : Arrays.asList("true", "false")) {
151+
for (String q : Arrays.asList("*:*", "*")) {
152+
try (SolrQueryRequest req = req("sow", sow, "qf", "id")) {
153+
QParser qParser = QParser.getParser(q, "edismax", req);
154+
assertThat(qParser.getQuery(), isA(MatchAllDocsQuery.class));
155+
}
156+
}
157+
}
158+
}
159+
146160
public void testTrailingOperators() throws Exception {
147161
for (String sow : Arrays.asList("true", "false")) {
148162
// really just test that exceptions aren't thrown by

0 commit comments

Comments
 (0)