From 37c802aff4b3c78ad27b65866a921009550a5f38 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Sat, 11 Sep 2010 17:28:14 +0000 Subject: [PATCH] SOLR-2001: prevent exception on missing q git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@996182 13f79535-47bb-0310-9956-ffa450edef68 --- solr/CHANGES.txt | 4 ++++ .../handler/component/QueryComponent.java | 21 ++++++++++++------- .../search/ExtendedDismaxQParserPlugin.java | 3 ++- .../solr/search/LuceneQParserPlugin.java | 3 ++- 4 files changed, 22 insertions(+), 9 deletions(-) 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()}; } }