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
This commit is contained in:
Chris M. Hostetter 2007-02-22 00:22:10 +00:00
parent 85e27f07de
commit a76bf773c6
6 changed files with 85 additions and 35 deletions

View File

@ -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).

View File

@ -270,6 +270,7 @@
2<-1 5<-2 6<90%
</str>
<int name="ps">100</int>
<str name="q.alt">*:*</str>
</lst>
</requestHandler>

View File

@ -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 * * */
/* 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

View File

@ -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
*/

View File

@ -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 {

View File

@ -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