use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.
the class SolrClient method update.
/**
* Update a record in the database. Any field/value pairs in the specified values HashMap will be
* written into the record with the specified record key, overwriting any existing values with the
* same field name.
*
* @param table
* The name of the table
* @param key
* The record key of the record to write.
* @param values
* A HashMap of field/value pairs to update in the record
* @return Zero on success, a non-zero error code on error. See this class's description for a
* discussion of error codes.
*/
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
try {
SolrInputDocument updatedDoc = new SolrInputDocument();
updatedDoc.addField("id", key);
for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
updatedDoc.addField(entry.getKey(), Collections.singletonMap("set", entry.getValue()));
}
UpdateResponse writeResponse;
if (batchMode) {
writeResponse = client.add(table, updatedDoc, commitTime);
} else {
writeResponse = client.add(table, updatedDoc);
client.commit(table);
}
return checkStatus(writeResponse.getStatus());
} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
return Status.ERROR;
}
use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.
the class SolrClient method insert.
/**
* Insert a record in the database. Any field/value pairs in the specified values HashMap will be
* written into the record with the specified record key.
*
* @param table
* The name of the table
* @param key
* The record key of the record to insert.
* @param values
* A HashMap of field/value pairs to insert in the record
* @return Zero on success, a non-zero error code on error. See this class's description for a
* discussion of error codes.
*/
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
try {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", key);
for (Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
doc.addField(entry.getKey(), entry.getValue());
}
UpdateResponse response;
if (batchMode) {
response = client.add(table, doc, commitTime);
} else {
response = client.add(table, doc);
client.commit(table);
}
return checkStatus(response.getStatus());
} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
return Status.ERROR;
}
use of org.apache.solr.client.solrj.response.UpdateResponse in project YCSB by brianfrankcooper.
the class SolrClient method delete.
/**
* Delete a record from the database.
*
* @param table
* The name of the table
* @param key
* The record key of the record to delete.
* @return Zero on success, a non-zero error code on error. See this class's description for a
* discussion of error codes.
*/
@Override
public Status delete(String table, String key) {
try {
UpdateResponse response;
if (batchMode) {
response = client.deleteById(table, key, commitTime);
} else {
response = client.deleteById(table, key);
client.commit(table);
}
return checkStatus(response.getStatus());
} catch (IOException | SolrServerException e) {
e.printStackTrace();
}
return Status.ERROR;
}
use of org.apache.solr.client.solrj.response.UpdateResponse in project gora by apache.
the class SolrStore method deleteByQuery.
@Override
public long deleteByQuery(Query<K, T> query) {
UpdateResponse rsp;
try {
/*
In this If block we check whether, user needs to delete full document or some fields in the document. We can't delete fields in a document by using solr deleteByQuery method.
therefore what we have done here is setting the particular fields values into null.
*/
if (query.getFields() != null && query.getFields().length < mapping.mapping.size() && !(Arrays.asList(query.getFields()).contains(mapping.getPrimaryKey()))) {
Result<K, T> result = query.execute();
Map<String, String> partialUpdateNull = new HashMap<>();
partialUpdateNull.put("set", null);
while (result.next()) {
SolrInputDocument inputDoc = new SolrInputDocument();
inputDoc.setField(mapping.getPrimaryKey(), result.getKey());
for (String field : query.getFields()) {
inputDoc.setField(field, partialUpdateNull);
}
batch.add(inputDoc);
}
if (commitWithin == 0) {
rsp = server.add(batch);
server.commit(false, true, true);
batch.clear();
LOG.info(rsp.toString());
} else {
rsp = server.add(batch, commitWithin);
batch.clear();
LOG.info(rsp.toString());
}
} else {
SolrQuery<K, T> solrQuery = (SolrQuery<K, T>) query;
String q = solrQuery.toSolrQuery();
rsp = server.deleteByQuery(q);
server.commit();
LOG.info(rsp.toString());
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return 0;
}
use of org.apache.solr.client.solrj.response.UpdateResponse in project lucene-solr by apache.
the class TestTolerantUpdateProcessorCloud method testSanity.
public void testSanity() throws Exception {
// verify some basic sanity checking of indexing & querying across the collection
// w/o using our custom update processor chain
assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_ONE_PRE + "1"), f("foo_i", 42))).getStatus());
assertEquals(0, CLOUD_CLIENT.add(doc(f("id", S_TWO_PRE + "2"), f("foo_i", 66))).getStatus());
assertEquals(0, CLOUD_CLIENT.commit().getStatus());
for (SolrClient c : Arrays.asList(S_ONE_LEADER_CLIENT, S_TWO_LEADER_CLIENT, S_ONE_NON_LEADER_CLIENT, S_TWO_NON_LEADER_CLIENT, NO_COLLECTION_CLIENT, CLOUD_CLIENT)) {
assertQueryDocIds(c, true, S_ONE_PRE + "1", S_TWO_PRE + "2");
assertQueryDocIds(c, false, "id_not_exists");
// verify adding 2 broken docs causes a clint exception
try {
UpdateResponse rsp = update(params(), doc(f("id", S_ONE_PRE + "X"), f("foo_i", "bogus_val_X")), doc(f("id", S_TWO_PRE + "Y"), f("foo_i", "bogus_val_Y"))).process(c);
fail("did not get a top level exception when more then 10 docs failed: " + rsp.toString());
} catch (SolrException e) {
assertEquals("not the type of error we were expecting (" + e.code() + "): " + e.toString(), 400, e.code());
}
// verify malformed deleteByQuerys fail
try {
UpdateResponse rsp = update(params()).deleteByQuery("foo_i:not_a_num").process(c);
fail("sanity check for malformed DBQ didn't fail: " + rsp.toString());
} catch (SolrException e) {
assertEquals("not the expected DBQ failure: " + e.getMessage(), 400, e.code());
}
// verify opportunistic concurrency deletions fail as we expect when docs are / aren't present
for (UpdateRequest r : new UpdateRequest[] { update(params("commit", "true")).deleteById(S_ONE_PRE + "1", -1L), update(params("commit", "true")).deleteById(S_TWO_PRE + "2", -1L), update(params("commit", "true")).deleteById("id_not_exists", 1L) }) {
try {
UpdateResponse rsp = r.process(c);
fail("sanity check for opportunistic concurrency delete didn't fail: " + r.toString() + " => " + rsp.toString());
} catch (SolrException e) {
assertEquals("not the expected opportunistic concurrency failure code: " + r.toString() + " => " + e.getMessage(), 409, e.code());
}
}
}
}
Aggregations