You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-commits@lucene.apache.org by ho...@apache.org on 2007/02/22 01:22:11 UTC

svn commit: r510322 - in /lucene/solr/trunk: ./ example/solr/conf/ src/java/org/apache/solr/request/ src/java/org/apache/solr/util/ src/test/org/apache/solr/ src/test/test-files/solr/conf/

Author: hossman
Date: Wed Feb 21 16:22:10 2007
New Revision: 510322

URL: http://svn.apache.org/viewvc?view=rev&rev=510322
Log:
SOLR-152 - support for q.alt in dismax handler so requests without query strings can return results based on whatever alternate query is configured

Modified:
    lucene/solr/trunk/CHANGES.txt
    lucene/solr/trunk/example/solr/conf/solrconfig.xml
    lucene/solr/trunk/src/java/org/apache/solr/request/DisMaxRequestHandler.java
    lucene/solr/trunk/src/java/org/apache/solr/util/DisMaxParams.java
    lucene/solr/trunk/src/test/org/apache/solr/DisMaxRequestHandlerTest.java
    lucene/solr/trunk/src/test/test-files/solr/conf/solrconfig.xml

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Wed Feb 21 16:22:10 2007
@@ -110,6 +110,12 @@
 13. SOLR-86: Added standalone Java-based command-line updater.
     (Erik Hatcher via Bertrand Delecretaz)
 
+14. SOLR-152: DisMaxRequestHandler now supports configurable alternate
+    behavior when q is not specified.  A "q.alt" param can be specified
+    using SolrQueryParser syntax as a mechanism for specifying what query
+    the dismax handler should execute if the main user query (q) is blank.
+    (Ryan McKinley via hossman)
+    
 Changes in runtime behavior
  1. Highlighting using DisMax will only pick up terms from the main 
     user query, not boost or filter queries (klaas).

Modified: lucene/solr/trunk/example/solr/conf/solrconfig.xml
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/example/solr/conf/solrconfig.xml?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/example/solr/conf/solrconfig.xml (original)
+++ lucene/solr/trunk/example/solr/conf/solrconfig.xml Wed Feb 21 16:22:10 2007
@@ -270,6 +270,7 @@
         2<-1 5<-2 6<90%
      </str>
      <int name="ps">100</int>
+     <str name="q.alt">*:*</str>
     </lst>
   </requestHandler>
 

Modified: lucene/solr/trunk/src/java/org/apache/solr/request/DisMaxRequestHandler.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/request/DisMaxRequestHandler.java?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/request/DisMaxRequestHandler.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/request/DisMaxRequestHandler.java Wed Feb 21 16:22:10 2007
@@ -30,6 +30,7 @@
 import org.apache.lucene.queryParser.QueryParser;
 import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.solr.core.SolrCore;
@@ -60,6 +61,12 @@
  * </p>
  *
  * <ul>
+ * <li>q.alt - An alternate query to be used in cases where the main
+ *             query (q) is not specified (or blank).  This query should
+ *             be expressed in the Standard SolrQueryParser syntax (you
+ *             can use <code>q.alt=*:*</code> to denote that all documents
+ *             should be returned when no query is specified)
+ * </li>
  * <li>tie - (Tie breaker) float value to use as tiebreaker in
  *           DisjunctionMaxQueries (should be something much less than 1)
  * </li>
@@ -189,42 +196,58 @@
       pp.setPhraseSlop(pslop);
             
             
-      /* * * Main User Query * * */
-
-      String userQuery = U.partialEscape
-        (U.stripUnbalancedQuotes(params.get(Q))).toString();
-            
       /* the main query we will execute.  we disable the coord because
        * this query is an artificial construct
        */
       BooleanQuery query = new BooleanQuery(true);
 
-      String minShouldMatch = params.get(DMP.MM, "100%");
-      Query dis = up.parse(userQuery);
-      Query parsedUserQuery = dis;
-
-      if (dis instanceof BooleanQuery) {
-        BooleanQuery t = new BooleanQuery();
-        U.flattenBooleanQuery(t, (BooleanQuery)dis);
-        U.setMinShouldMatch(t, minShouldMatch);                
-        parsedUserQuery = t;
-      } 
-      query.add(parsedUserQuery, Occur.MUST);
-
-      /* * * Add on Phrases for the Query * * */
+      /* * * Main User Query * * */
+      Query parsedUserQuery = null;
+      String userQuery = params.get( Q );
+      Query altUserQuery = null;
+      if( userQuery == null || userQuery.trim().length() < 1 ) {
+        // If no query is specified, we may have an alternate
+        String altQ = params.get( DMP.ALTQ );
+        if (altQ != null) {
+          altUserQuery = p.parse(altQ);
+          query.add( altUserQuery , Occur.MUST );
+        } else {
+          throw new SolrException( 400, "missing query string" );
+        }
+      }
+      else {
+        // There is a valid query string
+        userQuery = U.partialEscape(U.stripUnbalancedQuotes(userQuery)).toString();
             
-      /* build up phrase boosting queries */
-
-      /* if the userQuery already has some quotes, stip them out.
-       * we've already done the phrases they asked for in the main
-       * part of the query, this is to boost docs that may not have
-       * matched those phrases but do match looser phrases.
-       */
-      String userPhraseQuery = userQuery.replace("\"","");
-      Query phrase = pp.parse("\"" + userPhraseQuery + "\"");
-      if (null != phrase) {
-        query.add(phrase, Occur.SHOULD);
+        String minShouldMatch = params.get(DMP.MM, "100%");
+        Query dis = up.parse(userQuery);
+        parsedUserQuery = dis;
+  
+        if (dis instanceof BooleanQuery) {
+          BooleanQuery t = new BooleanQuery();
+          U.flattenBooleanQuery(t, (BooleanQuery)dis);
+          U.setMinShouldMatch(t, minShouldMatch);                
+          parsedUserQuery = t;
+        } 
+        query.add(parsedUserQuery, Occur.MUST);
+        
+
+        /* * * Add on Phrases for the Query * * */
+              
+        /* build up phrase boosting queries */
+
+        /* if the userQuery already has some quotes, stip them out.
+         * we've already done the phrases they asked for in the main
+         * part of the query, this is to boost docs that may not have
+         * matched those phrases but do match looser phrases.
+         */
+        String userPhraseQuery = userQuery.replace("\"","");
+        Query phrase = pp.parse("\"" + userPhraseQuery + "\"");
+        if (null != phrase) {
+          query.add(phrase, Occur.SHOULD);
+        }
       }
+
             
       /* * * Boosting Query * * */
 
@@ -289,6 +312,7 @@
       try {
         NamedList debug = U.doStandardDebug(req, userQuery, query, results.docList);
         if (null != debug) {
+          debug.add("altquerystring", altUserQuery);
           debug.add("boostquery", boostQuery);
           debug.add("boostfunc", boostFunc);
           if (null != restrictions) {
@@ -309,7 +333,7 @@
       }
 
       /* * * Highlighting/Summarizing  * * */
-      if(HighlightingUtils.isHighlightingEnabled(req)) {
+      if(HighlightingUtils.isHighlightingEnabled(req) && parsedUserQuery != null) {
         String[] highFields = queryFields.keySet().toArray(new String[0]);
         NamedList sumData =
           HighlightingUtils.doHighlighting(results.docList, parsedUserQuery, 
@@ -347,17 +371,17 @@
 
 	@Override
 	public String getVersion() {
-	    return "$Revision:$";
+	    return "$Revision$";
 	}
 
 	@Override
 	public String getSourceId() {
-	  return "$Id:$";
+	  return "$Id$";
 	}
 
 	@Override
 	public String getSource() {
-	  return "$URL:$";
+	  return "$URL$";
 	}
   
   @Override

Modified: lucene/solr/trunk/src/java/org/apache/solr/util/DisMaxParams.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/java/org/apache/solr/util/DisMaxParams.java?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/src/java/org/apache/solr/util/DisMaxParams.java (original)
+++ lucene/solr/trunk/src/java/org/apache/solr/util/DisMaxParams.java Wed Feb 21 16:22:10 2007
@@ -61,6 +61,11 @@
     public static String BQ = "bq";
     /** query and init param for boosting functions */
     public static String BF = "bf";
+    /**
+     * Alternate query (expressed in Solr QuerySyntax)
+     * to use if main query (q) is empty
+     */
+    public static String ALTQ = "q.alt";
     /** query and init param for filtering query
      * @deprecated use SolrParams.FQ or SolrPluginUtils.parseFilterQueries
      */

Modified: lucene/solr/trunk/src/test/org/apache/solr/DisMaxRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/DisMaxRequestHandlerTest.java?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/DisMaxRequestHandlerTest.java (original)
+++ lucene/solr/trunk/src/test/org/apache/solr/DisMaxRequestHandlerTest.java Wed Feb 21 16:22:10 2007
@@ -107,7 +107,20 @@
             ,"//*[@numFound='3']"
             );
 
-
+    assertQ("relying on ALTQ from config",
+            req( "qt", "dismax",
+                 "fq", "id:666",
+                 "facet", "false" )
+            ,"//*[@numFound='1']"
+            );
+    
+    assertQ("explicit ALTQ",
+            req( "qt", "dismax",
+                 "q.alt", "id:blahbalh",
+                 "fq", "id:666",
+                 "facet", "false" )
+            ,"//*[@numFound='0']"
+            );
   }
 
   public void testOldStyleDefaults() throws Exception {

Modified: lucene/solr/trunk/src/test/test-files/solr/conf/solrconfig.xml
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/test-files/solr/conf/solrconfig.xml?view=diff&rev=510322&r1=510321&r2=510322
==============================================================================
--- lucene/solr/trunk/src/test/test-files/solr/conf/solrconfig.xml (original)
+++ lucene/solr/trunk/src/test/test-files/solr/conf/solrconfig.xml Wed Feb 21 16:22:10 2007
@@ -215,6 +215,7 @@
   </requestHandler>
   <requestHandler name="dismax" class="solr.DisMaxRequestHandler" >
     <lst name="defaults">
+     <str name="q.alt">*:*</str>
      <float name="tie">0.01</float>
      <str name="qf">
         text^0.5 features_t^1.0 subject^1.4 title_stemmed^2.0