Solr: Use QueryUtils.combineQueryAndFilter more

and check MatchAllDocsQuery (minor & cheap optimization)
 Closes #1407
This commit is contained in:
David Smiley 2020-04-05 11:28:33 -04:00
parent 47bc18478a
commit 1fc4a546df
8 changed files with 16 additions and 49 deletions

View File

@ -19,15 +19,14 @@ package org.apache.solr.analytics.facet;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.solr.analytics.function.ReductionCollectionManager.ReductionDataCollection;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.search.Filter;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryUtils;
/**
* A facet that breaks down the data by additional Solr Queries.
@ -51,10 +50,7 @@ public class QueryFacet extends AbstractSolrQueryFacet {
}
// The searcher sends docIds to the QueryFacetAccumulator which forwards
// them to <code>collectQuery()</code> in this class for collection.
Query queryQuery = new BooleanQuery.Builder()
.add(q, Occur.MUST)
.add(filter, Occur.FILTER)
.build();
Query queryQuery = QueryUtils.combineQueryAndFilter(q, filter);
ReductionDataCollection dataCol = collectionManager.newDataCollection();
reductionData.put(queryName, dataCol);

View File

@ -20,9 +20,7 @@ import java.util.EnumSet;
import java.util.List;
import java.util.function.Consumer;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.solr.analytics.function.ReductionCollectionManager.ReductionDataCollection;
import org.apache.solr.analytics.util.FacetRangeGenerator;
import org.apache.solr.analytics.util.FacetRangeGenerator.FacetRange;
@ -31,6 +29,7 @@ import org.apache.solr.common.params.FacetParams.FacetRangeOther;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.Filter;
import org.apache.solr.search.QueryUtils;
/**
* A facet that groups data by a discrete set of ranges.
@ -66,10 +65,7 @@ public class RangeFacet extends AbstractSolrQueryFacet {
Query q = sf.getType().getRangeQuery(null, sf, range.lower, range.upper, range.includeLower,range.includeUpper);
// The searcher sends docIds to the RangeFacetAccumulator which forwards
// them to <code>collectRange()</code> in this class for collection.
Query rangeQuery = new BooleanQuery.Builder()
.add(q, Occur.MUST)
.add(filter, Occur.FILTER)
.build();
Query rangeQuery = QueryUtils.combineQueryAndFilter(q, filter);
ReductionDataCollection dataCol = collectionManager.newDataCollection();
reductionData.put(range.toString(), dataCol);

View File

@ -176,7 +176,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
sort = SortSpecParsing.parseSortSpec(sortParam, rb.req).getSort();
}
Query query;
final Query query;
List<Query> newFilters = new ArrayList<>();
try {
if (qs == null) {
@ -415,8 +415,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
collector = groupExpandCollector;
}
query = QueryUtils.combineQueryAndFilter(query, pfilter.filter);
searcher.search(query, collector);
searcher.search(QueryUtils.combineQueryAndFilter(query, pfilter.filter), collector);
ReturnFields returnFields = rb.rsp.getReturnFields();
LongObjectMap<Collector> groups = ((GroupCollector) groupExpandCollector).getGroups();

View File

@ -47,8 +47,6 @@ import org.apache.lucene.index.MultiTerms;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.FilterCollector;
@ -82,11 +80,11 @@ import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.TrieField;
import org.apache.solr.search.BitDocSet;
import org.apache.solr.search.DocSet;
import org.apache.solr.search.Filter;
import org.apache.solr.search.Grouping;
import org.apache.solr.search.Insanity;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.search.QueryUtils;
import org.apache.solr.search.SolrIndexSearcher;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.facet.FacetDebugInfo;
@ -336,12 +334,7 @@ public class SimpleFacets {
}
AllGroupsCollector collector = new AllGroupsCollector<>(new TermGroupSelector(groupField));
Filter mainQueryFilter = docSet.getTopFilter(); // This returns a filter that only matches documents matching with q param and fq params
Query filteredFacetQuery = new BooleanQuery.Builder()
.add(facetQuery, Occur.MUST)
.add(mainQueryFilter, Occur.FILTER)
.build();
searcher.search(filteredFacetQuery, collector);
searcher.search(QueryUtils.combineQueryAndFilter(facetQuery, docSet.getTopFilter()), collector);
return collector.getGroupCount();
}

View File

@ -28,8 +28,6 @@ import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Collector;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.LeafCollector;
@ -120,11 +118,7 @@ public class DocSetUtil {
public static DocSet createDocSet(SolrIndexSearcher searcher, Query query, DocSet filter) throws IOException {
if (filter != null) {
Filter luceneFilter = filter.getTopFilter();
query = new BooleanQuery.Builder()
.add(query, BooleanClause.Occur.MUST)
.add(luceneFilter, BooleanClause.Occur.FILTER)
.build();
query = QueryUtils.combineQueryAndFilter(query, filter.getTopFilter());
}
if (query instanceof TermQuery) {

View File

@ -438,8 +438,7 @@ public class Grouping {
collector = timeLimitingCollector;
}
try {
Query q = QueryUtils.combineQueryAndFilter(query, luceneFilter);
searcher.search(q, collector);
searcher.search(QueryUtils.combineQueryAndFilter(query, luceneFilter), collector);
} catch (TimeLimitingCollector.TimeExceededException | ExitableDirectoryReader.ExitingReaderException x) {
log.warn( "Query: " + query + "; " + x.getMessage() );
qr.setPartialResults(true);

View File

@ -151,14 +151,15 @@ public class QueryUtils {
* If neither are null then we combine with a BooleanQuery.
*/
public static Query combineQueryAndFilter(Query scoreQuery, Query filterQuery) {
if (scoreQuery == null) {
// check for *:* is simple and avoids needless BooleanQuery wrapper even though BQ.rewrite optimizes this away
if (scoreQuery == null || scoreQuery instanceof MatchAllDocsQuery) {
if (filterQuery == null) {
return new MatchAllDocsQuery(); // default if nothing -- match everything
} else {
return new ConstantScoreQuery(filterQuery);
}
} else {
if (filterQuery == null) {
if (filterQuery == null || filterQuery instanceof MatchAllDocsQuery) {
return scoreQuery;
} else {
return new BooleanQuery.Builder()

View File

@ -50,7 +50,6 @@ import org.apache.lucene.index.Term;
import org.apache.lucene.index.Terms;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.*;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
@ -894,12 +893,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI
collector = pf.postFilter;
}
if (pf.filter != null) {
Query query = new BooleanQuery.Builder().add(main, Occur.MUST).add(pf.filter, Occur.FILTER).build();
search(query, collector);
} else {
search(main, collector);
}
search(QueryUtils.combineQueryAndFilter(main, pf.filter), collector);
if (collector instanceof DelegatingCollector) {
((DelegatingCollector) collector).finish();
@ -1584,10 +1578,8 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI
boolean needScores = (cmd.getFlags() & GET_SCORES) != 0;
Query query = QueryUtils.makeQueryable(cmd.getQuery());
ProcessedFilter pf = getProcessedFilter(cmd.getFilter(), cmd.getFilterList());
query = QueryUtils.combineQueryAndFilter(query, pf.filter);
final Query query = QueryUtils.combineQueryAndFilter(QueryUtils.makeQueryable(cmd.getQuery()), pf.filter);
// handle zero case...
if (lastDocRequested <= 0) {
@ -1691,10 +1683,7 @@ public class SolrIndexSearcher extends IndexSearcher implements Closeable, SolrI
int maxDoc = maxDoc();
ProcessedFilter pf = getProcessedFilter(cmd.getFilter(), cmd.getFilterList());
Query query = QueryUtils.makeQueryable(cmd.getQuery());
if (pf.filter != null) {
query = new BooleanQuery.Builder().add(query, Occur.MUST).add(pf.filter, Occur.FILTER).build();
}
final Query query = QueryUtils.combineQueryAndFilter(QueryUtils.makeQueryable(cmd.getQuery()), pf.filter);
// handle zero case...
if (lastDocRequested <= 0) {