SOLR-5829: Allow ExpandComponent to accept query and filter query parameters

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1583802 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Joel Bernstein 2014-04-01 21:37:05 +00:00
parent fe148ceb1a
commit e081587dea
5 changed files with 154 additions and 37 deletions

View File

@ -167,6 +167,11 @@ New Features
date for documents from the "TTL" expression, as well as automatically deleting expired
documents on a periodic basis. (hossman)
* SOLR-5829: Allow ExpandComponent to accept query and filter query parameters
(Joel Bernstein)
Bug Fixes
----------------------

View File

@ -39,6 +39,7 @@ import org.apache.solr.common.params.ShardParams;
import org.apache.solr.search.CollapsingQParserPlugin;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.QParser;
import org.apache.solr.search.QueryParsing;
import org.apache.solr.schema.FieldType;
import org.apache.solr.common.params.SolrParams;
@ -51,11 +52,9 @@ import org.apache.solr.util.plugin.PluginInfoInitialized;
import org.apache.solr.util.plugin.SolrCoreAware;
import org.apache.solr.core.PluginInfo;
import org.apache.solr.core.SolrCore;
import com.carrotsearch.hppc.IntObjectOpenHashMap;
import com.carrotsearch.hppc.IntOpenHashSet;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
@ -64,7 +63,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
/**
* The ExpandComponent is designed to work with the CollapsingPostFilter.
@ -75,9 +73,11 @@ import java.util.Vector;
* http parameters:
* <p/>
* expand=true <br/>
* expand.rows=5 </br>
* expand.sort=field asc|desc
*
* expand.rows=5 <br/>
* expand.sort=field asc|desc<br/>
* expand.q=*:* (optional, overrides the main query)<br/>
* expand.fq=type:child (optional, overrides the main filter queries)<br/>
* expand.field=field (mandatory if the not used with the CollapsingQParserPlugin)<br/>
**/
public class ExpandComponent extends SearchComponent implements PluginInfoInitialized, SolrCoreAware {
@ -117,8 +117,26 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
return;
}
String field = null;
String field = params.get(ExpandParams.EXPAND_FIELD);
if(field == null) {
List<Query> filters = rb.getFilters();
if(filters != null) {
for(Query q : filters) {
if(q instanceof CollapsingQParserPlugin.CollapsingPostFilter) {
CollapsingQParserPlugin.CollapsingPostFilter cp = (CollapsingQParserPlugin.CollapsingPostFilter)q;
field = cp.getField();
}
}
}
}
if(field == null) {
throw new IOException("Expand field is null.");
}
String sortParam = params.get(ExpandParams.EXPAND_SORT);
String[] fqs = params.getParams(ExpandParams.EXPAND_FQ);
String qs = params.get(ExpandParams.EXPAND_Q);
int limit = params.getInt(ExpandParams.EXPAND_ROWS, 5);
Sort sort = null;
@ -127,20 +145,40 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
sort = QueryParsing.parseSortSpec(sortParam, rb.req).getSort();
}
Query query = rb.getQuery();
List<Query> filters = rb.getFilters();
List<Query> newFilters = new ArrayList();
for(Query q : filters) {
if(!(q instanceof CollapsingQParserPlugin.CollapsingPostFilter)) {
newFilters.add(q);
} else {
CollapsingQParserPlugin.CollapsingPostFilter cp = (CollapsingQParserPlugin.CollapsingPostFilter)q;
field = cp.getField();
Query query = null;
if(qs == null) {
query = rb.getQuery();
} else {
try {
QParser parser = QParser.getParser(qs, null, req);
query = parser.getQuery();
} catch(Exception e) {
throw new IOException(e);
}
}
if(field == null) {
throw new IOException("Expand field is null.");
List<Query> newFilters = new ArrayList();
if(fqs == null) {
List<Query> filters = rb.getFilters();
if(filters != null) {
for(Query q : filters) {
if(!(q instanceof CollapsingQParserPlugin.CollapsingPostFilter)) {
newFilters.add(q);
}
}
}
} else {
try {
for (String fq : fqs) {
if (fq != null && fq.trim().length()!=0 && !fq.equals("*:*")) {
QParser fqp = QParser.getParser(fq, null, req);
newFilters.add(fqp.getQuery());
}
}
} catch(Exception e) {
throw new IOException(e);
}
}
SolrIndexSearcher searcher = req.getSearcher();

View File

@ -88,6 +88,9 @@ public class DistributedExpandComponentTest extends BaseDistributedSearchTestCas
query("q", "test_ti:5", "fq", "{!collapse field=group_s}", "defType", "edismax", "bf", "field(test_ti)", "expand", "true", "expand.sort", "test_tl desc", "expand.rows", "1", "fl","*,score");
//Test zero results
query("q", "test_ti:5434343", "fq", "{!collapse field=group_s}", "defType", "edismax", "bf", "field(test_ti)", "expand", "true", "expand.sort", "test_tl desc", "expand.rows", "1", "fl","*,score");
//Test page 2
query("q", "*:*", "start","1", "rows", "1", "fq", "{!collapse field=group_s}", "defType", "edismax", "bf", "field(test_ti)", "expand", "true", "fl","*,score");
//First basic test case.
ModifiableSolrParams params = new ModifiableSolrParams();

View File

@ -45,10 +45,10 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
@Test
public void testExpand() throws Exception {
String[] doc = {"id","1", "term_s", "YYYY", "group_s", "group1", "test_ti", "5", "test_tl", "10", "test_tf", "2000"};
String[] doc = {"id","1", "term_s", "YYYY", "group_s", "group1", "test_ti", "5", "test_tl", "10", "test_tf", "2000", "type_s", "parent"};
assertU(adoc(doc));
assertU(commit());
String[] doc1 = {"id","2", "term_s","YYYY", "group_s", "group1", "test_ti", "50", "test_tl", "100", "test_tf", "200"};
String[] doc1 = {"id","2", "term_s","YYYY", "group_s", "group1", "test_ti", "50", "test_tl", "100", "test_tf", "200", "type_s", "child"};
assertU(adoc(doc1));
String[] doc2 = {"id","3", "term_s", "YYYY", "test_ti", "5000", "test_tl", "100", "test_tf", "200"};
@ -58,23 +58,21 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
assertU(adoc(doc3));
String[] doc4 = {"id","5", "term_s", "YYYY", "group_s", "group2", "test_ti", "4", "test_tl", "10", "test_tf", "2000"};
String[] doc4 = {"id","5", "term_s", "YYYY", "group_s", "group2", "test_ti", "4", "test_tl", "10", "test_tf", "2000", "type_s", "parent"};
assertU(adoc(doc4));
assertU(commit());
String[] doc5 = {"id","6", "term_s","YYYY", "group_s", "group2", "test_ti", "10", "test_tl", "100", "test_tf", "200"};
String[] doc5 = {"id","6", "term_s","YYYY", "group_s", "group2", "test_ti", "10", "test_tl", "100", "test_tf", "200", "type_s", "child"};
assertU(adoc(doc5));
assertU(commit());
String[] doc6 = {"id","7", "term_s", "YYYY", "group_s", "group1", "test_ti", "1", "test_tl", "100000", "test_tf", "2000"};
String[] doc6 = {"id","7", "term_s", "YYYY", "group_s", "group1", "test_ti", "1", "test_tl", "100000", "test_tf", "2000", "type_s", "child"};
assertU(adoc(doc6));
assertU(commit());
String[] doc7 = {"id","8", "term_s","YYYY", "group_s", "group2", "test_ti", "2", "test_tl", "100000", "test_tf", "200"};
String[] doc7 = {"id","8", "term_s","YYYY", "group_s", "group2", "test_ti", "2", "test_tl", "100000", "test_tf", "200", "type_s", "child"};
assertU(adoc(doc7));
assertU(commit());
//First basic test case.
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q", "*:*");
@ -92,6 +90,23 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
"/response/lst[@name='expanded']/result[@name='group2']/doc[2]/float[@name='id'][.='8.0']"
);
//Basic test case page 2
params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("fq", "{!collapse field=group_s}");
params.add("defType", "edismax");
params.add("bf", "field(test_ti)");
params.add("expand", "true");
params.add("rows", "1");
params.add("start", "1");
assertQ(req(params), "*[count(/response/result/doc)=1]",
"*[count(/response/lst[@name='expanded']/result)=1]",
"/response/result/doc[1]/float[@name='id'][.='6.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[1]/float[@name='id'][.='5.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[2]/float[@name='id'][.='8.0']"
);
//Test expand.sort
params = new ModifiableSolrParams();
params.add("q", "*:*");
@ -131,6 +146,70 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
);
//Test overide expand.q
params = new ModifiableSolrParams();
params.add("q", "type_s:parent");
params.add("defType", "edismax");
params.add("bf", "field(test_ti)");
params.add("expand", "true");
params.add("expand.q", "type_s:child");
params.add("expand.field", "group_s");
params.add("expand.sort", "test_tl desc");
assertQ(req(params), "*[count(/response/result/doc)=2]",
"*[count(/response/lst[@name='expanded']/result)=2]",
"/response/result/doc[1]/float[@name='id'][.='1.0']",
"/response/result/doc[2]/float[@name='id'][.='5.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[1]/float[@name='id'][.='7.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[2]/float[@name='id'][.='2.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[1]/float[@name='id'][.='8.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[2]/float[@name='id'][.='6.0']"
);
//Test overide expand.fq
params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("fq", "type_s:parent");
params.add("defType", "edismax");
params.add("bf", "field(test_ti)");
params.add("expand", "true");
params.add("expand.fq", "type_s:child");
params.add("expand.field", "group_s");
params.add("expand.sort", "test_tl desc");
assertQ(req(params), "*[count(/response/result/doc)=2]",
"*[count(/response/lst[@name='expanded']/result)=2]",
"/response/result/doc[1]/float[@name='id'][.='1.0']",
"/response/result/doc[2]/float[@name='id'][.='5.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[1]/float[@name='id'][.='7.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[2]/float[@name='id'][.='2.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[1]/float[@name='id'][.='8.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[2]/float[@name='id'][.='6.0']"
);
//Test overide expand.fq and expand.q
params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("fq", "type_s:parent");
params.add("defType", "edismax");
params.add("bf", "field(test_ti)");
params.add("expand", "true");
params.add("expand.q", "type_s:child");
params.add("expand.fq", "*:*");
params.add("expand.field", "group_s");
params.add("expand.sort", "test_tl desc");
assertQ(req(params), "*[count(/response/result/doc)=2]",
"*[count(/response/lst[@name='expanded']/result)=2]",
"/response/result/doc[1]/float[@name='id'][.='1.0']",
"/response/result/doc[2]/float[@name='id'][.='5.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[1]/float[@name='id'][.='7.0']",
"/response/lst[@name='expanded']/result[@name='group1']/doc[2]/float[@name='id'][.='2.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[1]/float[@name='id'][.='8.0']",
"/response/lst[@name='expanded']/result[@name='group2']/doc[2]/float[@name='id'][.='6.0']"
);
//Test expand.rows
params = new ModifiableSolrParams();
@ -179,15 +258,6 @@ public class TestExpandComponent extends SolrTestCaseJ4 {
assertQ(req(params), "*[count(/response/result/doc)=0]",
"*[count(/response/lst[@name='expanded']/result)=0]"
);
}
}

View File

@ -25,7 +25,8 @@ public interface ExpandParams {
public static final String EXPAND = "expand";
public static final String EXPAND_SORT = EXPAND + ".sort";
public static final String EXPAND_ROWS = EXPAND + ".rows";
public static final String EXPAND_FIELD = EXPAND + ".field";
public static final String EXPAND_Q = EXPAND + ".q";
public static final String EXPAND_FQ = EXPAND + ".fq";
}