lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dsmi...@apache.org
Subject svn commit: r1306151 - in /lucene/dev/branches/branch_3x/solr/core/src: java/org/apache/solr/handler/component/ java/org/apache/solr/search/ test/org/apache/solr/search/
Date Wed, 28 Mar 2012 04:19:31 GMT
Author: dsmiley
Date: Wed Mar 28 04:19:31 2012
New Revision: 1306151

URL: http://svn.apache.org/viewvc?rev=1306151&view=rev
Log:
SOLR-435 3x backport of SOLR-2001 plus empty string check, dismax support, and test

Modified:
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.java
    lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/QParser.java
    lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/search/QueryParsingTest.java

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
(original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java
Wed Mar 28 04:19:31 2012
@@ -92,13 +92,23 @@ public class QueryComponent extends Sear
 
     String defType = params.get(QueryParsing.DEFTYPE,QParserPlugin.DEFAULT_QTYPE);
 
-    if (rb.getQueryString() == null) {
-      rb.setQueryString( params.get( CommonParams.Q ) );
+    // get it from the response builder to give a different component a chance
+    // to set it.
+    String queryString = rb.getQueryString();
+    if (queryString == null) {
+      // this is the normal way it's set.
+      queryString = params.get( CommonParams.Q );
+      rb.setQueryString(queryString);
     }
 
     try {
       QParser parser = QParser.getParser(rb.getQueryString(), defType, req);
-      rb.setQuery( parser.getQuery() );
+      Query q = parser.getQuery();
+      if (q == null) {
+        // normalize a null query to a query that matches nothing
+        q = new BooleanQuery();        
+      }
+      rb.setQuery( q );
       rb.setSortSpec( parser.getSort(true) );
       rb.setQparser(parser);
 

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java
(original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/DisMaxQParser.java
Wed Mar 28 04:19:31 2012
@@ -20,7 +20,6 @@ import org.apache.lucene.queryParser.Par
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Query;
-import org.apache.solr.common.SolrException;
 import org.apache.solr.common.params.DisMaxParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
@@ -75,7 +74,9 @@ public class DisMaxQParser extends QPars
      */
     BooleanQuery query = new BooleanQuery(true);
 
-    addMainQuery(query, solrParams);
+    boolean notBlank = addMainQuery(query, solrParams);
+    if (!notBlank)
+      return null;
     addBoostQuery(query, solrParams);
     addBoostFunctions(query, solrParams);
 
@@ -134,7 +135,8 @@ public class DisMaxQParser extends QPars
     }
   }
 
-  protected void addMainQuery(BooleanQuery query, SolrParams solrParams) throws ParseException
{
+  /** Adds the main query to the query argument. If its blank then false is returned. */
+  protected boolean addMainQuery(BooleanQuery query, SolrParams solrParams) throws ParseException
{
     Map<String, Float> phraseFields = SolrPluginUtils.parseFieldBoosts(solrParams.getParams(DisMaxParams.PF));
     float tiebreaker = solrParams.getFloat(DisMaxParams.TIE, 0.0f);
 
@@ -153,6 +155,8 @@ public class DisMaxQParser extends QPars
     if (userQuery == null || userQuery.trim().length() < 1) {
       // If no query is specified, we may have an alternate
       altUserQuery = getAlternateUserQuery(solrParams);
+      if (altUserQuery == null)
+        return false;
       query.add(altUserQuery, BooleanClause.Occur.MUST);
     } else {
       // There is a valid query string
@@ -167,6 +171,7 @@ public class DisMaxQParser extends QPars
         query.add(phrase, BooleanClause.Occur.SHOULD);
       }
     }
+    return true;
   }
 
   protected Query getAlternateUserQuery(SolrParams solrParams) throws ParseException {
@@ -175,7 +180,7 @@ public class DisMaxQParser extends QPars
       QParser altQParser = subQuery(altQ, null);
       return altQParser.getQuery();
     } else {
-      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "missing query string");
+      return null;
     }
   }
 

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java
(original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/ExtendedDismaxQParserPlugin.java
Wed Mar 28 04:19:31 2012
@@ -27,14 +27,12 @@ import org.apache.lucene.queryParser.Que
 import org.apache.lucene.search.*;
 import org.apache.lucene.analysis.Analyzer;
 import org.apache.lucene.analysis.TokenStream;
-import org.apache.solr.common.SolrException;
 import org.apache.solr.search.SolrQueryParser.MagicFieldName;
 import org.apache.solr.common.params.DisMaxParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
 import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.schema.FieldType;
-import org.apache.solr.search.QueryUtils;
 import org.apache.solr.search.function.BoostedQuery;
 import org.apache.solr.search.function.FunctionQuery;
 import org.apache.solr.search.function.ProductFloatFunction;
@@ -166,7 +164,8 @@ class ExtendedDismaxQParser extends QPar
         altUserQuery = altQParser.getQuery();
         query.add( altUserQuery , BooleanClause.Occur.MUST );
       } else {
-        throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, "missing query string"
);
+        return null;
+        // throw new ParseException("missing query string" );
       }
     }
     else {     

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.java
(original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/LuceneQParserPlugin.java
Wed Mar 28 04:19:31 2012
@@ -20,7 +20,6 @@ import org.apache.lucene.queryParser.Par
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Sort;
-import org.apache.solr.common.SolrException;
 import org.apache.solr.common.params.CommonParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
@@ -49,7 +48,6 @@ public class LuceneQParserPlugin extends
 }
 
 class LuceneQParser extends QParser {
-  String sortStr;
   SolrQueryParser lparser;
 
   public LuceneQParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest
req) {
@@ -60,6 +58,7 @@ class LuceneQParser extends QParser {
   @Override
   public Query parse() throws ParseException {
     String qstr = getString();
+    if (qstr == null || qstr.length()==0) return null;
 
     String defaultField = getParam(CommonParams.DF);
     if (defaultField==null) {
@@ -83,7 +82,7 @@ class LuceneQParser extends QParser {
 
   @Override
   public String[] getDefaultHighlightFields() {
-    return new String[]{lparser.getField()};
+    return lparser == null ? new String[]{} : new String[]{lparser.getField()};
   }
   
 }
@@ -101,6 +100,8 @@ class OldLuceneQParser extends LuceneQPa
     // handle legacy "query;sort" syntax
     if (getLocalParams() == null) {
       String qstr = getString();
+      if (qstr == null || qstr.length() == 0)
+        return null;
       sortStr = getParams().get(CommonParams.SORT);
       if (sortStr == null) {
         // sort may be legacy form, included in the query string
@@ -113,7 +114,7 @@ class OldLuceneQParser extends LuceneQPa
           qstr = commands.get(0);
         }
         else if (commands.size() > 2) {
-          throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "If you want to use
multiple ';' in the query, use the 'sort' param.");
+          throw new ParseException("If you want to use multiple ';' in the query, use the
'sort' param.");
         }
       }
       setString(qstr);

Modified: lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/QParser.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/QParser.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/QParser.java (original)
+++ lucene/dev/branches/branch_3x/solr/core/src/java/org/apache/solr/search/QParser.java Wed
Mar 28 04:19:31 2012
@@ -94,7 +94,8 @@ public abstract class QParser {
     lst.add(val);
   }
 
-  /** Create and return the <code>Query</code> object represented by <code>qstr</code>
+  /** Create and return the <code>Query</code> object represented by <code>qstr</code>.
 Null MAY be returned to signify
+   * there was no input (e.g. no query string) to parse.
    * @see #getQuery()
    **/
   public abstract Query parse() throws ParseException;

Modified: lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/search/QueryParsingTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/search/QueryParsingTest.java?rev=1306151&r1=1306150&r2=1306151&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/search/QueryParsingTest.java
(original)
+++ lucene/dev/branches/branch_3x/solr/core/src/test/org/apache/solr/search/QueryParsingTest.java
Wed Mar 28 04:19:31 2012
@@ -16,6 +16,7 @@ package org.apache.solr.search;
  * limitations under the License.
  */
 
+import org.apache.lucene.search.Query;
 import org.apache.lucene.search.Sort;
 import org.apache.lucene.search.SortField;
 import org.apache.solr.SolrTestCaseJ4;
@@ -35,6 +36,40 @@ public class QueryParsingTest extends So
     initCore("solrconfig.xml","schema.xml");
   }
 
+  /**
+   * Test that the main QParserPlugins people are likely to use
+   * as defaults fail with a consistent exception when the query string 
+   * is either empty or null.
+   * @see <a href="https://issues.apache.org/jira/browse/SOLR-435">SOLR-435</a>
+   * @see <a href="https://issues.apache.org/jira/browse/SOLR-2001">SOLR-2001</a>
+   */
+  public void testQParserEmptyInput() throws Exception {
+    
+    SolrQueryRequest req = req();
+    
+    final String[] parsersTested = new String[] {
+      OldLuceneQParserPlugin.NAME,
+      LuceneQParserPlugin.NAME,
+      DisMaxQParserPlugin.NAME,
+      ExtendedDismaxQParserPlugin.NAME
+    };
+
+    for (String defType : parsersTested) {
+      for (String qstr : new String[] {null, ""}) {
+        QParser parser = null;
+        try {
+          parser = QParser.getParser(qstr, defType, req);
+        } catch (Exception e) {
+          throw new RuntimeException("getParser excep using defType=" + 
+                                     defType + " with qstr="+qstr, e);
+        }
+        
+        Query q = parser.parse();
+        assertNull("expected no query",q);
+      }
+    }
+  }
+  
   @Test
   public void testSort() throws Exception {
     Sort sort;



Mime
View raw message