lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Hostetter <hossman_luc...@fucit.org>
Subject Re: Got it working! And some questions
Date Tue, 12 Sep 2006 20:47:49 GMT

: I've implemented the ability to override the default operator with
: q.op=AND|OR.  The patch is pasted below for your review.

if i'm reading that right, one subtlety is that "new
SolrQueryParser(schema,field)" no longer pas attention to
schema.getQueryParserDefaultOperator() -- that only only becomes
applicable when using QueryParsing.parseQuery

...i am very okay with this change, i wasn't really a fan of the fact that
the SolrQueryParser pulled that info out of the IndexSchema in it's
constructor previously, i just wanted to point out that this patch would
change that.

Perhaps the constructor for SolrQueryParser shouldn't be aware of the op
at all (either from the schema or from the SolrParams) -- and setting it
should be left to QueryParsing.parseQuery (or some other utility in the
QueryParsing class) ... personally i'm a fan of leaving SolrQueryParser as
much like QueryParser as possible -- with the only real change being the
knowledege of hte individual field formats.


: Index: src/java/org/apache/solr/search/SolrQueryParser.java
: ===================================================================
: --- src/java/org/apache/solr/search/SolrQueryParser.java	(revision
: 442689)
: +++ src/java/org/apache/solr/search/SolrQueryParser.java	(working copy)
: @@ -34,10 +34,14 @@
:     protected final IndexSchema schema;
:     public SolrQueryParser(IndexSchema schema, String defaultField) {
: +    this(schema, defaultField, QueryParser.Operator.OR);
: +  }
: +
: +  public SolrQueryParser(IndexSchema schema, String defaultField,
: QueryParser.Operator defaultOperator) {
:       super(defaultField == null ? schema.getDefaultSearchFieldName
: () : defaultField, schema.getQueryAnalyzer());
:       this.schema = schema;
:       setLowercaseExpandedTerms(false);
: -    setDefaultOperator("AND".equals
: (schema.getQueryParserDefaultOperator()) ? QueryParser.Operator.AND :
: QueryParser.Operator.OR);
: +    setDefaultOperator(defaultOperator);
:     }
:     protected Query getFieldQuery(String field, String queryText)
: throws ParseException {
: Index: src/java/org/apache/solr/search/QueryParsing.java
: ===================================================================
: --- src/java/org/apache/solr/search/QueryParsing.java	(revision 442689)
: +++ src/java/org/apache/solr/search/QueryParsing.java	(working copy)
: @@ -19,6 +19,7 @@
: 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.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 @@
:    * @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,8 +61,24 @@
:       }
:     }
: +  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;
: +      Query query = new SolrQueryParser(schema, defaultField,
: defaultOperator).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
:      * to return.
: Index: src/java/org/apache/solr/request/StandardRequestHandler.java
: ===================================================================
: --- src/java/org/apache/solr/request/StandardRequestHandler.java
: (revision 442689)
: +++ src/java/org/apache/solr/request/StandardRequestHandler.java
: (working copy)
: @@ -94,7 +94,7 @@
:         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.
:



-Hoss


Mime
View raw message