SOLR-3534 dismax should consult 'df' as a default for 'qf' and throw an exception when none set

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1351026 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
David Wayne Smiley 2012-06-17 00:27:05 +00:00
parent 97b9307a6e
commit 83616468a0
7 changed files with 48 additions and 19 deletions

View File

@ -655,6 +655,10 @@ Other Changes
field can no longer be populated via <copyField/> or <field default=...>
in the schema.xml.
* SOLR-3534: The Dismax and eDismax query parsers will fall back on the 'df' parameter
when 'qf' is absent. And if neither is present nor the schema default search field
then an exception will be thrown now. (dsmiley)
Documentation
----------------------
@ -701,6 +705,9 @@ Upgrading from Solr 3.5
are strongly advised that they should re-index as document signatures may
have now changed. (see SOLR-3200 & SOLR-3226 for details)
* SOLR-2724: Specifying <defaultSearchField> and <solrQueryParser defaultOperator="..."/> in
schema.xml is now considered deprecated. Instead you are encouraged to specify these via the "df"
and "q.op" parameters in your request handler definition. (David Smiley)
New Features
----------------------

View File

@ -211,7 +211,9 @@ public final class IndexSchema {
/**
* Name of the default search field specified in the schema file
* Name of the default search field specified in the schema file.
* <br/><b>Note:</b>Avoid calling this, try to use this method so that the 'df' param is consulted as an override:
* {@link org.apache.solr.search.QueryParsing#getDefaultField(IndexSchema, String)}
*/
public String getDefaultSearchFieldName() {
return defaultSearchFieldName;
@ -438,7 +440,7 @@ public final class IndexSchema {
node = (Node) xpath.evaluate("/schema/defaultSearchField/text()", document, XPathConstants.NODE);
if (node==null) {
log.warn("no default search field specified in schema.");
log.debug("no default search field specified in schema.");
} else {
defaultSearchFieldName=node.getNodeValue().trim();
// throw exception if specified, but not found or not indexed
@ -449,7 +451,7 @@ public final class IndexSchema {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, msg );
}
}
log.info("default search field is "+defaultSearchFieldName);
log.info("default search field in schema is "+defaultSearchFieldName);
}
node = (Node) xpath.evaluate("/schema/solrQueryParser/@defaultOperator", document, XPathConstants.NODE);

View File

@ -21,6 +21,7 @@ import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.common.params.DisMaxParams;
import org.apache.solr.common.params.SolrParams;
@ -63,6 +64,23 @@ public class DisMaxQParser extends QParser {
op.equals(QueryParser.Operator.AND) ? "100%" : "0%");
}
/**
* Uses {@link SolrPluginUtils#parseFieldBoosts(String)} with the 'qf' parameter. Falls back to the 'df' parameter
* or {@link org.apache.solr.schema.IndexSchema#getDefaultSearchFieldName()}.
*/
public static Map<String, Float> parseQueryFields(final IndexSchema indexSchema, final SolrParams solrParams)
throws ParseException {
Map<String, Float> queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.QF));
if (queryFields.isEmpty()) {
String df = QueryParsing.getDefaultField(indexSchema, solrParams.get(CommonParams.DF));
if (df == null) {
throw new ParseException("Neither "+DisMaxParams.QF+", "+CommonParams.DF +", nor the default search field are present.");
}
queryFields.put(df, 1.0f);
}
return queryFields;
}
public DisMaxQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
super(qstr, localParams, params, req);
}
@ -81,10 +99,7 @@ public class DisMaxQParser extends QParser {
public Query parse() throws ParseException {
SolrParams solrParams = SolrParams.wrapDefaults(localParams, params);
queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.QF));
if (0 == queryFields.size()) {
queryFields.put(req.getSchema().getDefaultSearchFieldName(), 1.0f);
}
queryFields = parseQueryFields(req.getSchema(), solrParams);
/* the main query we will execute. we disable the coord because
* this query is an artificial construct

View File

@ -133,10 +133,7 @@ class ExtendedDismaxQParser extends QParser {
userFields = new UserFields(U.parseFieldBoosts(solrParams.getParams(DMP.UF)));
queryFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.QF));
if (0 == queryFields.size()) {
queryFields.put(req.getSchema().getDefaultSearchFieldName(), 1.0f);
}
queryFields = DisMaxQParser.parseQueryFields(req.getSchema(), solrParams);
// Boosted phrase of the full query string
List<FieldParams> phraseFields =

View File

@ -39,8 +39,6 @@ import org.apache.lucene.util.CharsRef;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.core.SolrCore;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.schema.FieldType;
import org.apache.solr.schema.IndexSchema;
@ -72,7 +70,7 @@ public class QueryParsing {
/**
* Returns the "prefered" default operator for use by Query Parsers,
* Returns the "preferred" default operator for use by Query Parsers,
* based on the settings in the IndexSchema which may be overridden using
* an optional String override value.
*
@ -86,6 +84,15 @@ public class QueryParsing {
return "AND".equals(val) ? QueryParser.Operator.AND : QueryParser.Operator.OR;
}
/**
* Returns the effective default field based on the 'df' param or
* hardcoded schema default. May be null if either exists specified.
* @see org.apache.solr.common.params.CommonParams#DF
* @see org.apache.solr.schema.IndexSchema#getDefaultSearchFieldName
*/
public static String getDefaultField(final IndexSchema s, final String df) {
return df != null ? df : s.getDefaultSearchFieldName();
}
// note to self: something needs to detect infinite recursion when parsing queries
public static int parseLocalParams(String txt, int start, Map<String, String> target, SolrParams params) throws ParseException {

View File

@ -103,12 +103,8 @@ class SurroundQParser extends QParser {
// so what do we do with the SrndQuery ??
// processing based on example in LIA Ch 9
String defaultField = getParam(CommonParams.DF);
if (defaultField == null) {
defaultField = getReq().getSchema().getDefaultSearchFieldName();
}
BasicQueryFactory bqFactory = new BasicQueryFactory(this.maxBasicQueries);
String defaultField = QueryParsing.getDefaultField(getReq().getSchema(),getParam(CommonParams.DF));
Query lquery = sq.makeLuceneQueryField(defaultField, bqFactory);
return lquery;
}

View File

@ -123,6 +123,11 @@ public class TestExtendedDismaxParser extends AbstractSolrTestCase {
"q","100"), oner
);
assertQ("qf defaults to df",
req("defType", "edismax", "df", "trait_ss",
"q","Tool"), twor
);
assertQ("qf defaults to defaultSearchField"
, req( "defType", "edismax"
,"q","op")