Apache Lucene open-source search software
SOLR-1139 -- Add TermsComponent Query and Response Support in SolrJ
Shalin Shekhar Mangar6e9af5f74d18
diff --git a/CHANGES.txt b/CHANGES.txtindex 4b3fd6cfd38..b1c0f01d3ec 100644--- a/CHANGES.txt+++ b/CHANGES.txt@@ -60,6 +60,8 @@ New Features* SOLR-1297: Add sort by Function capability (gsingers)+* SOLR-1139: Add TermsComponent Query and Response Support in SolrJ (Matt Weber via shalin)+Optimizations----------------------diff --git a/src/solrj/org/apache/solr/client/solrj/SolrQuery.java b/src/solrj/org/apache/solr/client/solrj/SolrQuery.javaindex 689c3064775..a3f29d5cc17 100644--- a/src/solrj/org/apache/solr/client/solrj/SolrQuery.java+++ b/src/solrj/org/apache/solr/client/solrj/SolrQuery.java@@ -22,6 +22,7 @@ import org.apache.solr.common.params.FacetParams;import org.apache.solr.common.params.HighlightParams;import org.apache.solr.common.params.ModifiableSolrParams;import org.apache.solr.common.params.StatsParams;+import org.apache.solr.common.params.TermsParams;import java.util.regex.Pattern;@@ -54,7 +55,154 @@ public class SolrQuery extends ModifiableSolrParamsthis.set(CommonParams.Q, q);}+ /** enable/disable terms.+ *+ * @param b flag to indicate terms should be enabled. <br /> if b==false, removes all other terms parameters+ * @return Current reference (<i>this</i>)+ */+ public SolrQuery setTerms(boolean b) {+ if (b) {+ this.set(TermsParams.TERMS, true);+ } else {+ this.remove(TermsParams.TERMS);+ this.remove(TermsParams.TERMS_FIELD);+ this.remove(TermsParams.TERMS_LOWER);+ this.remove(TermsParams.TERMS_UPPER);+ this.remove(TermsParams.TERMS_UPPER_INCLUSIVE);+ this.remove(TermsParams.TERMS_LOWER_INCLUSIVE);+ this.remove(TermsParams.TERMS_LIMIT);+ this.remove(TermsParams.TERMS_PREFIX_STR);+ this.remove(TermsParams.TERMS_MINCOUNT);+ this.remove(TermsParams.TERMS_MAXCOUNT);+ this.remove(TermsParams.TERMS_RAW);+ this.remove(TermsParams.TERMS_SORT);+ this.remove(TermsParams.TERMS_REGEXP_STR);+ this.remove(TermsParams.TERMS_REGEXP_FLAG);+ }+ return this;+ }++ public boolean getTerms() {+ return this.getBool(TermsParams.TERMS, false);+ }++ public SolrQuery addTermsField(String field) {+ this.add(TermsParams.TERMS_FIELD, field);+ return this;+ }++ public String[] getTermsFields() {+ return this.getParams(TermsParams.TERMS_FIELD);+ }++ public SolrQuery setTermsLower(String lower) {+ this.set(TermsParams.TERMS_LOWER, lower);+ return this;+ }++ public String getTermsLower() {+ return this.get(TermsParams.TERMS_LOWER, "");+ }++ public SolrQuery setTermsUpper(String upper) {+ this.set(TermsParams.TERMS_UPPER, upper);+ return this;+ }++ public String getTermsUpper() {+ return this.get(TermsParams.TERMS_UPPER, "");+ }++ public SolrQuery setTermsUpperInclusive(boolean b) {+ this.set(TermsParams.TERMS_UPPER_INCLUSIVE, b);+ return this;+ }++ public boolean getTermsUpperInclusive() {+ return this.getBool(TermsParams.TERMS_UPPER_INCLUSIVE, false);+ }++ public SolrQuery setTermsLowerInclusive(boolean b) {+ this.set(TermsParams.TERMS_LOWER_INCLUSIVE, b);+ return this;+ }++ public boolean getTermsLowerInclusive() {+ return this.getBool(TermsParams.TERMS_LOWER_INCLUSIVE, true);+ }++ public SolrQuery setTermsLimit(int limit) {+ this.set(TermsParams.TERMS_LIMIT, limit);+ return this;+ }++ public int getTermsLimit() {+ return this.getInt(TermsParams.TERMS_LIMIT, 10);+ }++ public SolrQuery setTermsMinCount(int cnt) {+ this.set(TermsParams.TERMS_MINCOUNT, cnt);+ return this;+ }++ public int getTermsMinCount() {+ return this.getInt(TermsParams.TERMS_MINCOUNT, 1);+ }+ public SolrQuery setTermsMaxCount(int cnt) {+ this.set(TermsParams.TERMS_MAXCOUNT, cnt);+ return this;+ }++ public int getTermsMaxCount() {+ return this.getInt(TermsParams.TERMS_MAXCOUNT, -1);+ }++ public SolrQuery setTermsPrefix(String prefix) {+ this.set(TermsParams.TERMS_PREFIX_STR, prefix);+ return this;+ }++ public String getTermsPrefix() {+ return this.get(TermsParams.TERMS_PREFIX_STR, "");+ }++ public SolrQuery setTermsRaw(boolean b) {+ this.set(TermsParams.TERMS_RAW, b);+ return this;+ }++ public boolean getTermsRaw() {+ return this.getBool(TermsParams.TERMS_RAW, false);+ }++ public SolrQuery setTermsSortString(String type) {+ this.set(TermsParams.TERMS_SORT, type);+ return this;+ }++ public String getTermsSortString() {+ return this.get(TermsParams.TERMS_SORT, TermsParams.TERMS_SORT_COUNT);+ }++ public SolrQuery setTermsRegex(String regex) {+ this.set(TermsParams.TERMS_REGEXP_STR, regex);+ return this;+ }++ public String getTermsRegex() {+ return this.get(TermsParams.TERMS_REGEXP_STR);+ }++ public SolrQuery setTermsRegexFlag(String flag) {+ this.add(TermsParams.TERMS_REGEXP_FLAG, flag);+ return this;+ }++ public String[] getTermsRegexFlags() {+ return this.getParams(TermsParams.TERMS_REGEXP_FLAG);+ }+/** Add field(s) for facet computation.** @param fields Array of field names from the IndexSchemadiff --git a/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java b/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.javaindex 3bf6d76d505..952ba14be3e 100644--- a/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java+++ b/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java@@ -46,6 +46,7 @@ public class QueryResponse extends SolrResponseBaseprivate NamedList<Object> _highlightingInfo = null;private NamedList<Object> _spellInfo = null;private NamedList<Object> _statsInfo = null;+ private NamedList<Object> _termsInfo = null;// Facet stuffprivate Map<String,Integer> _facetQuery = null;@@ -59,6 +60,9 @@ public class QueryResponse extends SolrResponseBase// SpellCheck Responseprivate SpellCheckResponse _spellResponse = null;+ // Terms Response+ private TermsResponse _termsResponse = null;+// Field stats Responseprivate Map<String,FieldStatsInfo> _fieldStatsInfo = null;@@ -118,6 +122,10 @@ public class QueryResponse extends SolrResponseBase_statsInfo = (NamedList<Object>) res.getVal( i );extractStatsInfo( _statsInfo );}+ else if ( "terms".equals( n ) ) {+ _termsInfo = (NamedList<Object>) res.getVal( i );+ extractTermsInfo( _termsInfo );+ }}}@@ -125,6 +133,10 @@ public class QueryResponse extends SolrResponseBase_spellResponse = new SpellCheckResponse(spellInfo);}+ private void extractTermsInfo(NamedList<Object> termsInfo) {+ _termsResponse = new TermsResponse(termsInfo);+ }+private void extractStatsInfo(NamedList<Object> info) {if( info != null ) {_fieldStatsInfo = new HashMap<String, FieldStatsInfo>();@@ -276,6 +288,10 @@ public class QueryResponse extends SolrResponseBasereturn _spellResponse;}+ public TermsResponse getTermsResponse() {+ return _termsResponse;+ }+/*** See also: {@link #getLimitingFacets()}*/diff --git a/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java b/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.javanew file mode 100644index 00000000000..f0b28b405c2--- /dev/null+++ b/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java@@ -0,0 +1,89 @@+package org.apache.solr.client.solrj.response;+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import org.apache.solr.common.util.NamedList;++import java.util.ArrayList;+import java.util.HashMap;+import java.util.List;+import java.util.Map;++/**+ * Encapsulates responses from TermsComponent+ */+public class TermsResponse {+ private Map<String, List<Term>> termMap = new HashMap<String, List<Term>>();++ public TermsResponse(NamedList<Object> termsInfo) {+ for (int i = 0; i < termsInfo.size(); i++) {+ String fieldName = termsInfo.getName(i);+ List<Term> itemList = new ArrayList<Term>();+ NamedList<Object> items = (NamedList<Object>) termsInfo.getVal(i);++ for (int j = 0; j < items.size(); j++) {+ Term t = new Term(items.getName(j), ((Number) items.getVal(j)).longValue());+ itemList.add(t);+ }++ termMap.put(fieldName, itemList);+ }+ }++ /**+ * Get's the term list for a given field+ *+ * @return the term list or null if no terms for the given field exist+ */+ public List<Term> getTerms(String field) {+ return termMap.get(field);+ }++ public Map<String, List<Term>> getTermMap() {+ return termMap;+ }++ public static class Term {+ private String term;+ private long frequency;++ public Term(String term, long frequency) {+ this.term = term;+ this.frequency = frequency;+ }++ public String getTerm() {+ return term;+ }++ public void setTerm(String term) {+ this.term = term;+ }++ public long getFrequency() {+ return frequency;+ }++ public void setFrequency(long frequency) {+ this.frequency = frequency;+ }++ public void addFrequency(long frequency) {+ this.frequency += frequency;+ }+ }+}diff --git a/src/test/org/apache/solr/client/solrj/SolrQueryTest.java b/src/test/org/apache/solr/client/solrj/SolrQueryTest.javaindex 3cda2151f8f..2ef260d2c74 100644--- a/src/test/org/apache/solr/client/solrj/SolrQueryTest.java+++ b/src/test/org/apache/solr/client/solrj/SolrQueryTest.java@@ -143,4 +143,56 @@ public class SolrQueryTest extends TestCase {assertEquals( SolrQuery.ORDER.asc, SolrQuery.ORDER.desc.reverse() );assertEquals( SolrQuery.ORDER.desc, SolrQuery.ORDER.asc.reverse() );}++ public void testTerms() {+ SolrQuery q = new SolrQuery();++ // check getters+ assertEquals(false, q.getTerms());+ assertEquals(null, q.getTermsFields());+ assertEquals("", q.getTermsLower());+ assertEquals("", q.getTermsUpper());+ assertEquals(false, q.getTermsUpperInclusive());+ assertEquals(true, q.getTermsLowerInclusive());+ assertEquals(10, q.getTermsLimit());+ assertEquals(1, q.getTermsMinCount());+ assertEquals(-1, q.getTermsMaxCount());+ assertEquals("", q.getTermsPrefix());+ assertEquals(false, q.getTermsRaw());+ assertEquals("count", q.getTermsSortString());+ assertEquals(null, q.getTermsRegex());+ assertEquals(null, q.getTermsRegexFlags());++ // check setters+ q.setTerms(true);+ assertEquals(true, q.getTerms());+ q.addTermsField("testfield");+ assertEquals(1, q.getTermsFields().length);+ assertEquals("testfield", q.getTermsFields()[0]);+ q.setTermsLower("lower");+ assertEquals("lower", q.getTermsLower());+ q.setTermsUpper("upper");+ assertEquals("upper", q.getTermsUpper());+ q.setTermsUpperInclusive(true);+ assertEquals(true, q.getTermsUpperInclusive());+ q.setTermsLowerInclusive(false);+ assertEquals(false, q.getTermsLowerInclusive());+ q.setTermsLimit(5);+ assertEquals(5, q.getTermsLimit());+ q.setTermsMinCount(2);+ assertEquals(2, q.getTermsMinCount());+ q.setTermsMaxCount(5);+ assertEquals(5, q.getTermsMaxCount());+ q.setTermsPrefix("prefix");+ assertEquals("prefix", q.getTermsPrefix());+ q.setTermsRaw(true);+ assertEquals(true, q.getTermsRaw());+ q.setTermsSortString("index");+ assertEquals("index", q.getTermsSortString());+ q.setTermsRegex("a.*");+ assertEquals("a.*", q.getTermsRegex());+ q.setTermsRegexFlag("case_insensitive");+ q.setTermsRegexFlag("multiline");+ assertEquals(2, q.getTermsRegexFlags().length);+ }}diff --git a/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java b/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.javanew file mode 100644index 00000000000..dcddc477e84--- /dev/null+++ b/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java@@ -0,0 +1,79 @@+package org.apache.solr.client.solrj.response;+/**+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.util.List;+import junit.framework.Assert;++import org.apache.solr.client.solrj.SolrQuery;+import org.apache.solr.client.solrj.SolrServer;+import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.client.solrj.SolrExampleTestBase;+import org.apache.solr.client.solrj.request.QueryRequest;+import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;+import org.apache.solr.client.solrj.response.TermsResponse.Term;++/**+ * Test for TermComponent's response in Solrj+ */+public class TermsResponseTest extends SolrExampleTestBase {++ SolrServer server;++ @Override+ public void setUp() throws Exception {+ super.setUp();+ server = createNewSolrServer();+ }++ @Override+ protected SolrServer getSolrServer() {+ return server;+ }++ @Override+ protected SolrServer createNewSolrServer() {+ return new EmbeddedSolrServer(h.getCoreContainer(), "");+ }++ public void testTermsResponse() throws Exception {+ SolrInputDocument doc = new SolrInputDocument();+ doc.setField("id", 1);+ doc.setField("terms_s", "samsung");+ getSolrServer().add(doc);+ getSolrServer().commit(true, true);++ SolrQuery query = new SolrQuery();+ query.setQueryType("/terms");+ query.setTerms(true);+ query.setTermsLimit(5);+ query.setTermsLower("s");+ query.setTermsPrefix("s");+ query.addTermsField("terms_s");+ query.setTermsMinCount(1);++ QueryRequest request = new QueryRequest(query);+ List<Term> terms = request.process(getSolrServer()).getTermsResponse().getTerms("terms_s");++ Assert.assertNotNull(terms);+ Assert.assertEquals(terms.size(), 1);++ Term term = terms.get(0);+ Assert.assertEquals(term.getTerm(), "samsung");+ Assert.assertEquals(term.getFrequency(), 1);+ }+}