You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by sh...@apache.org on 2015/01/23 23:25:35 UTC

svn commit: r1654420 - in /lucene/dev/trunk/solr: CHANGES.txt solrj/src/java/org/apache/solr/client/solrj/SolrClient.java solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java

Author: shalin
Date: Fri Jan 23 22:25:34 2015
New Revision: 1654420

URL: http://svn.apache.org/r1654420
Log:
SOLR-6449: Add first class support for Real Time Get in Solrj

Added:
    lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java   (with props)
Modified:
    lucene/dev/trunk/solr/CHANGES.txt
    lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java

Modified: lucene/dev/trunk/solr/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/CHANGES.txt?rev=1654420&r1=1654419&r2=1654420&view=diff
==============================================================================
--- lucene/dev/trunk/solr/CHANGES.txt (original)
+++ lucene/dev/trunk/solr/CHANGES.txt Fri Jan 23 22:25:34 2015
@@ -82,6 +82,9 @@ New Features
 
 * SOLR-6845: Add a “buildOnStartup” option for suggesters. (Tomás Fernández Löbbe)
 
+* SOLR-6449: Add first class support for Real Time Get in Solrj.
+  (Anurag Sharma, Steve Davids via shalin)
+
 Other Changes
 ----------------------
 * SOLR-7014: Collapse identical catch branches in try-catch statements. (shalin)

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java?rev=1654420&r1=1654419&r2=1654420&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/client/solrj/SolrClient.java Fri Jan 23 22:25:34 2015
@@ -26,13 +26,19 @@ import org.apache.solr.client.solrj.requ
 import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.client.solrj.response.SolrPingResponse;
 import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
 import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.StringUtils;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.ModifiableSolrParams;
 import org.apache.solr.common.params.SolrParams;
 import org.apache.solr.common.util.NamedList;
 
 import java.io.IOException;
 import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 
@@ -332,6 +338,56 @@ public abstract class SolrClient impleme
   }
 
   /**
+   * Retrieves the SolrDocument associated with the given identifier.
+   *
+   * @return retrieved SolrDocument, null if no document is found.
+   */
+  public SolrDocument getById(String id) throws SolrServerException {
+    return getById(id, null);
+  }
+
+  /**
+   * Retrieves the SolrDocument associated with the given identifier and uses
+   * the SolrParams to execute the request.
+   *
+   * @return retrieved SolrDocument, null if no document is found.
+   */
+  public SolrDocument getById(String id, SolrParams params) throws SolrServerException {
+    SolrDocumentList docs = getById(Arrays.asList(id), params);
+    if (!docs.isEmpty()) {
+      return docs.get(0);
+    }
+    return null;
+  }
+
+  /**
+   * Retrieves the SolrDocuments associated with the given identifiers.
+   * If a document was not found, it will not be added to the SolrDocumentList.
+   */
+  public SolrDocumentList getById(Collection<String> ids) throws SolrServerException {
+    return getById(ids, null);
+  }
+
+  /**
+   * Retrieves the SolrDocuments associated with the given identifiers and uses
+   * the SolrParams to execute the request.
+   * If a document was not found, it will not be added to the SolrDocumentList.
+   */
+  public SolrDocumentList getById(Collection<String> ids, SolrParams params) throws SolrServerException {
+    if (ids == null || ids.isEmpty()) {
+      throw new IllegalArgumentException("Must provide an identifier of a document to retrieve.");
+    }
+
+    ModifiableSolrParams reqParams = new ModifiableSolrParams(params);
+    if (StringUtils.isEmpty(reqParams.get(CommonParams.QT))) {
+      reqParams.set(CommonParams.QT, "/get");
+    }
+    reqParams.set("ids", (String[]) ids.toArray());
+
+    return query(reqParams).getResults();
+  }
+  
+  /**
    * SolrServer implementations need to implement how a request is actually processed
    */
   public abstract NamedList<Object> request(final SolrRequest request) throws SolrServerException, IOException;

Added: lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java?rev=1654420&view=auto
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java (added)
+++ lucene/dev/trunk/solr/solrj/src/test/org/apache/solr/client/solrj/GetByIdTest.java Fri Jan 23 22:25:34 2015
@@ -0,0 +1,117 @@
+package org.apache.solr.client.solrj;
+
+/*
+ * 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.io.File;
+import java.util.Arrays;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.solr.SolrJettyTestBase;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.params.CommonParams;
+import org.apache.solr.common.params.SolrParams;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class GetByIdTest extends SolrJettyTestBase {
+  
+  @BeforeClass
+  public static void beforeClass() throws Exception {
+    initCore();
+  }
+  
+  @Before
+  @Override
+  public void setUp() throws Exception {
+    super.setUp();
+    getSolrClient().deleteByQuery("*:*");
+    getSolrClient().add(Arrays.asList(
+        sdoc("id", "1", "term_s", "Microsoft", "term2_s", "MSFT"),
+        sdoc("id", "2", "term_s", "Apple", "term2_s", "AAPL"),
+        sdoc("id", "3", "term_s", "Yahoo", "term2_s", "YHOO")));
+    
+    getSolrClient().commit(true, true);
+  }
+  
+  @Test
+  public void testGetId() throws Exception {
+    SolrDocument rsp = getSolrClient().getById("0");
+    assertNull(rsp);
+    
+    rsp = getSolrClient().getById("1");
+    assertEquals("1", rsp.get("id"));
+    assertEquals("Microsoft", rsp.get("term_s"));
+    assertEquals("MSFT", rsp.get("term2_s"));
+
+    rsp = getSolrClient().getById("2");    
+    assertEquals("2", rsp.get("id"));
+    assertEquals("Apple", rsp.get("term_s"));
+    assertEquals("AAPL", rsp.get("term2_s"));
+  }
+  
+  @Test
+  public void testGetIdWithParams() throws Exception {
+    final SolrParams ID_FL_ONLY = params(CommonParams.FL, "id");
+    
+    SolrDocument rsp = getSolrClient().getById("0", ID_FL_ONLY);
+    assertNull(rsp);
+    
+    rsp = getSolrClient().getById("1", ID_FL_ONLY);
+    assertEquals("1", rsp.get("id"));
+    assertNull("This field should have been removed from the response.", rsp.get("term_s"));
+    assertNull("This field should have been removed from the response.", rsp.get("term2_s"));
+
+    rsp = getSolrClient().getById("2", ID_FL_ONLY);    
+    assertEquals("2", rsp.get("id"));
+    assertNull("This field should have been removed from the response.", rsp.get("term_s"));
+    assertNull("This field should have been removed from the response.", rsp.get("term2_s"));
+  }
+
+  @Test
+  public void testGetIds() throws Exception {
+    SolrDocumentList rsp = getSolrClient().getById(Arrays.asList("0", "1", "2", "3", "4"));
+    assertEquals(3, rsp.getNumFound());
+    assertEquals("1", rsp.get(0).get("id"));
+    assertEquals("Microsoft", rsp.get(0).get("term_s"));
+    assertEquals("MSFT", rsp.get(0).get("term2_s"));
+    
+    assertEquals("2", rsp.get(1).get("id"));
+    assertEquals("Apple", rsp.get(1).get("term_s"));
+    assertEquals("AAPL", rsp.get(1).get("term2_s"));
+    
+    assertEquals("3", rsp.get(2).get("id"));
+    assertEquals("Yahoo", rsp.get(2).get("term_s"));
+    assertEquals("YHOO", rsp.get(2).get("term2_s"));
+  }
+  
+  @Test
+  public void testGetIdsWithParams() throws Exception {
+    SolrDocumentList rsp = getSolrClient().getById(Arrays.asList("0", "1", "2"), params(CommonParams.FL, "id"));
+    assertEquals(2, rsp.getNumFound());
+    
+    assertEquals("1", rsp.get(0).get("id"));
+    assertNull("This field should have been removed from the response.", rsp.get(0).get("term_s"));
+    assertNull("This field should have been removed from the response.", rsp.get(0).get("term2_s"));
+    
+    assertEquals("2", rsp.get(1).get("id"));
+    assertNull("This field should have been removed from the response.", rsp.get(1).get("term_s"));
+    assertNull("This field should have been removed from the response.", rsp.get(1).get("term2_s"));
+  }
+}