lucene-solr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sha...@apache.org
Subject svn commit: r890053 - in /lucene/solr/trunk: ./ src/solrj/org/apache/solr/client/solrj/ src/solrj/org/apache/solr/client/solrj/response/ src/test/org/apache/solr/client/solrj/ src/test/org/apache/solr/client/solrj/response/
Date Sun, 13 Dec 2009 11:58:37 GMT
Author: shalin
Date: Sun Dec 13 11:58:37 2009
New Revision: 890053

URL: http://svn.apache.org/viewvc?rev=890053&view=rev
Log:
SOLR-1139 -- Add TermsComponent Query and Response Support in SolrJ

Added:
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java 
 (with props)
    lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
  (with props)
Modified:
    lucene/solr/trunk/CHANGES.txt
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/SolrQuery.java
    lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java
    lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrQueryTest.java

Modified: lucene/solr/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/CHANGES.txt?rev=890053&r1=890052&r2=890053&view=diff
==============================================================================
--- lucene/solr/trunk/CHANGES.txt (original)
+++ lucene/solr/trunk/CHANGES.txt Sun Dec 13 11:58:37 2009
@@ -60,6 +60,8 @@
 
 * SOLR-1297: Add sort by Function capability (gsingers)
 
+* SOLR-1139: Add TermsComponent Query and Response Support in SolrJ (Matt Weber via shalin)
+
 Optimizations
 ----------------------
 

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/SolrQuery.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/SolrQuery.java?rev=890053&r1=890052&r2=890053&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/SolrQuery.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/SolrQuery.java Sun Dec 13 11:58:37
2009
@@ -22,6 +22,7 @@
 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 @@
     this.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 IndexSchema

Modified: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java?rev=890053&r1=890052&r2=890053&view=diff
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java (original)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/QueryResponse.java Sun
Dec 13 11:58:37 2009
@@ -46,6 +46,7 @@
   private NamedList<Object> _highlightingInfo = null;
   private NamedList<Object> _spellInfo = null;
   private NamedList<Object> _statsInfo = null;
+  private NamedList<Object> _termsInfo = null;
 
   // Facet stuff
   private Map<String,Integer> _facetQuery = null;
@@ -59,6 +60,9 @@
   // SpellCheck Response
   private SpellCheckResponse _spellResponse = null;
 
+  // Terms Response
+  private TermsResponse _termsResponse = null;
+  
   // Field stats Response
   private Map<String,FieldStatsInfo> _fieldStatsInfo = null;
   
@@ -118,6 +122,10 @@
         _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 @@
     _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 @@
     return _spellResponse;
   }
 
+  public TermsResponse getTermsResponse() {
+    return _termsResponse;
+  }
+  
   /**
    * See also: {@link #getLimitingFacets()}
    */

Added: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java?rev=890053&view=auto
==============================================================================
--- lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java (added)
+++ lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java Sun
Dec 13 11:58:37 2009
@@ -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;
+    }
+  }
+}

Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/src/solrj/org/apache/solr/client/solrj/response/TermsResponse.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrQueryTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrQueryTest.java?rev=890053&r1=890052&r2=890053&view=diff
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrQueryTest.java (original)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/SolrQueryTest.java Sun Dec 13
11:58:37 2009
@@ -143,4 +143,56 @@
     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);
+  }
 }

Added: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
URL: http://svn.apache.org/viewvc/lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java?rev=890053&view=auto
==============================================================================
--- lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
(added)
+++ lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
Sun Dec 13 11:58:37 2009
@@ -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);
+  }
+}

Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: lucene/solr/trunk/src/test/org/apache/solr/client/solrj/response/TermsResponseTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



Mime
View raw message