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
This commit is contained in:
Yonik Seeley 2010-09-11 17:28:14 +00:00
parent 628b4ed4d2
commit 37c802aff4
4 changed files with 22 additions and 9 deletions

View File

@ -259,6 +259,10 @@ New Features
when generating filter queries from terms returned from field faceting or when generating filter queries from terms returned from field faceting or
the terms component. Example: fq={!term f=weight}1.5 (hossman, yonik) 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 Optimizations
---------------------- ----------------------

View File

@ -20,10 +20,7 @@ package org.apache.solr.handler.component;
import org.apache.lucene.document.Field; import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.FieldComparator; import org.apache.lucene.search.*;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrDocumentList;
@ -77,13 +74,23 @@ public class QueryComponent extends SearchComponent
String defType = params.get(QueryParsing.DEFTYPE,QParserPlugin.DEFAULT_QTYPE); String defType = params.get(QueryParsing.DEFTYPE,QParserPlugin.DEFAULT_QTYPE);
if (rb.getQueryString() == null) { // get it from the response builder to give a different component a chance
rb.setQueryString( params.get( CommonParams.Q ) ); // 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 { try {
QParser parser = QParser.getParser(rb.getQueryString(), defType, req); 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.setSortSpec( parser.getSort(true) );
rb.setQparser(parser); rb.setQparser(parser);

View File

@ -146,7 +146,8 @@ class ExtendedDismaxQParser extends QParser {
altUserQuery = altQParser.getQuery(); altUserQuery = altQParser.getQuery();
query.add( altUserQuery , BooleanClause.Occur.MUST ); query.add( altUserQuery , BooleanClause.Occur.MUST );
} else { } else {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "missing query string" ); return null;
// throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "missing query string" );
} }
} }
else { else {

View File

@ -58,6 +58,7 @@ class LuceneQParser extends QParser {
public Query parse() throws ParseException { public Query parse() throws ParseException {
String qstr = getString(); String qstr = getString();
if (qstr == null) return null;
String defaultField = getParam(CommonParams.DF); String defaultField = getParam(CommonParams.DF);
if (defaultField==null) { if (defaultField==null) {
@ -74,7 +75,7 @@ class LuceneQParser extends QParser {
public String[] getDefaultHighlightFields() { public String[] getDefaultHighlightFields() {
return new String[]{lparser.getField()}; return lparser == null ? new String[]{} : new String[]{lparser.getField()};
} }
} }