use of org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient in project lucene-solr by apache.
the class SolrExampleTests method testUpdateField.
@Test
public void testUpdateField() throws Exception {
//no versions
SolrClient client = getSolrClient();
client.deleteByQuery("*:*");
client.commit();
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "unique");
doc.addField("name", "gadget");
doc.addField("price", 1);
client.add(doc);
client.commit();
SolrQuery q = new SolrQuery("*:*");
q.setFields("id", "price", "name", "_version_");
QueryResponse resp = client.query(q);
assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
Long version = (Long) resp.getResults().get(0).getFirstValue("_version_");
assertNotNull("no version returned", version);
assertEquals(1.0f, resp.getResults().get(0).getFirstValue("price"));
//update "price" with incorrect version (optimistic locking)
//need better api for this???
HashMap<String, Object> oper = new HashMap<>();
oper.put("set", 100);
doc = new SolrInputDocument();
doc.addField("id", "unique");
doc.addField("_version_", version + 1);
doc.addField("price", oper);
try {
client.add(doc);
if (client instanceof HttpSolrClient) {
//XXX concurrent client reports exceptions differently
fail("Operation should throw an exception!");
} else {
//just to be sure the client has sent the doc
client.commit();
ErrorTrackingConcurrentUpdateSolrClient concurrentClient = (ErrorTrackingConcurrentUpdateSolrClient) client;
assertNotNull("ConcurrentUpdateSolrClient did not report an error", concurrentClient.lastError);
assertTrue("ConcurrentUpdateSolrClient did not report an error", concurrentClient.lastError.getMessage().contains("Conflict"));
}
} catch (SolrException se) {
assertTrue("No identifiable error message", se.getMessage().contains("version conflict for unique"));
}
//update "price", use correct version (optimistic locking)
doc = new SolrInputDocument();
doc.addField("id", "unique");
doc.addField("_version_", version);
doc.addField("price", oper);
client.add(doc);
client.commit();
resp = client.query(q);
assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
assertEquals("price was not updated?", 100.0f, resp.getResults().get(0).getFirstValue("price"));
assertEquals("no name?", "gadget", resp.getResults().get(0).getFirstValue("name"));
//update "price", no version
oper.put("set", 200);
doc = new SolrInputDocument();
doc.addField("id", "unique");
doc.addField("price", oper);
client.add(doc);
client.commit();
resp = client.query(q);
assertEquals("Doc count does not match", 1, resp.getResults().getNumFound());
assertEquals("price was not updated?", 200.0f, resp.getResults().get(0).getFirstValue("price"));
assertEquals("no name?", "gadget", resp.getResults().get(0).getFirstValue("name"));
}
use of org.apache.solr.client.solrj.embedded.SolrExampleStreamingTest.ErrorTrackingConcurrentUpdateSolrClient in project lucene-solr by apache.
the class SolrExampleTests method testErrorHandling.
@Test
public void testErrorHandling() throws Exception {
SolrClient client = getSolrClient();
SolrQuery query = new SolrQuery();
query.set(CommonParams.QT, "/analysis/field");
query.set(AnalysisParams.FIELD_TYPE, "int");
query.set(AnalysisParams.FIELD_VALUE, "ignore_exception");
try {
client.query(query);
Assert.fail("should have a number format exception");
} catch (SolrException ex) {
assertEquals(400, ex.code());
assertThat(ex.getMessage(), containsString("Invalid Number: ignore_exception"));
} catch (Throwable t) {
t.printStackTrace();
Assert.fail("should have thrown a SolrException! not: " + t);
}
try {
//the df=text here is a kluge for the test to supply a default field in case there is none in schema.xml
// alternatively, the resulting assertion could be modified to assert that no default field is specified.
// query syntax error
client.deleteByQuery("{!df=text} ??::?? ignore_exception");
Assert.fail("should have a number format exception");
} catch (SolrException ex) {
assertEquals(400, ex.code());
// The reason should get passed through
assertTrue(ex.getMessage().indexOf("??::?? ignore_exception") > 0);
} catch (Throwable t) {
t.printStackTrace();
Assert.fail("should have thrown a SolrException! not: " + t);
}
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "DOCID");
doc.addField("id", "DOCID2");
doc.addField("name", "hello");
if (client instanceof HttpSolrClient) {
try {
client.add(doc);
fail("Should throw exception!");
} catch (SolrException ex) {
assertEquals(400, ex.code());
assertTrue(ex.getMessage().indexOf("contains multiple values for uniqueKey") > // The reason should get passed through
0);
} catch (Throwable t) {
Assert.fail("should have thrown a SolrException! not: " + t);
}
} else if (client instanceof ErrorTrackingConcurrentUpdateSolrClient) {
//XXX concurrentupdatesolrserver reports errors differently
ErrorTrackingConcurrentUpdateSolrClient concurrentClient = (ErrorTrackingConcurrentUpdateSolrClient) client;
concurrentClient.lastError = null;
concurrentClient.add(doc);
concurrentClient.blockUntilFinished();
assertNotNull("Should throw exception!", concurrentClient.lastError);
assertEquals("Unexpected exception type", RemoteSolrException.class, concurrentClient.lastError.getClass());
assertTrue("Unexpected exception message: " + concurrentClient.lastError.getMessage(), concurrentClient.lastError.getMessage().contains("Remote error message: Document contains multiple values for uniqueKey"));
} else {
log.info("Ignoring update test for client:" + client.getClass().getName());
}
}
Aggregations