Apache Solr open-source search software
SOLR-877: add in term prefix, change some param names, added more tests
Grant Ingersollf1a3951c6db0
diff --git a/src/java/org/apache/solr/common/params/TermsParams.java b/src/java/org/apache/solr/common/params/TermsParams.javaindex bdb1017a39d..9c3cb0a4927 100644--- a/src/java/org/apache/solr/common/params/TermsParams.java+++ b/src/java/org/apache/solr/common/params/TermsParams.java@@ -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";}diff --git a/src/java/org/apache/solr/handler/component/TermsComponent.java b/src/java/org/apache/solr/handler/component/TermsComponent.javaindex f95e642d4cd..be89eabeb63 100644--- a/src/java/org/apache/solr/handler/component/TermsComponent.java+++ b/src/java/org/apache/solr/handler/component/TermsComponent.java@@ -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 donebreak;diff --git a/src/test/org/apache/solr/handler/component/TermsComponentTest.java b/src/test/org/apache/solr/handler/component/TermsComponentTest.javaindex a8c5e316e2f..413174690cd 100644--- a/src/test/org/apache/solr/handler/component/TermsComponentTest.java+++ b/src/test/org/apache/solr/handler/component/TermsComponentTest.java@@ -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 existparams.add(TermsParams.TERMS_LOWER, "d");params.add(TermsParams.TERMS_ROWS, String.valueOf(50));SolrRequestHandler handler;@@ -169,13 +223,14 @@ public class TermsComponentTest extends AbstractSolrTestCase {assertTrue("terms Size: " + terms.size() + " is not: " + 0, terms.size() == 0);}- public void test() throws Exception {+ 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 termModifiableSolrParams 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");@@ -186,54 +241,85 @@ public class TermsComponentTest extends AbstractSolrTestCase {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: " + 6, terms.size() == 6);+ 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("aaa is null and it shouldn't be", terms.get("aaa") != 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 null", terms.get("a") != null);- assertTrue("b is not null and it should be", terms.get("b") == null);+ assertTrue("a is not null", terms.get("a") == null);+ assertTrue("baa is not null", terms.get("baa") == null);- params.add(TermsParams.TERMS_UPPER_INCLUSIVE, "true");+ //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");+ 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");+ 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: " + 7, terms.size() == 7);+ 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("ab is null and it shouldn't be", terms.get("ab") != 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 null", terms.get("a") != null);- assertTrue("baa is not null", terms.get("baa") == null);+ assertTrue("b is not null and it should be", terms.get("b") == null);- params.add(TermsParams.TERMS_LOWER_INCLUSIVE, "false");+ params.add(TermsParams.TERMS_UPPER_INCLUSIVE, "true");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("terms Size: " + terms.size() + " is not: " + 7, terms.size() == 7);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("a is 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");