You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by gs...@apache.org on 2011/12/13 19:22:39 UTC
svn commit: r1213824 - in /lucene/dev/trunk/solr/core/src:
java/org/apache/solr/handler/component/QueryElevationComponent.java
test-files/solr/conf/elevate.xml
test/org/apache/solr/handler/component/QueryElevationComponentTest.java
Author: gsingers
Date: Tue Dec 13 18:22:39 2011
New Revision: 1213824
URL: http://svn.apache.org/viewvc?rev=1213824&view=rev
Log:
SOLR-1520: added tests
Modified:
lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java
lucene/dev/trunk/solr/core/src/test-files/solr/conf/elevate.xml
lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java
Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java?rev=1213824&r1=1213823&r2=1213824&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/handler/component/QueryElevationComponent.java Tue Dec 13 18:22:39 2011
@@ -84,6 +84,7 @@ public class QueryElevationComponent ext
private SolrParams initArgs = null;
private Analyzer analyzer = null;
private String idField = null;
+ private FieldType idSchemaFT;
boolean forceElevation = false;
// For each IndexReader, keep a query->elevation map
@@ -115,6 +116,7 @@ public class QueryElevationComponent ext
this.priority = new HashMap<BytesRef, Integer>();
int max = elevate.size()+5;
for( String id : elevate ) {
+ id = idSchemaFT.readableToIndexed(id);
ids.add(id);
TermQuery tq = new TermQuery( new Term( idField, id ) );
include.add( tq, BooleanClause.Occur.SHOULD );
@@ -127,7 +129,7 @@ public class QueryElevationComponent ext
else {
this.exclude = new BooleanClause[exclude.size()];
for( int i=0; i<exclude.size(); i++ ) {
- TermQuery tq = new TermQuery( new Term( idField, exclude.get(i) ) );
+ TermQuery tq = new TermQuery( new Term( idField, idSchemaFT.readableToIndexed(exclude.get(i)) ) );
this.exclude[i] = new BooleanClause( tq, BooleanClause.Occur.MUST_NOT );
}
}
@@ -155,10 +157,11 @@ public class QueryElevationComponent ext
}
SchemaField sf = core.getSchema().getUniqueKeyField();
- if( sf == null || !(sf.getType() instanceof StrField)) {
+ if( sf == null || sf.getType().isTokenized() == true) {
throw new SolrException( SolrException.ErrorCode.SERVER_ERROR,
- "QueryElevationComponent requires the schema to have a uniqueKeyField implemented using StrField" );
+ "QueryElevationComponent requires the schema to have a uniqueKeyField implemented using a non-tokenized field" );
}
+ idSchemaFT = sf.getType();
idField = sf.getName();
//register the EditorialMarkerFactory
EditorialMarkerFactory factory = new EditorialMarkerFactory();
Modified: lucene/dev/trunk/solr/core/src/test-files/solr/conf/elevate.xml
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test-files/solr/conf/elevate.xml?rev=1213824&r1=1213823&r2=1213824&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test-files/solr/conf/elevate.xml (original)
+++ lucene/dev/trunk/solr/core/src/test-files/solr/conf/elevate.xml Tue Dec 13 18:22:39 2011
@@ -32,5 +32,11 @@
<doc id="2" />
<doc id="3" />
</query>
-
+
+ <query text="AAAA">
+ <doc id="7.0" />
+ </query>
+
+
+
</elevate>
Modified: lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java?rev=1213824&r1=1213823&r2=1213824&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java (original)
+++ lucene/dev/trunk/solr/core/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java Tue Dec 13 18:22:39 2011
@@ -36,6 +36,7 @@ import org.apache.solr.core.SolrCore;
import org.apache.solr.handler.component.QueryElevationComponent.ElevationObj;
import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.SolrQueryRequest;
+import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -44,237 +45,281 @@ import org.junit.Test;
public class QueryElevationComponentTest extends SolrTestCaseJ4 {
- @BeforeClass
- public static void beforeClass() throws Exception {
+
+ @Before
+ @Override
+ public void setUp() throws Exception{
+ super.setUp();
+ }
+
+ private void init(String schema) throws Exception {
//write out elevate-data.xml to the Data dir first by copying it from conf, which we know exists, this way we can test both conf and data configurations
createTempDir();
File parent = new File(TEST_HOME(), "conf");
File elevateFile = new File(parent, "elevate.xml");
File elevateDataFile = new File(dataDir, "elevate-data.xml");
FileUtils.copyFile(elevateFile, elevateDataFile);
- initCore("solrconfig-elevate.xml", "schema12.xml");
- }
- @Before
- @Override
- public void setUp() throws Exception{
- super.setUp();
+ initCore("solrconfig-elevate.xml",schema);
clearIndex();
assertU(commit());
- assertU(optimize());
- // make sure this component is initialized correctly for each test
- QueryElevationComponent comp = (QueryElevationComponent)h.getCore().getSearchComponent("elevate");
- NamedList<String> args = new NamedList<String>();
- args.add( QueryElevationComponent.CONFIG_FILE, "elevate.xml" );
- args.add( QueryElevationComponent.FIELD_TYPE, "string" );
- comp.init( args );
- comp.inform( h.getCore() );
- comp.forceElevation = false;
}
-
+
+ private void delete() throws Exception {
+ deleteCore();
+ }
+
+ @Test
+ public void testFieldType() throws Exception {
+ try {
+ init("schema11.xml");
+ clearIndex();
+ assertU(commit());
+ assertU(adoc("id", "1", "text", "XXXX XXXX", "str_s", "a" ));
+ assertU(adoc("id", "2", "text", "YYYY", "str_s", "b" ));
+ assertU(adoc("id", "3", "text", "ZZZZ", "str_s", "c" ));
+
+ assertU(adoc("id", "4", "text", "XXXX XXXX", "str_s", "x" ));
+ assertU(adoc("id", "5", "text", "YYYY YYYY", "str_s", "y" ));
+ assertU(adoc("id", "6", "text", "XXXX XXXX", "str_s", "z" ));
+ assertU(adoc("id", "7", "text", "AAAA", "str_s", "a" ));
+ assertU(adoc("id", "8", "text", "AAAA", "str_s", "a" ));
+ assertU(adoc("id", "9", "text", "AAAA AAAA", "str_s", "a" ));
+ assertU(commit());
+
+ assertQ("", req(CommonParams.Q, "AAAA", CommonParams.QT, "/elevate",
+ CommonParams.FL, "id, score, [elevated]")
+ ,"//*[@numFound='3']"
+ ,"//result/doc[1]/float[@name='id'][.='7.0']"
+ ,"//result/doc[2]/float[@name='id'][.='8.0']"
+ ,"//result/doc[3]/float[@name='id'][.='9.0']",
+ "//result/doc[1]/bool[@name='[elevated]'][.='true']",
+ "//result/doc[2]/bool[@name='[elevated]'][.='false']",
+ "//result/doc[3]/bool[@name='[elevated]'][.='false']"
+ );
+ } finally{
+ delete();
+ }
+ }
+
@Test
public void testInterface() throws Exception
{
- SolrCore core = h.getCore();
-
- NamedList<String> args = new NamedList<String>();
- args.add( QueryElevationComponent.FIELD_TYPE, "string" );
- args.add( QueryElevationComponent.CONFIG_FILE, "elevate.xml" );
-
- QueryElevationComponent comp = new QueryElevationComponent();
- comp.init( args );
- comp.inform( core );
-
- SolrQueryRequest req = req();
- IndexReader reader = req.getSearcher().getIndexReader();
- Map<String, ElevationObj> map = comp.getElevationMap( reader, core );
- req.close();
-
- // Make sure the boosts loaded properly
- assertEquals( 3, map.size() );
- assertEquals( 1, map.get( "XXXX" ).priority.size() );
- assertEquals( 2, map.get( "YYYY" ).priority.size() );
- assertEquals( 3, map.get( "ZZZZ" ).priority.size() );
- assertEquals( null, map.get( "xxxx" ) );
- assertEquals( null, map.get( "yyyy" ) );
- assertEquals( null, map.get( "zzzz" ) );
-
- // Now test the same thing with a lowercase filter: 'lowerfilt'
- args = new NamedList<String>();
- args.add( QueryElevationComponent.FIELD_TYPE, "lowerfilt" );
- args.add( QueryElevationComponent.CONFIG_FILE, "elevate.xml" );
-
- comp = new QueryElevationComponent();
- comp.init( args );
- comp.inform( core );
- map = comp.getElevationMap( reader, core );
- assertEquals( 3, map.size() );
- assertEquals( null, map.get( "XXXX" ) );
- assertEquals( null, map.get( "YYYY" ) );
- assertEquals( null, map.get( "ZZZZ" ) );
- assertEquals( 1, map.get( "xxxx" ).priority.size() );
- assertEquals( 2, map.get( "yyyy" ).priority.size() );
- assertEquals( 3, map.get( "zzzz" ).priority.size() );
-
- assertEquals( "xxxx", comp.getAnalyzedQuery( "XXXX" ) );
- assertEquals( "xxxxyyyy", comp.getAnalyzedQuery( "XXXX YYYY" ) );
-
- assertQ("Make sure QEC handles null queries", req("qt","/elevate", "q.alt","*:*", "defType","dismax"),
- "//*[@numFound='0']");
+ try {
+ init("schema12.xml");
+ SolrCore core = h.getCore();
+
+ NamedList<String> args = new NamedList<String>();
+ args.add( QueryElevationComponent.FIELD_TYPE, "string" );
+ args.add( QueryElevationComponent.CONFIG_FILE, "elevate.xml" );
+
+ QueryElevationComponent comp = new QueryElevationComponent();
+ comp.init( args );
+ comp.inform( core );
+
+ SolrQueryRequest req = req();
+ IndexReader reader = req.getSearcher().getIndexReader();
+ Map<String, ElevationObj> map = comp.getElevationMap( reader, core );
+ req.close();
+
+ // Make sure the boosts loaded properly
+ assertEquals( 4, map.size() );
+ assertEquals( 1, map.get( "XXXX" ).priority.size() );
+ assertEquals( 2, map.get( "YYYY" ).priority.size() );
+ assertEquals( 3, map.get( "ZZZZ" ).priority.size() );
+ assertEquals( null, map.get( "xxxx" ) );
+ assertEquals( null, map.get( "yyyy" ) );
+ assertEquals( null, map.get( "zzzz" ) );
+
+ // Now test the same thing with a lowercase filter: 'lowerfilt'
+ args = new NamedList<String>();
+ args.add( QueryElevationComponent.FIELD_TYPE, "lowerfilt" );
+ args.add( QueryElevationComponent.CONFIG_FILE, "elevate.xml" );
+
+ comp = new QueryElevationComponent();
+ comp.init( args );
+ comp.inform( core );
+ map = comp.getElevationMap( reader, core );
+ assertEquals( 4, map.size() );
+ assertEquals( null, map.get( "XXXX" ) );
+ assertEquals( null, map.get( "YYYY" ) );
+ assertEquals( null, map.get( "ZZZZ" ) );
+ assertEquals( 1, map.get( "xxxx" ).priority.size() );
+ assertEquals( 2, map.get( "yyyy" ).priority.size() );
+ assertEquals( 3, map.get( "zzzz" ).priority.size() );
+
+ assertEquals( "xxxx", comp.getAnalyzedQuery( "XXXX" ) );
+ assertEquals( "xxxxyyyy", comp.getAnalyzedQuery( "XXXX YYYY" ) );
+
+ assertQ("Make sure QEC handles null queries", req("qt","/elevate", "q.alt","*:*", "defType","dismax"),
+ "//*[@numFound='0']");
+ } finally {
+ delete();
+ }
}
@Test
public void testMarker() throws Exception {
- assertU(adoc("id", "1", "title", "XXXX XXXX", "str_s1", "a" ));
- assertU(adoc("id", "2", "title", "YYYY", "str_s1", "b" ));
- assertU(adoc("id", "3", "title", "ZZZZ", "str_s1", "c" ));
-
- assertU(adoc("id", "4", "title", "XXXX XXXX", "str_s1", "x" ));
- assertU(adoc("id", "5", "title", "YYYY YYYY", "str_s1", "y" ));
- assertU(adoc("id", "6", "title", "XXXX XXXX", "str_s1", "z" ));
- assertU(adoc("id", "7", "title", "AAAA", "str_s1", "a" ));
- assertU(commit());
-
- assertQ("", req(CommonParams.Q, "XXXX", CommonParams.QT, "/elevate",
- CommonParams.FL, "id, score, [elevated]")
- ,"//*[@numFound='3']"
- ,"//result/doc[1]/str[@name='id'][.='1']"
- ,"//result/doc[2]/str[@name='id'][.='4']"
- ,"//result/doc[3]/str[@name='id'][.='6']",
- "//result/doc[1]/bool[@name='[elevated]'][.='true']",
- "//result/doc[2]/bool[@name='[elevated]'][.='false']",
- "//result/doc[3]/bool[@name='[elevated]'][.='false']"
- );
-
- assertQ("", req(CommonParams.Q, "AAAA", CommonParams.QT, "/elevate",
- CommonParams.FL, "id, score, [elevated]")
- ,"//*[@numFound='1']"
- ,"//result/doc[1]/str[@name='id'][.='7']",
- "//result/doc[1]/bool[@name='[elevated]'][.='false']"
- );
-
- assertQ("", req(CommonParams.Q, "AAAA", CommonParams.QT, "/elevate",
- CommonParams.FL, "id, score, [elev]")
- ,"//*[@numFound='1']"
- ,"//result/doc[1]/str[@name='id'][.='7']",
- "not(//result/doc[1]/bool[@name='[elevated]'][.='false'])",
- "not(//result/doc[1]/bool[@name='[elev]'][.='false'])" // even though we asked for elev, there is no Transformer registered w/ that, so we shouldn't get a result
- );
+ try {
+ init("schema12.xml");
+ assertU(adoc("id", "1", "title", "XXXX XXXX", "str_s1", "a" ));
+ assertU(adoc("id", "2", "title", "YYYY", "str_s1", "b" ));
+ assertU(adoc("id", "3", "title", "ZZZZ", "str_s1", "c" ));
+
+ assertU(adoc("id", "4", "title", "XXXX XXXX", "str_s1", "x" ));
+ assertU(adoc("id", "5", "title", "YYYY YYYY", "str_s1", "y" ));
+ assertU(adoc("id", "6", "title", "XXXX XXXX", "str_s1", "z" ));
+ assertU(adoc("id", "7", "title", "AAAA", "str_s1", "a" ));
+ assertU(commit());
+
+ assertQ("", req(CommonParams.Q, "XXXX", CommonParams.QT, "/elevate",
+ CommonParams.FL, "id, score, [elevated]")
+ ,"//*[@numFound='3']"
+ ,"//result/doc[1]/str[@name='id'][.='1']"
+ ,"//result/doc[2]/str[@name='id'][.='4']"
+ ,"//result/doc[3]/str[@name='id'][.='6']",
+ "//result/doc[1]/bool[@name='[elevated]'][.='true']",
+ "//result/doc[2]/bool[@name='[elevated]'][.='false']",
+ "//result/doc[3]/bool[@name='[elevated]'][.='false']"
+ );
+
+ assertQ("", req(CommonParams.Q, "AAAA", CommonParams.QT, "/elevate",
+ CommonParams.FL, "id, score, [elevated]")
+ ,"//*[@numFound='1']"
+ ,"//result/doc[1]/str[@name='id'][.='7']",
+ "//result/doc[1]/bool[@name='[elevated]'][.='false']"
+ );
+
+ assertQ("", req(CommonParams.Q, "AAAA", CommonParams.QT, "/elevate",
+ CommonParams.FL, "id, score, [elev]")
+ ,"//*[@numFound='1']"
+ ,"//result/doc[1]/str[@name='id'][.='7']",
+ "not(//result/doc[1]/bool[@name='[elevated]'][.='false'])",
+ "not(//result/doc[1]/bool[@name='[elev]'][.='false'])" // even though we asked for elev, there is no Transformer registered w/ that, so we shouldn't get a result
+ );
+ } finally {
+ delete();
+ }
}
@Test
- public void testSorting() throws IOException
+ public void testSorting() throws Exception
{
- assertU(adoc("id", "a", "title", "ipod", "str_s1", "a" ));
- assertU(adoc("id", "b", "title", "ipod ipod", "str_s1", "b" ));
- assertU(adoc("id", "c", "title", "ipod ipod ipod", "str_s1", "c" ));
-
- assertU(adoc("id", "x", "title", "boosted", "str_s1", "x" ));
- assertU(adoc("id", "y", "title", "boosted boosted", "str_s1", "y" ));
- assertU(adoc("id", "z", "title", "boosted boosted boosted", "str_s1", "z" ));
- assertU(commit());
-
- String query = "title:ipod";
-
- Map<String,String> args = new HashMap<String, String>();
- args.put( CommonParams.Q, query );
- args.put( CommonParams.QT, "/elevate" );
- args.put( CommonParams.FL, "id,score" );
- args.put( "indent", "true" );
- //args.put( CommonParams.FL, "id,title,score" );
- SolrQueryRequest req = new LocalSolrQueryRequest( h.getCore(), new MapSolrParams( args) );
- IndexReader reader = req.getSearcher().getIndexReader();
- QueryElevationComponent booster = (QueryElevationComponent)req.getCore().getSearchComponent( "elevate" );
-
- assertQ("Make sure standard sort works as expected", req
- ,"//*[@numFound='3']"
- ,"//result/doc[1]/str[@name='id'][.='a']"
- ,"//result/doc[2]/str[@name='id'][.='b']"
- ,"//result/doc[3]/str[@name='id'][.='c']"
- );
-
- // Explicitly set what gets boosted
- booster.elevationCache.clear();
- booster.setTopQueryResults( reader, query, new String[] { "x", "y", "z" }, null );
-
-
- assertQ("All six should make it", req
- ,"//*[@numFound='6']"
- ,"//result/doc[1]/str[@name='id'][.='x']"
- ,"//result/doc[2]/str[@name='id'][.='y']"
- ,"//result/doc[3]/str[@name='id'][.='z']"
- ,"//result/doc[4]/str[@name='id'][.='a']"
- ,"//result/doc[5]/str[@name='id'][.='b']"
- ,"//result/doc[6]/str[@name='id'][.='c']"
- );
-
- booster.elevationCache.clear();
-
- // now switch the order:
- booster.setTopQueryResults( reader, query, new String[] { "a", "x" }, null );
- assertQ("All four should make it", req
- ,"//*[@numFound='4']"
- ,"//result/doc[1]/str[@name='id'][.='a']"
- ,"//result/doc[2]/str[@name='id'][.='x']"
- ,"//result/doc[3]/str[@name='id'][.='b']"
- ,"//result/doc[4]/str[@name='id'][.='c']"
- );
-
- // Test reverse sort
- args.put( CommonParams.SORT, "score asc" );
- assertQ("All four should make it", req
- ,"//*[@numFound='4']"
- ,"//result/doc[4]/str[@name='id'][.='a']"
- ,"//result/doc[3]/str[@name='id'][.='x']"
- ,"//result/doc[2]/str[@name='id'][.='b']"
- ,"//result/doc[1]/str[@name='id'][.='c']"
- );
-
- // Try normal sort by 'id'
- // default 'forceBoost' should be false
- assertEquals( false, booster.forceElevation );
- args.put( CommonParams.SORT, "str_s1 asc" );
- assertQ( null, req
- ,"//*[@numFound='4']"
- ,"//result/doc[1]/str[@name='id'][.='a']"
- ,"//result/doc[2]/str[@name='id'][.='b']"
- ,"//result/doc[3]/str[@name='id'][.='c']"
- ,"//result/doc[4]/str[@name='id'][.='x']"
- );
-
- booster.forceElevation = true;
- assertQ( null, req
- ,"//*[@numFound='4']"
- ,"//result/doc[1]/str[@name='id'][.='a']"
- ,"//result/doc[2]/str[@name='id'][.='x']"
- ,"//result/doc[3]/str[@name='id'][.='b']"
- ,"//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", "a" }, new String[] {} );
- assertQ( null, req
- ,"//*[@numFound='2']"
- ,"//result/doc[1]/str[@name='id'][.='x']"
- ,"//result/doc[2]/str[@name='id'][.='a']"
- );
-
- // 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']"
- ,"//result/doc[1]/str[@name='id'][.='x']"
- ,"//result/doc[2]/str[@name='id'][.='b']"
- ,"//result/doc[3]/str[@name='id'][.='c']"
- );
-
-
- req.close();
+ try {
+ init("schema12.xml");
+ assertU(adoc("id", "a", "title", "ipod", "str_s1", "a" ));
+ assertU(adoc("id", "b", "title", "ipod ipod", "str_s1", "b" ));
+ assertU(adoc("id", "c", "title", "ipod ipod ipod", "str_s1", "c" ));
+
+ assertU(adoc("id", "x", "title", "boosted", "str_s1", "x" ));
+ assertU(adoc("id", "y", "title", "boosted boosted", "str_s1", "y" ));
+ assertU(adoc("id", "z", "title", "boosted boosted boosted", "str_s1", "z" ));
+ assertU(commit());
+
+ String query = "title:ipod";
+
+ Map<String,String> args = new HashMap<String, String>();
+ args.put( CommonParams.Q, query );
+ args.put( CommonParams.QT, "/elevate" );
+ args.put( CommonParams.FL, "id,score" );
+ args.put( "indent", "true" );
+ //args.put( CommonParams.FL, "id,title,score" );
+ SolrQueryRequest req = new LocalSolrQueryRequest( h.getCore(), new MapSolrParams( args) );
+ IndexReader reader = req.getSearcher().getIndexReader();
+ QueryElevationComponent booster = (QueryElevationComponent)req.getCore().getSearchComponent( "elevate" );
+
+ assertQ("Make sure standard sort works as expected", req
+ ,"//*[@numFound='3']"
+ ,"//result/doc[1]/str[@name='id'][.='a']"
+ ,"//result/doc[2]/str[@name='id'][.='b']"
+ ,"//result/doc[3]/str[@name='id'][.='c']"
+ );
+
+ // Explicitly set what gets boosted
+ booster.elevationCache.clear();
+ booster.setTopQueryResults( reader, query, new String[] { "x", "y", "z" }, null );
+
+
+ assertQ("All six should make it", req
+ ,"//*[@numFound='6']"
+ ,"//result/doc[1]/str[@name='id'][.='x']"
+ ,"//result/doc[2]/str[@name='id'][.='y']"
+ ,"//result/doc[3]/str[@name='id'][.='z']"
+ ,"//result/doc[4]/str[@name='id'][.='a']"
+ ,"//result/doc[5]/str[@name='id'][.='b']"
+ ,"//result/doc[6]/str[@name='id'][.='c']"
+ );
+
+ booster.elevationCache.clear();
+
+ // now switch the order:
+ booster.setTopQueryResults( reader, query, new String[] { "a", "x" }, null );
+ assertQ("All four should make it", req
+ ,"//*[@numFound='4']"
+ ,"//result/doc[1]/str[@name='id'][.='a']"
+ ,"//result/doc[2]/str[@name='id'][.='x']"
+ ,"//result/doc[3]/str[@name='id'][.='b']"
+ ,"//result/doc[4]/str[@name='id'][.='c']"
+ );
+
+ // Test reverse sort
+ args.put( CommonParams.SORT, "score asc" );
+ assertQ("All four should make it", req
+ ,"//*[@numFound='4']"
+ ,"//result/doc[4]/str[@name='id'][.='a']"
+ ,"//result/doc[3]/str[@name='id'][.='x']"
+ ,"//result/doc[2]/str[@name='id'][.='b']"
+ ,"//result/doc[1]/str[@name='id'][.='c']"
+ );
+
+ // Try normal sort by 'id'
+ // default 'forceBoost' should be false
+ assertEquals( false, booster.forceElevation );
+ args.put( CommonParams.SORT, "str_s1 asc" );
+ assertQ( null, req
+ ,"//*[@numFound='4']"
+ ,"//result/doc[1]/str[@name='id'][.='a']"
+ ,"//result/doc[2]/str[@name='id'][.='b']"
+ ,"//result/doc[3]/str[@name='id'][.='c']"
+ ,"//result/doc[4]/str[@name='id'][.='x']"
+ );
+
+ booster.forceElevation = true;
+ assertQ( null, req
+ ,"//*[@numFound='4']"
+ ,"//result/doc[1]/str[@name='id'][.='a']"
+ ,"//result/doc[2]/str[@name='id'][.='x']"
+ ,"//result/doc[3]/str[@name='id'][.='b']"
+ ,"//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", "a" }, new String[] {} );
+ assertQ( null, req
+ ,"//*[@numFound='2']"
+ ,"//result/doc[1]/str[@name='id'][.='x']"
+ ,"//result/doc[2]/str[@name='id'][.='a']"
+ );
+
+ // 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']"
+ ,"//result/doc[1]/str[@name='id'][.='x']"
+ ,"//result/doc[2]/str[@name='id'][.='b']"
+ ,"//result/doc[3]/str[@name='id'][.='c']"
+ );
+
+
+ req.close();
+ } finally {
+ delete();
+ }
}
// write a test file to boost some docs
@@ -298,33 +343,38 @@ public class QueryElevationComponentTest
@Test
public void testElevationReloading() throws Exception
{
- String testfile = "data-elevation.xml";
- File f = new File( h.getCore().getDataDir(), testfile );
- writeFile( f, "aaa", "A" );
-
- QueryElevationComponent comp = (QueryElevationComponent)h.getCore().getSearchComponent("elevate");
- NamedList<String> args = new NamedList<String>();
- args.add( QueryElevationComponent.CONFIG_FILE, testfile );
- comp.init( args );
- comp.inform( h.getCore() );
-
- SolrQueryRequest req = req();
- IndexReader reader = req.getSearcher().getIndexReader();
- Map<String, ElevationObj> map = comp.getElevationMap(reader, h.getCore());
- assertTrue( map.get( "aaa" ).priority.containsKey( new BytesRef("A") ) );
- assertNull( map.get( "bbb" ) );
- req.close();
-
- // now change the file
- writeFile( f, "bbb", "B" );
- assertU(adoc("id", "10000")); // will get same reader if no index change
- assertU(commit());
-
- req = req();
- reader = req.getSearcher().getIndexReader();
- map = comp.getElevationMap(reader, h.getCore());
- assertNull( map.get( "aaa" ) );
- assertTrue( map.get( "bbb" ).priority.containsKey( new BytesRef("B") ) );
- req.close();
+ try {
+ init("schema12.xml");
+ String testfile = "data-elevation.xml";
+ File f = new File( h.getCore().getDataDir(), testfile );
+ writeFile( f, "aaa", "A" );
+
+ QueryElevationComponent comp = (QueryElevationComponent)h.getCore().getSearchComponent("elevate");
+ NamedList<String> args = new NamedList<String>();
+ args.add( QueryElevationComponent.CONFIG_FILE, testfile );
+ comp.init( args );
+ comp.inform( h.getCore() );
+
+ SolrQueryRequest req = req();
+ IndexReader reader = req.getSearcher().getIndexReader();
+ Map<String, ElevationObj> map = comp.getElevationMap(reader, h.getCore());
+ assertTrue( map.get( "aaa" ).priority.containsKey( new BytesRef("A") ) );
+ assertNull( map.get( "bbb" ) );
+ req.close();
+
+ // now change the file
+ writeFile( f, "bbb", "B" );
+ assertU(adoc("id", "10000")); // will get same reader if no index change
+ assertU(commit());
+
+ req = req();
+ reader = req.getSearcher().getIndexReader();
+ map = comp.getElevationMap(reader, h.getCore());
+ assertNull( map.get( "aaa" ) );
+ assertTrue( map.get( "bbb" ).priority.containsKey( new BytesRef("B") ) );
+ req.close();
+ } finally {
+ delete();
+ }
}
}