SOLR-877: add in term prefix, change some param names, added more tests

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@721681 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Grant Ingersoll 2008-11-29 13:51:56 +00:00
parent 222f4863bc
commit f1a3951c6d
3 changed files with 130 additions and 29 deletions

View File

@ -22,9 +22,14 @@ package org.apache.solr.common.params;
*
**/
public class TermsParams {
/**
* The component name. Set to true to turn on the TermsComponent
*/
public static final String TERMS = "terms";
/**
* Used for building up the other terms
*/
public static final String TERMS_PREFIX = TERMS + ".";
/**
@ -48,15 +53,17 @@ public class TermsParams {
/**
* Optional. If true, include the upper bound term in the results. False by default.
*/
public static final String TERMS_UPPER_INCLUSIVE = TERMS_PREFIX + "upr.incl";
public static final String TERMS_UPPER_INCLUSIVE = TERMS_PREFIX + "upper.incl";
/**
* Optional. If true, include the lower bound term in the results, otherwise skip to the next one. True by default.
*/
public static final String TERMS_LOWER_INCLUSIVE = TERMS_PREFIX + "lwr.incl";
public static final String TERMS_LOWER_INCLUSIVE = TERMS_PREFIX + "lower.incl";
/**
* Optional. The number of results to return. If not specified, looks for {@link org.apache.solr.common.params.CommonParams#ROWS}. If that's not specified, uses 10.
*/
public static final String TERMS_ROWS = TERMS_PREFIX + "rows";
public static final String TERMS_PREFIX_STR = TERMS_PREFIX + "prefix";
}

View File

@ -44,21 +44,27 @@ public class TermsComponent extends SearchComponent {
if (fields != null && fields.length > 0) {
NamedList terms = new NamedList();
rb.rsp.add("terms", terms);
int rows = params.getInt(TermsParams.TERMS_ROWS, params.getInt(CommonParams.ROWS, 10));
if (rows < 0){
rows = Integer.MAX_VALUE;
}
String upper = params.get(TermsParams.TERMS_UPPER);
boolean upperIncl = params.getBool(TermsParams.TERMS_UPPER_INCLUSIVE, false);
boolean lowerIncl = params.getBool(TermsParams.TERMS_LOWER_INCLUSIVE, true);
String prefix = params.get(TermsParams.TERMS_PREFIX_STR);
for (int j = 0; j < fields.length; j++) {
String field = fields[j];
Term lowerTerm = new Term(field, lower);
Term upperTerm = upper != null ? new Term(field, upper) : null;
TermEnum termEnum = rb.req.getSearcher().getReader().terms(lowerTerm);//this will be positioned ready to go
int rows = params.getInt(TermsParams.TERMS_ROWS, params.getInt(CommonParams.ROWS, 10));
int i = 0;
NamedList fieldTerms = new NamedList();
terms.add(field, fieldTerms);
String upper = params.get(TermsParams.TERMS_UPPER);
Term upperTerm = upper != null ? new Term(field, upper) : null;
boolean upperIncl = params.getBool(TermsParams.TERMS_UPPER_INCLUSIVE, false);
boolean lowerIncl = params.getBool(TermsParams.TERMS_LOWER_INCLUSIVE, true);
boolean hasMore = true;
if (lowerIncl == false) {
Term lowerTestTerm = termEnum.term();
//Only advance the enum if we are excluding the lower bound and the lower Term actually matches
if (lowerIncl == false && lowerTestTerm.field().equals(field) == true && lowerTestTerm.text().equals(lower)) {
hasMore = termEnum.next();
}
if (hasMore == true) {
@ -68,7 +74,9 @@ public class TermsComponent extends SearchComponent {
int upperCmp = upperTerm != null ? theTerm.compareTo(upperTerm) : -1;
if (theTerm != null && theTerm.field().equals(field)
&& ((upperIncl == true && upperCmp <= 0) ||
(upperIncl == false && upperCmp < 0))) {
(upperIncl == false && upperCmp < 0))
&& (prefix == null || theText.startsWith(prefix))
) {
fieldTerms.add(theText, termEnum.docFreq());
} else {//we're done
break;

View File

@ -144,6 +144,60 @@ public class TermsComponentTest extends AbstractSolrTestCase {
assertTrue("terms Size: " + terms.size() + " is not: " + 4, terms.size() == 4);
}
public void testUnlimitedRows() throws Exception {
SolrCore core = h.getCore();
TermsComponent tc = (TermsComponent) core.getSearchComponent("termsComp");
assertTrue("tc is null and it shouldn't be", tc != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_FIELD, "lowerfilt", "standardfilt");
//no lower bound, upper bound or rows
params.add(TermsParams.TERMS_ROWS, String.valueOf(-1));
SolrRequestHandler handler;
SolrQueryResponse rsp;
NamedList values;
NamedList terms;
handler = core.getRequestHandler("/terms");
assertTrue("handler is null and it shouldn't be", handler != null);
rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
values = rsp.getValues();
terms = (NamedList) ((NamedList) values.get("terms")).get("lowerfilt");
assertTrue("terms Size: " + terms.size() + " is not: " + 9, terms.size() == 9);
}
public void testPrefix() throws Exception {
SolrCore core = h.getCore();
TermsComponent tc = (TermsComponent) core.getSearchComponent("termsComp");
assertTrue("tc is null and it shouldn't be", tc != null);
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_FIELD, "lowerfilt", "standardfilt");
params.add(TermsParams.TERMS_LOWER, "aa");
params.add(TermsParams.TERMS_LOWER_INCLUSIVE, "false");
params.add(TermsParams.TERMS_PREFIX_STR, "aa");
params.add(TermsParams.TERMS_UPPER, "b");
params.add(TermsParams.TERMS_ROWS, String.valueOf(50));
SolrRequestHandler handler;
SolrQueryResponse rsp;
NamedList values;
NamedList terms;
handler = core.getRequestHandler("/terms");
assertTrue("handler is null and it shouldn't be", handler != null);
rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
values = rsp.getValues();
terms = (NamedList) ((NamedList) values.get("terms")).get("lowerfilt");
assertTrue("terms Size: " + terms.size() + " is not: " + 1, terms.size() == 1);
Object value = terms.get("aaa");
assertTrue("value is null and it shouldn't be", value != null);
}
public void testPastUpper() throws Exception {
SolrCore core = h.getCore();
TermsComponent tc = (TermsComponent) core.getSearchComponent("termsComp");
@ -152,7 +206,7 @@ public class TermsComponentTest extends AbstractSolrTestCase {
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_FIELD, "lowerfilt");
//no lower bound
//no upper bound, lower bound doesn't exist
params.add(TermsParams.TERMS_LOWER, "d");
params.add(TermsParams.TERMS_ROWS, String.valueOf(50));
SolrRequestHandler handler;
@ -169,6 +223,56 @@ public class TermsComponentTest extends AbstractSolrTestCase {
assertTrue("terms Size: " + terms.size() + " is not: " + 0, terms.size() == 0);
}
public void testLowerExclusive() throws Exception {
SolrCore core = h.getCore();
TermsComponent tc = (TermsComponent) core.getSearchComponent("termsComp");
assertTrue("tc is null and it shouldn't be", tc != null);
//test where the lower is an actual term
ModifiableSolrParams params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_LOWER_INCLUSIVE, "false");
params.add(TermsParams.TERMS_FIELD, "lowerfilt");
params.add(TermsParams.TERMS_LOWER, "a");
params.add(TermsParams.TERMS_UPPER, "b");
params.add(TermsParams.TERMS_ROWS, String.valueOf(50));
SolrRequestHandler handler;
SolrQueryResponse rsp;
NamedList values;
NamedList terms;
handler = core.getRequestHandler("/terms");
assertTrue("handler is null and it shouldn't be", handler != null);
rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
values = rsp.getValues();
terms = (NamedList) ((NamedList) values.get("terms")).get("lowerfilt");
assertTrue("terms Size: " + terms.size() + " is not: " + 5, terms.size() == 5);
assertTrue("aa is null and it shouldn't be", terms.get("aa") != null);
assertTrue("ab is null and it shouldn't be", terms.get("ab") != null);
assertTrue("aaa is null and it shouldn't be", terms.get("aaa") != null);
assertTrue("abb is null and it shouldn't be", terms.get("abb") != null);
assertTrue("abc is null and it shouldn't be", terms.get("abc") != null);
assertTrue("a is not null", terms.get("a") == null);
assertTrue("baa is not null", terms.get("baa") == null);
//test where the lower is not a term
params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_LOWER_INCLUSIVE, "false");
params.add(TermsParams.TERMS_FIELD, "standardfilt");
params.add(TermsParams.TERMS_LOWER, "cc");
params.add(TermsParams.TERMS_UPPER, "d");
params.add(TermsParams.TERMS_ROWS, String.valueOf(50));
rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
values = rsp.getValues();
terms = (NamedList) ((NamedList) values.get("terms")).get("standardfilt");
assertTrue("terms Size: " + terms.size() + " is not: " + 2, terms.size() == 2);
}
public void test() throws Exception {
SolrCore core = h.getCore();
TermsComponent tc = (TermsComponent) core.getSearchComponent("termsComp");
@ -216,24 +320,6 @@ public class TermsComponentTest extends AbstractSolrTestCase {
assertTrue("a is null", terms.get("a") != null);
assertTrue("baa is not null", terms.get("baa") == null);
params.add(TermsParams.TERMS_LOWER_INCLUSIVE, "false");
rsp = new SolrQueryResponse();
rsp.add("responseHeader", new SimpleOrderedMap());
handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
values = rsp.getValues();
terms = (NamedList) ((NamedList) values.get("terms")).get("lowerfilt");
assertTrue("terms Size: " + terms.size() + " is not: " + 6, terms.size() == 6);
assertTrue("aa is null and it shouldn't be", terms.get("aa") != null);
assertTrue("ab is null and it shouldn't be", terms.get("ab") != null);
assertTrue("aaa is null and it shouldn't be", terms.get("aaa") != null);
assertTrue("abb is null and it shouldn't be", terms.get("abb") != null);
assertTrue("abc is null and it shouldn't be", terms.get("abc") != null);
assertTrue("b is null and it shouldn't be", terms.get("b") != null);
assertTrue("a is not null", terms.get("a") == null);
assertTrue("baa is not null", terms.get("baa") == null);
params = new ModifiableSolrParams();
params.add(TermsParams.TERMS, "true");
params.add(TermsParams.TERMS_FIELD, "lowerfilt");