use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrCmdDistributorTest method testOneRetry.
private void testOneRetry() throws Exception {
final HttpSolrClient solrclient = (HttpSolrClient) clients.get(0);
long numFoundBefore = solrclient.query(new SolrQuery("*:*")).getResults().getNumFound();
final MockStreamingSolrClients streamingClients = new MockStreamingSolrClients(updateShardHandler);
SolrCmdDistributor cmdDistrib = new SolrCmdDistributor(streamingClients, 5, 0);
streamingClients.setExp(Exp.CONNECT_EXCEPTION);
ArrayList<Node> nodes = new ArrayList<>();
ZkNodeProps nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, solrclient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
final AtomicInteger retries = new AtomicInteger();
nodeProps = new ZkNodeProps(ZkStateReader.BASE_URL_PROP, solrclient.getBaseURL(), ZkStateReader.CORE_NAME_PROP, "");
RetryNode retryNode = new RetryNode(new ZkCoreNodeProps(nodeProps), null, "collection1", "shard1") {
@Override
public boolean checkRetry() {
streamingClients.setExp(null);
retries.incrementAndGet();
return true;
}
};
nodes.add(retryNode);
AddUpdateCommand cmd = new AddUpdateCommand(null);
cmd.solrDoc = sdoc("id", id.incrementAndGet());
ModifiableSolrParams params = new ModifiableSolrParams();
CommitUpdateCommand ccmd = new CommitUpdateCommand(null, false);
cmdDistrib.distribAdd(cmd, nodes, params);
cmdDistrib.distribCommit(ccmd, nodes, params);
cmdDistrib.finish();
assertEquals(1, retries.get());
long numFoundAfter = solrclient.query(new SolrQuery("*:*")).getResults().getNumFound();
// we will get java.net.ConnectException which we retry on
assertEquals(numFoundBefore + 1, numFoundAfter);
assertEquals(0, cmdDistrib.getErrors().size());
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrClientCache method getHttpSolrClient.
public synchronized HttpSolrClient getHttpSolrClient(String host) {
HttpSolrClient client;
if (solrClients.containsKey(host)) {
client = (HttpSolrClient) solrClients.get(host);
} else {
HttpSolrClient.Builder builder = new HttpSolrClient.Builder(host);
if (httpClient != null) {
builder = builder.withHttpClient(httpClient);
}
client = builder.build();
solrClients.put(host, client);
}
return client;
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrExampleJettyTest method testBadSetup.
@Test
public void testBadSetup() {
try {
// setup the server...
String url = "http" + (isSSLMode() ? "s" : "") + "://127.0.0.1/?core=xxx";
HttpSolrClient client = getHttpSolrClient(url);
Assert.fail("HttpSolrServer should not allow a path with a parameter: " + client.getBaseURL());
} catch (Exception ex) {
// expected
}
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrExampleJettyTest method testArbitraryJsonIndexing.
@Test
public void testArbitraryJsonIndexing() throws Exception {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// two docs, one with uniqueKey, another without it
String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
HttpClient httpClient = client.getHttpClient();
HttpPost post = new HttpPost(getUri(client));
post.setHeader("Content-Type", "application/json");
post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
assertEquals(200, response.getStatusLine().getStatusCode());
client.commit();
QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*"));
assertEquals(2, rsp.getResults().getNumFound());
SolrDocument doc = rsp.getResults().get(0);
String src = (String) doc.getFieldValue("_src_");
Map m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("abc1", m.get("id"));
assertEquals("name1", m.get("name"));
doc = rsp.getResults().get(1);
src = (String) doc.getFieldValue("_src_");
m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("name2", m.get("name"));
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class TestLBHttpSolrClient method addDocs.
private void addDocs(SolrInstance solrInstance) throws IOException, SolrServerException {
List<SolrInputDocument> docs = new ArrayList<>();
for (int i = 0; i < 10; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", i);
doc.addField("name", solrInstance.name);
docs.add(doc);
}
SolrResponseBase resp;
try (HttpSolrClient client = getHttpSolrClient(solrInstance.getUrl(), httpClient)) {
resp = client.add(docs);
assertEquals(0, resp.getStatus());
resp = client.commit();
assertEquals(0, resp.getStatus());
}
}
Aggregations