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

@ -186,6 +186,8 @@ New Features
(ehatcher) (ehatcher)
* SOLR-1974: Add LimitTokenCountFilterFactory. (koji) * SOLR-1974: Add LimitTokenCountFilterFactory. (koji)
* SOLR-1966: QueryElevationComponent can now return just the included results in the elevation file (gsingers, yonik)
Optimizations 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.List;
import java.util.Map; import java.util.Map;
import java.util.WeakHashMap; import java.util.WeakHashMap;
import org.apache.solr.common.params.QueryElevationParams;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -77,22 +79,20 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
// Constants used in solrconfig.xml // Constants used in solrconfig.xml
static final String FIELD_TYPE = "queryFieldType"; static final String FIELD_TYPE = "queryFieldType";
static final String CONFIG_FILE = "config-file"; static final String CONFIG_FILE = "config-file";
static final String FORCE_ELEVATION = "forceElevation";
static final String EXCLUDE = "exclude"; static final String EXCLUDE = "exclude";
// Runtime param -- should be in common? // Runtime param -- should be in common?
static final String ENABLE = "enableElevation";
private SolrParams initArgs = null; private SolrParams initArgs = null;
private Analyzer analyzer = null; private Analyzer analyzer = null;
private String idField = null; private String idField = null;
boolean forceElevation = false; boolean forceElevation = false;
// For each IndexReader, keep a query->elevation map // For each IndexReader, keep a query->elevation map
// When the configuration is loaded from the data directory. // When the configuration is loaded from the data directory.
// The key is null if loaded from the config directory, and // The key is null if loaded from the config directory, and
// is never re-loaded. // is never re-loaded.
final Map<IndexReader,Map<String, ElevationObj>> elevationCache = final Map<IndexReader,Map<String, ElevationObj>> elevationCache =
new WeakHashMap<IndexReader, Map<String,ElevationObj>>(); new WeakHashMap<IndexReader, Map<String,ElevationObj>>();
class ElevationObj { class ElevationObj {
@ -160,7 +160,7 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
} }
idField = StringHelper.intern(sf.getName()); idField = StringHelper.intern(sf.getName());
forceElevation = initArgs.getBool( FORCE_ELEVATION, forceElevation ); forceElevation = initArgs.getBool( QueryElevationParams.FORCE_ELEVATION, forceElevation );
try { try {
synchronized( elevationCache ) { synchronized( elevationCache ) {
elevationCache.clear(); elevationCache.clear();
@ -316,12 +316,13 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
SolrQueryRequest req = rb.req; SolrQueryRequest req = rb.req;
SolrParams params = req.getParams(); SolrParams params = req.getParams();
// A runtime param can skip // A runtime param can skip
if( !params.getBool( ENABLE, true ) ) { if( !params.getBool( QueryElevationParams.ENABLE, true ) ) {
return; return;
} }
boolean exclusive = params.getBool(QueryElevationParams.EXCLUSIVE, false);
// A runtime parameter can alter the config value for forceElevation // 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(); Query query = rb.getQuery();
String qstr = rb.getQueryString(); String qstr = rb.getQueryString();
@ -342,15 +343,21 @@ public class QueryElevationComponent extends SearchComponent implements SolrCore
if( booster != null ) { if( booster != null ) {
// Change the query to insert forced documents // Change the query to insert forced documents
BooleanQuery newq = new BooleanQuery( true ); if (exclusive == true){
newq.add( query, BooleanClause.Occur.SHOULD ); //we only want these results
newq.add( booster.include, BooleanClause.Occur.SHOULD ); rb.setQuery(booster.include);
if( booster.exclude != null ) { } else {
for( BooleanClause bq : booster.exclude ) { BooleanQuery newq = new BooleanQuery( true );
newq.add( bq ); 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 // if the sort is 'score desc' use a custom sorting method to
// insert documents in their proper place // 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.SolrTestCaseJ4;
import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.MapSolrParams; 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.common.util.NamedList;
import org.apache.solr.core.SolrCore; import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.QueryElevationComponent.ElevationObj; import org.apache.solr.handler.component.QueryElevationComponent.ElevationObj;
@ -198,10 +199,19 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {
,"//result/doc[3]/str[@name='id'][.='b']" ,"//result/doc[3]/str[@name='id'][.='b']"
,"//result/doc[4]/str[@name='id'][.='c']" ,"//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 // Test exclusion
booster.elevationCache.clear(); booster.elevationCache.clear();
args.remove( CommonParams.SORT ); args.remove( CommonParams.SORT );
args.remove( QueryElevationParams.EXCLUSIVE);
booster.setTopQueryResults( reader, query, new String[] { "x" }, new String[] { "a" } ); booster.setTopQueryResults( reader, query, new String[] { "x" }, new String[] { "a" } );
assertQ( null, req assertQ( null, req
,"//*[@numFound='3']" ,"//*[@numFound='3']"
@ -209,6 +219,7 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {
,"//result/doc[2]/str[@name='id'][.='b']" ,"//result/doc[2]/str[@name='id'][.='b']"
,"//result/doc[3]/str[@name='id'][.='c']" ,"//result/doc[3]/str[@name='id'][.='c']"
); );
} }
// write a test file to boost some docs // write a test file to boost some docs