use of org.elasticsearch.action.DocWriteResponse in project elasticsearch by elastic.
the class TransportShardBulkActionTests method testUpdateReplicaRequestWithSuccess.
public void testUpdateReplicaRequestWithSuccess() throws Exception {
DocWriteRequest writeRequest = new IndexRequest("index", "type", "id").source(Requests.INDEX_CONTENT_TYPE, "field", "value");
BulkItemRequest replicaRequest = new BulkItemRequest(0, writeRequest);
boolean created = randomBoolean();
Translog.Location resultLocation = new Translog.Location(42, 42, 42);
Engine.IndexResult indexResult = new FakeResult(1, 1, created, resultLocation);
DocWriteResponse indexResponse = new IndexResponse(shardId, "index", "id", 1, 1, created);
BulkItemResultHolder goodResults = new BulkItemResultHolder(indexResponse, indexResult, replicaRequest);
Translog.Location originalLocation = new Translog.Location(21, 21, 21);
BulkItemRequest[] items = new BulkItemRequest[0];
BulkShardRequest bulkShardRequest = new BulkShardRequest(shardId, RefreshPolicy.NONE, items);
Translog.Location newLocation = TransportShardBulkAction.updateReplicaRequest(goodResults, DocWriteRequest.OpType.INDEX, originalLocation, bulkShardRequest);
BulkItemResponse primaryResponse = replicaRequest.getPrimaryResponse();
// Check that the translog is successfully advanced
assertThat(newLocation, equalTo(resultLocation));
// Since this was not a conflict failure, the primary response
// should be filled out with the failure information
assertThat(primaryResponse.getItemId(), equalTo(0));
assertThat(primaryResponse.getId(), equalTo("id"));
assertThat(primaryResponse.getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
DocWriteResponse response = primaryResponse.getResponse();
assertThat(response.status(), equalTo(created ? RestStatus.CREATED : RestStatus.OK));
}
use of org.elasticsearch.action.DocWriteResponse in project yacy_grid_mcp by yacy.
the class ElasticsearchClient method writeMapBulk.
/**
* bulk message write
* @param jsonMapList
* a list of json documents to be indexed
* @param indexName
* the name of the index
* @param typeName
* the type of the index
* @return a list with error messages.
* The key is the id of the document, the value is an error string.
* The method was only successful if this list is empty.
* This must be a list, because keys may appear several times.
*/
public BulkWriteResult writeMapBulk(final String indexName, final List<BulkEntry> jsonMapList) {
long start = System.currentTimeMillis();
BulkRequestBuilder bulkRequest = elasticsearchClient.prepareBulk();
for (BulkEntry be : jsonMapList) {
if (be.id == null)
continue;
bulkRequest.add(elasticsearchClient.prepareIndex(indexName, be.type, be.id).setSource(be.jsonMap).setVersion(1).setCreate(// enforces OpType.INDEX
false).setVersionType(VersionType.EXTERNAL_GTE));
}
BulkResponse bulkResponse = bulkRequest.get();
BulkWriteResult result = new BulkWriteResult();
for (BulkItemResponse r : bulkResponse.getItems()) {
String id = r.getId();
DocWriteResponse response = r.getResponse();
if (response.getResult() == DocWriteResponse.Result.CREATED)
result.created.add(id);
String err = r.getFailureMessage();
if (err != null) {
result.errors.put(id, err);
}
}
long duration = Math.max(1, System.currentTimeMillis() - start);
long regulator = 0;
int created = result.created.size();
long ops = created * 1000 / duration;
if (duration > throttling_time_threshold && ops < throttling_ops_threshold) {
regulator = (long) (throttling_factor * duration);
try {
Thread.sleep(regulator);
} catch (InterruptedException e) {
}
}
Data.logger.info("elastic write bulk to index " + indexName + ": " + jsonMapList.size() + " entries, " + result.created.size() + " created, " + result.errors.size() + " errors, " + duration + " ms" + (regulator == 0 ? "" : ", throttled with " + regulator + " ms") + ", " + ops + " objects/second");
return result;
}
use of org.elasticsearch.action.DocWriteResponse in project metron by apache.
the class ElasticsearchBulkDocumentWriterTest method setupElasticsearchToSucceed.
private void setupElasticsearchToSucceed() throws IOException {
final String documentId = UUID.randomUUID().toString();
final boolean isFailed = false;
final int itemID = 0;
// the write response will contain what is used as the document ID
DocWriteResponse writeResponse = mock(DocWriteResponse.class);
when(writeResponse.getId()).thenReturn(documentId);
// define the item level response
BulkItemResponse itemResponse = mock(BulkItemResponse.class);
when(itemResponse.isFailed()).thenReturn(isFailed);
when(itemResponse.getItemId()).thenReturn(itemID);
when(itemResponse.getResponse()).thenReturn(writeResponse);
List<BulkItemResponse> itemsResponses = Collections.singletonList(itemResponse);
// define the bulk response to indicate success
BulkResponse response = mock(BulkResponse.class);
when(response.iterator()).thenReturn(itemsResponses.iterator());
when(response.hasFailures()).thenReturn(isFailed);
// have the client return the mock response
when(highLevelClient.bulk(any(BulkRequest.class))).thenReturn(response);
}
Aggregations