Apache Solr open-source search software
SOLR-152 - support for q.alt in dismax handler so requests without query strings can return results based on whatever alternate query is configured
Chris M. Hostettera76bf773c6de
diff --git a/CHANGES.txt b/CHANGES.txtindex 8d1e749679f..ebfcdfe3772 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -110,6 +110,12 @@ New Features13. 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 behavior1. Highlighting using DisMax will only pick up terms from the mainuser query, not boost or filter queries (klaas).diff --git a/example/solr/conf/solrconfig.xml b/example/solr/conf/solrconfig.xmlindex b7ee00116fc..8d435a9b790 100755--- a/example/solr/conf/solrconfig.xml+++ b/example/solr/conf/solrconfig.xml@@ -270,6 +270,7 @@2<-1 5<-2 6<90%</str><int name="ps">100</int>+ <str name="q.alt">*:*</str></lst></requestHandler>diff --git a/src/java/org/apache/solr/request/DisMaxRequestHandler.java b/src/java/org/apache/solr/request/DisMaxRequestHandler.javaindex ccb43292c84..6a9dec4c2b0 100644--- a/src/java/org/apache/solr/request/DisMaxRequestHandler.java+++ b/src/java/org/apache/solr/request/DisMaxRequestHandler.java@@ -30,6 +30,7 @@ import java.util.Map;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 @@ import org.apache.solr.util.SolrPluginUtils;* </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 @@ public class DisMaxRequestHandler extends RequestHandlerBase {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 @@ public class DisMaxRequestHandler extends RequestHandlerBase {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 @@ public class DisMaxRequestHandler extends RequestHandlerBase {}/* * * 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 @@ public class DisMaxRequestHandler extends RequestHandlerBase {@Overridepublic String getVersion() {- return "$Revision:$";+ return "$Revision$";}@Overridepublic String getSourceId() {- return "$Id:$";+ return "$Id$";}@Overridepublic String getSource() {- return "$URL:$";+ return "$URL$";}@Overridediff --git a/src/java/org/apache/solr/util/DisMaxParams.java b/src/java/org/apache/solr/util/DisMaxParams.javaindex 0c1be44a51a..ff8427a2183 100755--- a/src/java/org/apache/solr/util/DisMaxParams.java+++ b/src/java/org/apache/solr/util/DisMaxParams.java@@ -61,6 +61,11 @@ import java.io.IOException;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*/diff --git a/src/test/org/apache/solr/DisMaxRequestHandlerTest.java b/src/test/org/apache/solr/DisMaxRequestHandlerTest.javaindex 2a8f2d79e84..67976c379db 100644--- a/src/test/org/apache/solr/DisMaxRequestHandlerTest.java+++ b/src/test/org/apache/solr/DisMaxRequestHandlerTest.java@@ -107,7 +107,20 @@ public class DisMaxRequestHandlerTest extends AbstractSolrTestCase {,"//*[@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 {diff --git a/src/test/test-files/solr/conf/solrconfig.xml b/src/test/test-files/solr/conf/solrconfig.xmlindex 1030b26fd7a..c0d6d544f70 100644--- a/src/test/test-files/solr/conf/solrconfig.xml+++ b/src/test/test-files/solr/conf/solrconfig.xml@@ -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