lucene-solr-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Erik Hatcher <e...@ehatchersolutions.com>
Subject Re: Got it working! And some questions
Date Tue, 12 Sep 2006 20:15:00 GMT

On Sep 11, 2006, at 2:52 PM, Yonik Seeley wrote:

> On 9/11/06, Erik Hatcher <erik@ehatchersolutions.com> wrote:
>>
>> On Sep 10, 2006, at 10:47 PM, Michael Imbeault wrote:
>> >  I'm still a little disappointed that I can't change the OR/AND
>> > parsing by just changing some parameter (like I can do for the
>> > number of results returned, for example); adding a OR between each
>> > word in the text i want to compare sounds suboptimal, but i'll
>> > probably do it that way; its a very minor nitpick, solr is awesome,
>> > as I said before.
>>
>> I'm the one that added support for controlling the default operator
>> of Solr's query parser, and I hadn't considered the use case of
>> controlling that setting from a request parameter.  It should be easy
>> enough to add.  I'll take a look at adding that support and commit it
>> once I have it working.
>>
>> What parameter name should be used for this?    do=[AND|OR] (for
>> default operator)?  We have df for default field.
>
> Maybe something like q.op or q.oper if it *only* applies to q.  Which
> begs the question... what *does* it apply to?  At first blush, it
> doesn't seem like it should apply to other queries like fq, facet
> queries, and esp queries defined in solrconfig.xml.  I think that
> would be very surprising.

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

The one thing I don't like is that QueryParsing.parseQuery(String qs,  
String defaultField, SolrParams params, IndexSchema schema) is a bit  
redundant in that it takes defaultField which can also be gleaned  
from params, but StandardRequestHandler uses "df" for highlighting also.

I'm happy to commit this if there are no objections or suggestions  
for improvement (and of course update the wiki documentation for the  
parameters).

	Erik



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.


Mime
View raw message