Skip to content

Apache Lucene open-source search software

SOLR-529 - Better error messages from SolrQueryParser when field isn't specified and there is no defaultSearchField in schema.xml

Chris M. Hostetter6042bdbad0c0
diff --git a/CHANGES.txt b/CHANGES.txt
index a6010e8c1aa..71c3fae773e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -323,6 +323,10 @@ Bug Fixes
20. SOLR-535: Fixed typo (Tokenzied -> Tokenized) in schema.jsp (Thomas Peuss via billa)
+21. SOLR-529: Better error messages from SolrQueryParser when field isn't
+ specified and there is no defaultSearchField in schema.xml
+ (Lars Kotthoff via hossman)
+
Other Changes
1. SOLR-135: Moved common classes to org.apache.solr.common and altered the
build scripts to make two jars: apache-solr-1.3.jar and
diff --git a/src/java/org/apache/solr/search/SolrQueryParser.java b/src/java/org/apache/solr/search/SolrQueryParser.java
index 94f7134c327..27bc50ba29d 100644
--- a/src/java/org/apache/solr/search/SolrQueryParser.java
+++ b/src/java/org/apache/solr/search/SolrQueryParser.java
@@ -22,6 +22,7 @@ import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.ConstantScoreRangeQuery;
import org.apache.lucene.search.Query;
+import org.apache.solr.common.SolrException;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
@@ -51,6 +52,7 @@ import org.apache.solr.schema.IndexSchema;
public class SolrQueryParser extends QueryParser {
protected final IndexSchema schema;
protected final QParser parser;
+ protected final String defaultField;
/**
* Constructs a SolrQueryParser using the schema to understand the
@@ -66,6 +68,7 @@ public class SolrQueryParser extends QueryParser {
super(defaultField == null ? schema.getDefaultSearchFieldName() : defaultField, schema.getQueryAnalyzer());
this.schema = schema;
this.parser = null;
+ this.defaultField = defaultField;
setLowercaseExpandedTerms(false);
}
@@ -73,11 +76,20 @@ public class SolrQueryParser extends QueryParser {
super(defaultField, parser.getReq().getSchema().getQueryAnalyzer());
this.schema = parser.getReq().getSchema();
this.parser = parser;
+ this.defaultField = defaultField;
setLowercaseExpandedTerms(false);
}
+ private void checkNullField(String field) throws SolrException {
+ if (field == null && defaultField == null) {
+ throw new SolrException
+ (SolrException.ErrorCode.BAD_REQUEST,
+ "no field name specified in query and no defaultSearchField defined in schema.xml");
+ }
+ }
protected Query getFieldQuery(String field, String queryText) throws ParseException {
+ checkNullField(field);
// intercept magic field name of "_" to use as a hook for our
// own functions.
if (field.charAt(0) == '_') {
@@ -98,6 +110,7 @@ public class SolrQueryParser extends QueryParser {
}
protected Query getRangeQuery(String field, String part1, String part2, boolean inclusive) throws ParseException {
+ checkNullField(field);
FieldType ft = schema.getFieldType(field);
return new ConstantScoreRangeQuery(
field,
@@ -107,6 +120,7 @@ public class SolrQueryParser extends QueryParser {
}
protected Query getPrefixQuery(String field, String termStr) throws ParseException {
+ checkNullField(field);
if (getLowercaseExpandedTerms()) {
termStr = termStr.toLowerCase();
}