diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index bca20827bc8..15e8c940146 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -259,6 +259,10 @@ New Features when generating filter queries from terms returned from field faceting or the terms component. Example: fq={!term f=weight}1.5 (hossman, yonik) +* SOLR-2001: The query component will substitute an empty query that matches + no documents if the query parser returns null. This also prevents an + exception from being thrown by the default parser if "q" is missing. (yonik) + Optimizations ---------------------- diff --git a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/src/java/org/apache/solr/handler/component/QueryComponent.java index 4d49f2a3b96..1713ec8ea30 100644 --- a/solr/src/java/org/apache/solr/handler/component/QueryComponent.java +++ b/solr/src/java/org/apache/solr/handler/component/QueryComponent.java @@ -20,10 +20,7 @@ package org.apache.solr.handler.component; import org.apache.lucene.document.Field; import org.apache.lucene.index.Term; import org.apache.lucene.queryParser.ParseException; -import org.apache.lucene.search.FieldComparator; -import org.apache.lucene.search.Query; -import org.apache.lucene.search.Sort; -import org.apache.lucene.search.SortField; +import org.apache.lucene.search.*; import org.apache.lucene.util.BytesRef; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; @@ -77,13 +74,23 @@ public class QueryComponent extends SearchComponent String defType = params.get(QueryParsing.DEFTYPE,QParserPlugin.DEFAULT_QTYPE); - if (rb.getQueryString() == null) { - rb.setQueryString( params.get( CommonParams.Q ) ); + // get it from the response builder to give a different component a chance + // to set it. + String queryString = rb.getQueryString(); + if (queryString == null) { + // this is the normal way it's set. + queryString = params.get( CommonParams.Q ); + rb.setQueryString(queryString); } try { QParser parser = QParser.getParser(rb.getQueryString(), defType, req); - rb.setQuery( parser.getQuery() ); + Query q = parser.getQuery(); + if (q == null) { + // normalize a null query to a query that matches nothing + q = new BooleanQuery(); + } + rb.setQuery( q ); rb.setSortSpec( parser.getSort(true) ); rb.setQparser(parser); diff --git a/solr/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java b/solr/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java index 3a386d15e66..46eeca58228 100755 --- a/solr/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java +++ b/solr/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java @@ -146,7 +146,8 @@ class ExtendedDismaxQParser extends QParser { altUserQuery = altQParser.getQuery(); query.add( altUserQuery , BooleanClause.Occur.MUST ); } else { - throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "missing query string" ); + return null; + // throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "missing query string" ); } } else { diff --git a/solr/src/java/org/apache/solr/search/LuceneQParserPlugin.java b/solr/src/java/org/apache/solr/search/LuceneQParserPlugin.java index 2010c966bcf..1c2e8dadcc3 100755 --- a/solr/src/java/org/apache/solr/search/LuceneQParserPlugin.java +++ b/solr/src/java/org/apache/solr/search/LuceneQParserPlugin.java @@ -58,6 +58,7 @@ class LuceneQParser extends QParser { public Query parse() throws ParseException { String qstr = getString(); + if (qstr == null) return null; String defaultField = getParam(CommonParams.DF); if (defaultField==null) { @@ -74,7 +75,7 @@ class LuceneQParser extends QParser { public String[] getDefaultHighlightFields() { - return new String[]{lparser.getField()}; + return lparser == null ? new String[]{} : new String[]{lparser.getField()}; } }