use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTestsBase method testCommitWithinOnDelete.
@Test
public void testCommitWithinOnDelete() throws Exception {
// make sure it is empty...
SolrClient client = getSolrClient();
// delete everything!
client.deleteByQuery("*:*");
client.commit();
QueryResponse rsp = client.query(new SolrQuery("*:*"));
Assert.assertEquals(0, rsp.getResults().getNumFound());
// Now add one document...
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField("id", "id3");
doc3.addField("name", "doc3");
doc3.addField("price", 10);
client.add(doc3);
client.commit();
// now check that it comes out...
rsp = client.query(new SolrQuery("id:id3"));
Assert.assertEquals(1, rsp.getResults().getNumFound());
// now test commitWithin on a delete
UpdateRequest up = new UpdateRequest();
up.setCommitWithin(1000);
up.deleteById("id3");
up.process(client);
// the document should still be there
rsp = client.query(new SolrQuery("id:id3"));
Assert.assertEquals(1, rsp.getResults().getNumFound());
// check if the doc has been deleted every 250 ms for 30 seconds
TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS);
do {
// wait 250 ms
Thread.sleep(250);
rsp = client.query(new SolrQuery("id:id3"));
if (rsp.getResults().getNumFound() == 0) {
return;
}
} while (!timeout.hasTimedOut());
Assert.fail("commitWithin failed to commit");
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTestsBase method testStreamingRequest.
@Test
public void testStreamingRequest() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// Add some docs to the index
UpdateRequest req = new UpdateRequest();
for (int i = 0; i < 10; i++) {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "" + i);
doc.addField("cat", "foocat");
req.add(doc);
}
req.setAction(ACTION.COMMIT, true, true);
req.process(client);
// Make sure it ran OK
SolrQuery query = new SolrQuery("*:*");
query.set(CommonParams.FL, "id,score,_docid_");
QueryResponse response = client.query(query);
assertEquals(0, response.getStatus());
assertEquals(10, response.getResults().getNumFound());
// Now make sure each document gets output
final AtomicInteger cnt = new AtomicInteger(0);
client.queryAndStreamResponse(query, new StreamingResponseCallback() {
@Override
public void streamDocListInfo(long numFound, long start, Float maxScore) {
assertEquals(10, numFound);
}
@Override
public void streamSolrDocument(SolrDocument doc) {
cnt.incrementAndGet();
// Make sure the transformer works for streaming
Float score = (Float) doc.get("score");
assertEquals("should have score", new Float(1.0), score);
}
});
assertEquals(10, cnt.get());
}
use of org.apache.solr.client.solrj.response.QueryResponse 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.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTests method testAddRetrieve.
/**
* query the example
*/
@Test
public void testAddRetrieve() throws Exception {
SolrClient client = getSolrClient();
// Empty the database...
// delete everything!
client.deleteByQuery("*:*");
// Now add something...
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField("id", "id1");
doc1.addField("name", "doc1");
doc1.addField("price", 10);
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField("id", "id2");
doc2.addField("name", "h𩬅llo");
doc2.addField("price", 20);
Collection<SolrInputDocument> docs = new ArrayList<>();
docs.add(doc1);
docs.add(doc2);
// Add the documents
client.add(docs);
client.commit();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
query.addSort(new SolrQuery.SortClause("price", SolrQuery.ORDER.asc));
QueryResponse rsp = client.query(query);
assertEquals(2, rsp.getResults().getNumFound());
// System.out.println( rsp.getResults() );
// Now do it again
client.add(docs);
client.commit();
rsp = client.query(query);
assertEquals(2, rsp.getResults().getNumFound());
// System.out.println( rsp.getResults() );
// query outside ascii range
query.setQuery("name:h𩬅llo");
rsp = client.query(query);
assertEquals(1, rsp.getResults().getNumFound());
}
use of org.apache.solr.client.solrj.response.QueryResponse in project lucene-solr by apache.
the class SolrExampleTests method testContentStreamRequest.
@Test
public void testContentStreamRequest() throws Exception {
SolrClient client = getSolrClient();
// delete everything!
client.deleteByQuery("*:*");
client.commit();
QueryResponse rsp = client.query(new SolrQuery("*:*"));
Assert.assertEquals(0, rsp.getResults().getNumFound());
ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
up.addFile(getFile("solrj/books.csv"), "application/csv");
up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
NamedList<Object> result = client.request(up);
assertNotNull("Couldn't upload books.csv", result);
rsp = client.query(new SolrQuery("*:*"));
Assert.assertEquals(10, rsp.getResults().getNumFound());
}
Aggregations