On Sep 12, 2006, at 4:47 PM, Chris Hostetter wrote:
: 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.

I've reworked it based on your feedback.  The patch is pasted below.

SolrQueryParser now knows nothing about the default operator, it is set from QueryParsing.parseQuery() when passed a SolrParams.

QueryParsing.parseQuery() methods could be simplified, perhaps even into a single method, that took a query expression and a SolrQueryRequest, where it can get the SolrParams and IndexSchema. It could even get the "q" parameter from there, but there is code that passes expressions that don't come from "q". Maybe we could have two parseQuery() methods: parseQuery(String expression, SolrQueryRequest req) and parseQuery(SolrQueryRequest req), and for the latter the "q" parameter is pulled from the request and used as the expression.

As it is, the patch below works fine and I'm happy to commit it, but am happy to rework this sort of thing to get it as clean as others like.

        Erik


Index: src/java/org/apache/solr/search/SolrQueryParser.java
===================================================================
--- src/java/org/apache/solr/search/SolrQueryParser.java (revision 442772)
+++ src/java/org/apache/solr/search/SolrQueryParser.java        (working copy)
@@ -37,7 +37,6 @@
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 {
Index: src/java/org/apache/solr/search/QueryParsing.java
===================================================================
--- src/java/org/apache/solr/search/QueryParsing.java   (revision 442772)
+++ 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,26 @@
     }
   }
+ 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
    * to return.
Index: src/java/org/apache/solr/request/StandardRequestHandler.java
===================================================================
--- src/java/org/apache/solr/request/StandardRequestHandler.java (revision 442772) +++ src/java/org/apache/solr/request/StandardRequestHandler.java (working copy)
@@ -105,7 +105,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.

Reply via email to