SOLR-6449: Add first class support for Real Time Get in Solrj

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1654420 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2015-01-23 22:25:34 +00:00
parent 9f909988f8
commit ff4e2c66e0
3 changed files with 176 additions and 0 deletions

View File

@ -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)

View File

@ -26,13 +26,19 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
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;
@ -331,6 +337,56 @@ public abstract class SolrClient implements Serializable {
return req.process(this);
}
/**
* 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
*/

View File

@ -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"));
}
}