Add request q.op=AND|OR to specify default operator for query parsing

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@443053 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Erik Hatcher 2006-09-13 18:17:21 +00:00
parent af0d115a76
commit cd441b610a
4 changed files with 38 additions and 4 deletions

View File

@ -5,7 +5,7 @@ New Features
1. added support for setting Lucene's positionIncrementGap
2. Admin: new statistics for SolrIndexSearcher
3. Admin: caches now show config params on stats page
3. max() function added to FunctionQuery suite
3. max() function added to FunctionQuery suite
4. postOptimize hook, mirroring the functionallity of the postCommit hook,
but only called on an index optimize.
5. Ability to HTTP POST query requests to /select in addition to HTTP-GET
@ -21,7 +21,7 @@ New Features
12. Ability to store term vectors for fields. (Mike Klaas via yonik, SOLR-23)
13. New abstract BufferedTokenStream for people who want to write
Tokenizers or TokenFilters that require arbitrary buffering of the
stream. (SOLR-11 / yonik, hossman)
stream. (SOLR-11 / yonik, hossman)
14. New RemoveDuplicatesToken - useful in situations where
synonyms, stemming, or word-deliminater-ing produce identical tokens at
the same position. (SOLR-11 / yonik, hossman)
@ -53,6 +53,8 @@ New Features
solrconfig.xml, support has been added for configuring values to be
appended to the multi-val request params, as well as for configuring
invariant params that can not overridden in the query. (hossman, SOLR-46)
26. Default operator for query parsing can now be specified with q.op=AND|OR
from the client request, overriding the schema value. (ehatcher)
Changes in runtime behavior
1. classes reorganized into different packages, package names changed to Apache

View File

@ -105,7 +105,7 @@ public class StandardRequestHandler implements SolrRequestHandler, SolrInfoMBean
List<String> commands = StrUtils.splitSmart(sreq,';');
String qs = commands.size() >= 1 ? commands.get(0) : "";
Query query = QueryParsing.parseQuery(qs, defaultField, req.getSchema());
Query query = QueryParsing.parseQuery(qs, defaultField, p, req.getSchema());
// If the first non-query, non-filter command is a simple sort on an indexed field, then
// we can use the Lucene sort ability.

View File

@ -19,6 +19,7 @@ package org.apache.solr.search;
import org.apache.lucene.search.*;
import org.apache.solr.search.function.*;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.Term;
import org.apache.solr.core.SolrCore;
@ -26,6 +27,7 @@ import org.apache.solr.core.SolrException;
import org.apache.solr.schema.IndexSchema;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.schema.FieldType;
import org.apache.solr.request.SolrParams;
import java.util.ArrayList;
import java.util.regex.Pattern;
@ -37,6 +39,7 @@ import java.io.IOException;
* @version $Id$
*/
public class QueryParsing {
public static final String OP = "q.op";
public static Query parseQuery(String qs, IndexSchema schema) {
return parseQuery(qs, null, schema);
@ -58,7 +61,32 @@ public class QueryParsing {
}
}
/**
* @param qs query expression
* @param defaultField default field used for unqualified search terms in the query expression
* @param params used to determine the default operator, overriding the schema specified operator
* @param schema used for default operator (overridden by params) and passed to the query parser for field format analysis information
* @return
*/
public static Query parseQuery(String qs, String defaultField, SolrParams params, IndexSchema schema) {
try {
String opParam = params.get(OP, schema.getQueryParserDefaultOperator());
QueryParser.Operator defaultOperator = "AND".equals(opParam) ? QueryParser.Operator.AND : QueryParser.Operator.OR;
SolrQueryParser parser = new SolrQueryParser(schema, defaultField);
parser.setDefaultOperator(defaultOperator);
Query query = parser.parse(qs);
if (SolrCore.log.isLoggable(Level.FINEST)) {
SolrCore.log.finest("After QueryParser:" + query);
}
return query;
} catch (ParseException e) {
SolrCore.log(e);
throw new SolrException(400,"Error parsing Lucene query",e);
}
}
/***
* SortSpec encapsulates a Lucene Sort and a count of the number of documents

View File

@ -33,11 +33,15 @@ import org.apache.solr.schema.FieldType;
public class SolrQueryParser extends QueryParser {
protected final IndexSchema schema;
/**
*
* @param schema Used for default search field name if defaultField is null and field information is used for analysis
* @param defaultField default field used for unspecified search terms. if null, the schema default field is used
*/
public SolrQueryParser(IndexSchema schema, String defaultField) {
super(defaultField == null ? schema.getDefaultSearchFieldName() : defaultField, schema.getQueryAnalyzer());
this.schema = schema;
setLowercaseExpandedTerms(false);
setDefaultOperator("AND".equals(schema.getQueryParserDefaultOperator()) ? QueryParser.Operator.AND : QueryParser.Operator.OR);
}
protected Query getFieldQuery(String field, String queryText) throws ParseException {