use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrExampleTests method testExampleConfig.
/**
* query the example
*/
@Test
public void testExampleConfig() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
String docID = "1112211111";
doc.addField("id", docID);
doc.addField("name", "my name!");
Assert.assertEquals(null, doc.getField("foo"));
Assert.assertTrue(doc.getField("name").getValue() != null);
UpdateResponse upres = client.add(doc);
// System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.commit(true, true);
// System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.optimize(true, true);
// System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
SolrQuery query = new SolrQuery();
query.setQuery("id:" + docID);
QueryResponse response = client.query(query);
Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id"));
// Now add a few docs for facet testing...
List<SolrInputDocument> docs = new ArrayList<>();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "2");
doc2.addField("inStock", true);
doc2.addField("price", 2);
doc2.addField("timestamp_dt", new java.util.Date());
docs.add(doc2);
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("id", "3");
doc3.addField("inStock", false);
doc3.addField("price", 3);
doc3.addField("timestamp_dt", new java.util.Date());
docs.add(doc3);
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField("id", "4");
doc4.addField("inStock", true);
doc4.addField("price", 4);
doc4.addField("timestamp_dt", new java.util.Date());
docs.add(doc4);
SolrInputDocument doc5 = new SolrInputDocument();
doc5.addField("id", "5");
doc5.addField("inStock", false);
doc5.addField("price", 5);
doc5.addField("timestamp_dt", new java.util.Date());
docs.add(doc5);
upres = client.add(docs);
// System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.commit(true, true);
// System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = client.optimize(true, true);
// System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
query = new SolrQuery("*:*");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
query.addFacetQuery("price:[5 TO *]");
query.addFacetField("inStock");
query.addFacetField("price");
query.addFacetField("timestamp_dt");
query.removeFilterQuery("inStock:true");
response = client.query(query);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(5, response.getResults().getNumFound());
Assert.assertEquals(3, response.getFacetQuery().size());
Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
Assert.assertEquals(4, response.getFacetField("price").getValueCount());
// test a second query, test making a copy of the main query
SolrQuery query2 = query.getCopy();
query2.addFilterQuery("inStock:true");
Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
response = client.query(query2);
Assert.assertEquals(1, query2.getFilterQueries().length);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(2, response.getResults().getNumFound());
for (SolrDocument outDoc : response.getResults()) {
assertEquals(true, outDoc.getFieldValue("inStock"));
}
// sanity check round tripping of params...
query = new SolrQuery("foo");
query.addFilterQuery("{!field f=inStock}true");
query.addFilterQuery("{!term f=name}hoss");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
response = client.query(query);
assertTrue("echoed params are not a NamedList: " + response.getResponseHeader().get("params").getClass(), response.getResponseHeader().get("params") instanceof NamedList);
NamedList echo = (NamedList) response.getResponseHeader().get("params");
List values = null;
assertEquals("foo", echo.get("q"));
assertTrue("echoed fq is not a List: " + echo.get("fq").getClass(), echo.get("fq") instanceof List);
values = (List) echo.get("fq");
Assert.assertEquals(2, values.size());
Assert.assertEquals("{!field f=inStock}true", values.get(0));
Assert.assertEquals("{!term f=name}hoss", values.get(1));
assertTrue("echoed facet.query is not a List: " + echo.get("facet.query").getClass(), echo.get("facet.query") instanceof List);
values = (List) echo.get("facet.query");
Assert.assertEquals(2, values.size());
Assert.assertEquals("price:[* TO 2]", values.get(0));
Assert.assertEquals("price:[2 TO 4]", values.get(1));
if (jetty != null) {
// check system wide system handler + "/admin/info/system"
String url = jetty.getBaseUrl().toString();
try (HttpSolrClient adminClient = getHttpSolrClient(url)) {
SolrQuery q = new SolrQuery();
q.set("qt", "/admin/info/system");
QueryResponse rsp = adminClient.query(q);
assertNotNull(rsp.getResponse().get("mode"));
assertNotNull(rsp.getResponse().get("lucene"));
}
}
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrExampleTests method testUnicode.
public void testUnicode() throws Exception {
Random random = random();
int numIterations = atLeast(3);
SolrClient client = getSolrClient();
// save the old parser, so we can set it back.
ResponseParser oldParser = null;
if (client instanceof HttpSolrClient) {
HttpSolrClient httpSolrClient = (HttpSolrClient) client;
oldParser = httpSolrClient.getParser();
}
try {
for (int iteration = 0; iteration < numIterations; iteration++) {
// choose format
if (client instanceof HttpSolrClient) {
if (random.nextBoolean()) {
((HttpSolrClient) client).setParser(new BinaryResponseParser());
} else {
((HttpSolrClient) client).setParser(new XMLResponseParser());
}
}
int numDocs = TestUtil.nextInt(random(), 1, 10 * RANDOM_MULTIPLIER);
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
List<SolrInputDocument> docs = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "" + i);
doc.addField("unicode_s", randomTestString(30));
docs.add(doc);
}
client.add(docs);
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.setRows(numDocs);
QueryResponse rsp = client.query(query);
for (int i = 0; i < numDocs; i++) {
String expected = (String) docs.get(i).getFieldValue("unicode_s");
String actual = (String) rsp.getResults().get(i).getFieldValue("unicode_s");
assertEquals(expected, actual);
}
}
} finally {
if (oldParser != null) {
// set the old parser back
((HttpSolrClient) client).setParser(oldParser);
}
}
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class TestBatchUpdate method testWithBinaryBean.
@Test
public void testWithBinaryBean() throws Exception {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
client.setRequestWriter(new BinaryRequestWriter());
// delete everything!
client.deleteByQuery("*:*");
final int[] counter = new int[1];
counter[0] = 0;
client.addBeans(new Iterator<Bean>() {
@Override
public boolean hasNext() {
return counter[0] < numdocs;
}
@Override
public Bean next() {
Bean bean = new Bean();
bean.id = "" + (++counter[0]);
bean.cat = "foocat";
return bean;
}
@Override
public void remove() {
//do nothing
}
});
client.commit();
SolrQuery query = new SolrQuery("*:*");
QueryResponse response = client.query(query);
assertEquals(0, response.getStatus());
assertEquals(numdocs, response.getResults().getNumFound());
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class TestBatchUpdate method testWithBinary.
@Test
public void testWithBinary() throws Exception {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
client.setRequestWriter(new BinaryRequestWriter());
// delete everything!
client.deleteByQuery("*:*");
doIt(client);
}
use of org.apache.solr.client.solrj.impl.HttpSolrClient in project lucene-solr by apache.
the class SolrExceptionTest method testSolrException.
public void testSolrException() throws Throwable {
// test a connection to a solr server that probably doesn't exist
// this is a very simple test and most of the test should be considered verified
// if the compiler won't let you by without the try/catch
boolean gotExpectedError = false;
CloseableHttpClient httpClient = null;
try {
// switched to a local address to avoid going out on the net, ns lookup issues, etc.
// set a 1ms timeout to let the connection fail faster.
httpClient = HttpClientUtil.createClient(null);
try (HttpSolrClient client = getHttpSolrClient("http://[ff01::114]:11235/solr/", httpClient)) {
client.setConnectionTimeout(1);
SolrQuery query = new SolrQuery("test123");
client.query(query);
}
httpClient.close();
} catch (SolrServerException sse) {
gotExpectedError = true;
/***
assertTrue(UnknownHostException.class == sse.getRootCause().getClass()
//If one is using OpenDNS, then you don't get UnknownHostException, instead you get back that the query couldn't execute
|| (sse.getRootCause().getClass() == SolrException.class && ((SolrException) sse.getRootCause()).code() == 302 && sse.getMessage().equals("Error executing query")));
***/
} finally {
if (httpClient != null)
HttpClientUtil.close(httpClient);
}
assertTrue(gotExpectedError);
}
Aggregations