SOLR-1966: QueryElevationComponent can now optionally return only the elevated results

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@959434 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Grant Ingersoll 2010-06-30 21:12:23 +00:00
parent b2f3f2edff
commit d69d138e4c
4 changed files with 49 additions and 16 deletions

View File

@ -187,6 +187,8 @@ New Features
* SOLR-1974: Add LimitTokenCountFilterFactory. (koji)
* SOLR-1966: QueryElevationComponent can now return just the included results in the elevation file (gsingers, yonik)
Optimizations
----------------------

View File

@ -0,0 +1,13 @@
package org.apache.solr.common.params;
/**
* Parameters used with the QueryElevationComponent
*
**/
public interface QueryElevationParams {
String ENABLE = "enableElevation";
String EXCLUSIVE = "exclusive";
String FORCE_ELEVATION = "forceElevation";
}

View File

@ -29,6 +29,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
import org.apache.solr.common.params.QueryElevationParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -77,17 +79,15 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
// Constants used in solrconfig.xml
static final String FIELD_TYPE = "queryFieldType";
static final String CONFIG_FILE = "config-file";
static final String FORCE_ELEVATION = "forceElevation";
static final String EXCLUDE = "exclude";
// Runtime param -- should be in common?
static final String ENABLE = "enableElevation";
private SolrParams initArgs = null;
private Analyzer analyzer = null;
private String idField = null;
boolean forceElevation = false;
boolean forceElevation = false;
// For each IndexReader, keep a query->elevation map
// When the configuration is loaded from the data directory.
// The key is null if loaded from the config directory, and
@ -160,7 +160,7 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
}
idField = StringHelper.intern(sf.getName());
forceElevation = initArgs.getBool( FORCE_ELEVATION, forceElevation );
forceElevation = initArgs.getBool( QueryElevationParams.FORCE_ELEVATION, forceElevation );
try {
synchronized( elevationCache ) {
elevationCache.clear();
@ -316,12 +316,13 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
SolrQueryRequest req = rb.req;
SolrParams params = req.getParams();
// A runtime param can skip
if( !params.getBool( ENABLE, true ) ) {
if( !params.getBool( QueryElevationParams.ENABLE, true ) ) {
return;
}
boolean exclusive = params.getBool(QueryElevationParams.EXCLUSIVE, false);
// A runtime parameter can alter the config value for forceElevation
boolean force = params.getBool( FORCE_ELEVATION, forceElevation );
boolean force = params.getBool( QueryElevationParams.FORCE_ELEVATION, forceElevation );
Query query = rb.getQuery();
String qstr = rb.getQueryString();
@ -342,15 +343,21 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
if( booster != null ) {
// Change the query to insert forced documents
BooleanQuery newq = new BooleanQuery( true );
newq.add( query, BooleanClause.Occur.SHOULD );
newq.add( booster.include, BooleanClause.Occur.SHOULD );
if( booster.exclude != null ) {
for( BooleanClause bq : booster.exclude ) {
newq.add( bq );
if (exclusive == true){
//we only want these results
rb.setQuery(booster.include);
} else {
BooleanQuery newq = new BooleanQuery( true );
newq.add( query, BooleanClause.Occur.SHOULD );
newq.add( booster.include, BooleanClause.Occur.SHOULD );
if( booster.exclude != null ) {
for( BooleanClause bq : booster.exclude ) {
newq.add( bq );
}
}
rb.setQuery( newq );
}
rb.setQuery( newq );
// if the sort is 'score desc' use a custom sorting method to
// insert documents in their proper place

View File

@ -29,6 +29,7 @@ import org.apache.lucene.util.BytesRef;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MapSolrParams;
import org.apache.solr.common.params.QueryElevationParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.QueryElevationComponent.ElevationObj;
@ -199,9 +200,18 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {
,"//result/doc[4]/str[@name='id'][.='c']"
);
//Test exclusive (not to be confused with exclusion)
args.put(QueryElevationParams.EXCLUSIVE, "true");
booster.setTopQueryResults( reader, query, new String[] { "x" }, new String[] {} );
assertQ( null, req
,"//*[@numFound='1']"
,"//result/doc[1]/str[@name='id'][.='x']"
);
// Test exclusion
booster.elevationCache.clear();
args.remove( CommonParams.SORT );
args.remove( QueryElevationParams.EXCLUSIVE);
booster.setTopQueryResults( reader, query, new String[] { "x" }, new String[] { "a" } );
assertQ( null, req
,"//*[@numFound='3']"
@ -209,6 +219,7 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {
,"//result/doc[2]/str[@name='id'][.='b']"
,"//result/doc[3]/str[@name='id'][.='c']"
);
}
// write a test file to boost some docs