use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.
the class DeleteDocumentationIT method testDelete.
/**
* This test documents docs/java-rest/high-level/document/delete.asciidoc
*/
public void testDelete() throws IOException {
RestHighLevelClient client = highLevelClient();
// tag::delete-request[]
DeleteRequest request = new DeleteRequest(// <1>
"index", // <2>
"type", // <3>
"id");
// end::delete-request[]
// tag::delete-request-props[]
// <1>
request.timeout(TimeValue.timeValueSeconds(1));
// <2>
request.timeout("1s");
// <3>
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
// <4>
request.setRefreshPolicy("wait_for");
// <5>
request.version(2);
// <6>
request.versionType(VersionType.EXTERNAL);
// end::delete-request-props[]
// tag::delete-execute[]
DeleteResponse response = client.delete(request);
try {
// tag::delete-notfound[]
if (response.getResult().equals(DocWriteResponse.Result.NOT_FOUND)) {
// <1>
throw new Exception("Can't find document to be removed");
}
// end::delete-notfound[]
} catch (Exception ignored) {
}
// tag::delete-execute-async[]
client.deleteAsync(request, new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
});
// tag::delete-conflict[]
try {
client.delete(request);
} catch (ElasticsearchException exception) {
if (exception.status().equals(RestStatus.CONFLICT)) {
// <1>
}
}
// end::delete-conflict[]
}
use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.
the class RequestTests method testDelete.
public void testDelete() throws IOException {
String index = randomAsciiOfLengthBetween(3, 10);
String type = randomAsciiOfLengthBetween(3, 10);
String id = randomAsciiOfLengthBetween(3, 10);
DeleteRequest deleteRequest = new DeleteRequest(index, type, id);
Map<String, String> expectedParams = new HashMap<>();
setRandomTimeout(deleteRequest, expectedParams);
setRandomRefreshPolicy(deleteRequest, expectedParams);
setRandomVersion(deleteRequest, expectedParams);
setRandomVersionType(deleteRequest, expectedParams);
if (frequently()) {
if (randomBoolean()) {
String routing = randomAsciiOfLengthBetween(3, 10);
deleteRequest.routing(routing);
expectedParams.put("routing", routing);
}
if (randomBoolean()) {
String parent = randomAsciiOfLengthBetween(3, 10);
deleteRequest.parent(parent);
expectedParams.put("parent", parent);
}
}
Request request = Request.delete(deleteRequest);
assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
assertEquals(expectedParams, request.params);
assertEquals("DELETE", request.method);
assertNull(request.entity);
}
use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.
the class IndicesRequestIT method testDelete.
public void testDelete() {
String[] deleteShardActions = new String[] { BulkAction.NAME + "[s][p]", BulkAction.NAME + "[s][r]" };
interceptTransportActions(deleteShardActions);
DeleteRequest deleteRequest = new DeleteRequest(randomIndexOrAlias(), "type", "id");
internalCluster().coordOnlyNodeClient().delete(deleteRequest).actionGet();
clearInterceptedActions();
assertSameIndices(deleteRequest, deleteShardActions);
}
use of org.elasticsearch.action.delete.DeleteRequest in project elasticsearch by elastic.
the class IndicesRequestIT method testBulk.
public void testBulk() {
String[] bulkShardActions = new String[] { BulkAction.NAME + "[s][p]", BulkAction.NAME + "[s][r]" };
interceptTransportActions(bulkShardActions);
List<String> indices = new ArrayList<>();
BulkRequest bulkRequest = new BulkRequest();
int numIndexRequests = iterations(1, 10);
for (int i = 0; i < numIndexRequests; i++) {
String indexOrAlias = randomIndexOrAlias();
bulkRequest.add(new IndexRequest(indexOrAlias, "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value"));
indices.add(indexOrAlias);
}
int numDeleteRequests = iterations(1, 10);
for (int i = 0; i < numDeleteRequests; i++) {
String indexOrAlias = randomIndexOrAlias();
bulkRequest.add(new DeleteRequest(indexOrAlias, "type", "id"));
indices.add(indexOrAlias);
}
int numUpdateRequests = iterations(1, 10);
for (int i = 0; i < numUpdateRequests; i++) {
String indexOrAlias = randomIndexOrAlias();
bulkRequest.add(new UpdateRequest(indexOrAlias, "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field1", "value1"));
indices.add(indexOrAlias);
}
internalCluster().coordOnlyNodeClient().bulk(bulkRequest).actionGet();
clearInterceptedActions();
assertIndicesSubset(indices, bulkShardActions);
}
use of org.elasticsearch.action.delete.DeleteRequest in project titan by thinkaurelius.
the class ElasticSearchIndex method mutate.
@Override
public void mutate(Map<String, Map<String, IndexMutation>> mutations, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
BulkRequestBuilder brb = client.prepareBulk();
int bulkrequests = 0;
try {
for (Map.Entry<String, Map<String, IndexMutation>> stores : mutations.entrySet()) {
String storename = stores.getKey();
for (Map.Entry<String, IndexMutation> entry : stores.getValue().entrySet()) {
String docid = entry.getKey();
IndexMutation mutation = entry.getValue();
assert mutation.isConsolidated();
Preconditions.checkArgument(!(mutation.isNew() && mutation.isDeleted()));
Preconditions.checkArgument(!mutation.isNew() || !mutation.hasDeletions());
Preconditions.checkArgument(!mutation.isDeleted() || !mutation.hasAdditions());
//Deletions first
if (mutation.hasDeletions()) {
if (mutation.isDeleted()) {
log.trace("Deleting entire document {}", docid);
brb.add(new DeleteRequest(indexName, storename, docid));
} else {
String script = getDeletionScript(informations, storename, mutation);
brb.add(client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE));
log.trace("Adding script {}", script);
}
bulkrequests++;
}
if (mutation.hasAdditions()) {
int ttl = mutation.determineTTL();
if (mutation.isNew()) {
//Index
log.trace("Adding entire document {}", docid);
brb.add(new IndexRequest(indexName, storename, docid).source(getNewDocument(mutation.getAdditions(), informations.get(storename), ttl)));
} else {
Preconditions.checkArgument(ttl == 0, "Elasticsearch only supports TTL on new documents [%s]", docid);
boolean needUpsert = !mutation.hasDeletions();
String script = getAdditionScript(informations, storename, mutation);
UpdateRequestBuilder update = client.prepareUpdate(indexName, storename, docid).setScript(script, ScriptService.ScriptType.INLINE);
if (needUpsert) {
XContentBuilder doc = getNewDocument(mutation.getAdditions(), informations.get(storename), ttl);
update.setUpsert(doc);
}
brb.add(update);
log.trace("Adding script {}", script);
}
bulkrequests++;
}
}
}
if (bulkrequests > 0) {
BulkResponse bulkItemResponses = brb.execute().actionGet();
if (bulkItemResponses.hasFailures()) {
boolean actualFailure = false;
for (BulkItemResponse response : bulkItemResponses.getItems()) {
//The document may have been deleted, which is OK
if (response.isFailed() && response.getFailure().getStatus() != RestStatus.NOT_FOUND) {
log.error("Failed to execute ES query {}", response.getFailureMessage());
actualFailure = true;
}
}
if (actualFailure) {
throw new Exception(bulkItemResponses.buildFailureMessage());
}
}
}
} catch (Exception e) {
log.error("Failed to execute ES query {}", brb.request().timeout(), e);
throw convert(e);
}
}
Aggregations