Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failures reason to delete by query response #5095

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.rest.action.deletebyquery;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
Expand All @@ -33,6 +34,7 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
Expand Down Expand Up @@ -101,14 +103,30 @@ public void onResponse(DeleteByQueryResponse result) {
XContentBuilder builder = RestXContentBuilder.restContentBuilder(request);
RestStatus restStatus = result.status();
builder.startObject();
builder.startObject("_indices");
builder.startObject(Fields._INDICES);
for (IndexDeleteByQueryResponse indexDeleteByQueryResponse : result.getIndices().values()) {
builder.startObject(indexDeleteByQueryResponse.getIndex(), XContentBuilder.FieldCaseConversion.NONE);

builder.startObject("_shards");
builder.field("total", indexDeleteByQueryResponse.getTotalShards());
builder.field("successful", indexDeleteByQueryResponse.getSuccessfulShards());
builder.field("failed", indexDeleteByQueryResponse.getFailedShards());
builder.startObject(Fields._SHARDS);
builder.field(Fields.TOTAL, indexDeleteByQueryResponse.getTotalShards());
builder.field(Fields.SUCCESSFUL, indexDeleteByQueryResponse.getSuccessfulShards());
builder.field(Fields.FAILED, indexDeleteByQueryResponse.getFailedShards());
ShardOperationFailedException[] failures = indexDeleteByQueryResponse.getFailures();
if (failures != null && failures.length > 0) {
builder.startArray(Fields.FAILURES);
for (ShardOperationFailedException shardFailure : failures) {
builder.startObject();
if (shardFailure.index() != null) {
builder.field(Fields.INDEX, shardFailure.index());
}
if (shardFailure.shardId() != -1) {
builder.field(Fields.SHARD, shardFailure.shardId());
}
builder.field(Fields.REASON, shardFailure.reason());
builder.endObject();
}
builder.endArray();
}
builder.endObject();

builder.endObject();
Expand All @@ -131,4 +149,16 @@ public void onFailure(Throwable e) {
}
});
}

static final class Fields {
static final XContentBuilderString _INDICES = new XContentBuilderString("_indices");
static final XContentBuilderString _SHARDS = new XContentBuilderString("_shards");
static final XContentBuilderString TOTAL = new XContentBuilderString("total");
static final XContentBuilderString SUCCESSFUL = new XContentBuilderString("successful");
static final XContentBuilderString FAILED = new XContentBuilderString("failed");
static final XContentBuilderString FAILURES = new XContentBuilderString("failures");
static final XContentBuilderString INDEX = new XContentBuilderString("index");
static final XContentBuilderString SHARD = new XContentBuilderString("shard");
static final XContentBuilderString REASON = new XContentBuilderString("reason");
}
}