From a76bf773c6de50c6734d769a000f214eb6cbbd21 Mon Sep 17 00:00:00 2001
From: "Chris M. Hostetter"
Date: Thu, 22 Feb 2007 00:22:10 +0000
Subject: [PATCH] SOLR-152 - support for q.alt in dismax handler so requests
without query strings can return results based on whatever alternate query is
configured
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@510322 13f79535-47bb-0310-9956-ffa450edef68
---
CHANGES.txt | 6 ++
example/solr/conf/solrconfig.xml | 1 +
.../solr/request/DisMaxRequestHandler.java | 92 ++++++++++++-------
.../org/apache/solr/util/DisMaxParams.java | 5 +
.../apache/solr/DisMaxRequestHandlerTest.java | 15 ++-
src/test/test-files/solr/conf/solrconfig.xml | 1 +
6 files changed, 85 insertions(+), 35 deletions(-)
diff --git a/CHANGES.txt b/CHANGES.txt
index 8d1e749679f..ebfcdfe3772 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -110,6 +110,12 @@ New Features
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).
diff --git a/example/solr/conf/solrconfig.xml b/example/solr/conf/solrconfig.xml
index 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%
100
+ *:*
diff --git a/src/java/org/apache/solr/request/DisMaxRequestHandler.java b/src/java/org/apache/solr/request/DisMaxRequestHandler.java
index 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;
*
*
*
+ * - 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
q.alt=*:*
to denote that all documents
+ * should be returned when no query is specified)
+ *
* - tie - (Tie breaker) float value to use as tiebreaker in
* DisjunctionMaxQueries (should be something much less than 1)
*
@@ -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 * * */
-
- /* 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);
+ /* * * 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();
+
+ 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 {
@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
diff --git a/src/java/org/apache/solr/util/DisMaxParams.java b/src/java/org/apache/solr/util/DisMaxParams.java
index 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.java
index 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.xml
index 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 @@
+ *:*
0.01
text^0.5 features_t^1.0 subject^1.4 title_stemmed^2.0