use of org.elasticsearch.action.update.UpdateRequest in project flink by apache.
the class RowElasticsearchEmitter method processUpsert.
private void processUpsert(RowData row, RequestIndexer indexer) {
final byte[] document = serializationSchema.serialize(row);
final String key = createKey.apply(row);
if (key != null) {
final UpdateRequest updateRequest = new UpdateRequest(indexGenerator.generate(row), documentType, key).doc(document, contentType).upsert(document, contentType);
indexer.add(updateRequest);
} else {
final IndexRequest indexRequest = new IndexRequest(indexGenerator.generate(row), documentType).id(key).source(document, contentType);
indexer.add(indexRequest);
}
}
use of org.elasticsearch.action.update.UpdateRequest in project snow-owl by b2ihealthcare.
the class RestHighLevelClientExt method bulk.
static Request bulk(BulkRequest bulkRequest) throws IOException {
// Bulk API only supports newline delimited JSON or Smile. Before executing
// the bulk, we need to check that all requests have the same content-type
// and this content-type is supported by the Bulk API.
XContentType bulkContentType = null;
String index = null;
for (int i = 0; i < bulkRequest.numberOfActions(); i++) {
DocWriteRequest<?> writeRequest = bulkRequest.requests().get(i);
index = enforceSameIndex(writeRequest.index(), index);
// Remove index property, as it will be encoded in the request path
DocWriteRequest.OpType opType = writeRequest.opType();
switch(opType) {
// $FALL-THROUGH$
case INDEX:
case CREATE:
bulkContentType = enforceSameContentType((IndexRequest) writeRequest, bulkContentType);
((IndexRequest) writeRequest).index(null);
break;
case DELETE:
((DeleteRequest) writeRequest).index(null);
break;
case UPDATE:
UpdateRequest updateRequest = (UpdateRequest) writeRequest;
if (updateRequest.doc() != null) {
bulkContentType = enforceSameContentType(updateRequest.doc(), bulkContentType);
}
if (updateRequest.upsertRequest() != null) {
bulkContentType = enforceSameContentType(updateRequest.upsertRequest(), bulkContentType);
}
updateRequest.index(null);
break;
}
}
if (bulkContentType == null) {
bulkContentType = XContentType.JSON;
}
String endpoint = endpoint(index, "_bulk");
Request request = new Request(HttpPost.METHOD_NAME, endpoint);
if (bulkRequest.timeout() != null) {
request.addParameter("timeout", bulkRequest.timeout().getStringRep());
}
if (bulkRequest.getRefreshPolicy() != WriteRequest.RefreshPolicy.NONE) {
request.addParameter("refresh", bulkRequest.getRefreshPolicy().getValue());
}
final byte separator = bulkContentType.xContent().streamSeparator();
final ContentType requestContentType = RequestConverters.createContentType(bulkContentType);
ByteArrayOutputStream content = new ByteArrayOutputStream();
for (DocWriteRequest<?> writeRequest : bulkRequest.requests()) {
DocWriteRequest.OpType opType = writeRequest.opType();
try (XContentBuilder metadata = XContentBuilder.builder(bulkContentType.xContent())) {
metadata.startObject();
{
metadata.startObject(opType.getLowercase());
if (Strings.hasLength(writeRequest.index())) {
metadata.field("_index", writeRequest.index());
}
if (Strings.hasLength(writeRequest.id())) {
metadata.field("_id", writeRequest.id());
}
if (Strings.hasLength(writeRequest.routing())) {
metadata.field("routing", writeRequest.routing());
}
if (writeRequest.version() != Versions.MATCH_ANY) {
metadata.field("version", writeRequest.version());
}
VersionType versionType = writeRequest.versionType();
if (versionType != VersionType.INTERNAL) {
if (versionType == VersionType.EXTERNAL) {
metadata.field("version_type", "external");
} else if (versionType == VersionType.EXTERNAL_GTE) {
metadata.field("version_type", "external_gte");
}
}
if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
IndexRequest indexRequest = (IndexRequest) writeRequest;
if (Strings.hasLength(indexRequest.getPipeline())) {
metadata.field("pipeline", indexRequest.getPipeline());
}
} else if (opType == DocWriteRequest.OpType.UPDATE) {
UpdateRequest updateRequest = (UpdateRequest) writeRequest;
if (updateRequest.retryOnConflict() > 0) {
metadata.field("retry_on_conflict", updateRequest.retryOnConflict());
}
if (updateRequest.fetchSource() != null) {
metadata.field("_source", updateRequest.fetchSource());
}
}
metadata.endObject();
}
metadata.endObject();
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
content.write(separator);
}
BytesRef source = null;
if (opType == DocWriteRequest.OpType.INDEX || opType == DocWriteRequest.OpType.CREATE) {
IndexRequest indexRequest = (IndexRequest) writeRequest;
BytesReference indexSource = indexRequest.source();
XContentType indexXContentType = indexRequest.getContentType();
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, indexSource, indexXContentType)) {
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
builder.copyCurrentStructure(parser);
source = BytesReference.bytes(builder).toBytesRef();
}
}
} else if (opType == DocWriteRequest.OpType.UPDATE) {
source = XContentHelper.toXContent((UpdateRequest) writeRequest, bulkContentType, false).toBytesRef();
}
if (source != null) {
content.write(source.bytes, source.offset, source.length);
content.write(separator);
}
}
HttpEntity entity = new ByteArrayEntity(content.toByteArray(), 0, content.size(), requestContentType);
request.setEntity(entity);
return request;
}
use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.
the class TransportShardBulkAction method executeBulkItemRequest.
/** Executes bulk item requests and handles request execution exceptions */
static Translog.Location executeBulkItemRequest(IndexMetaData metaData, IndexShard primary, BulkShardRequest request, Translog.Location location, int requestIndex, UpdateHelper updateHelper, LongSupplier nowInMillisSupplier, final MappingUpdatePerformer mappingUpdater) throws Exception {
final DocWriteRequest itemRequest = request.items()[requestIndex].request();
final DocWriteRequest.OpType opType = itemRequest.opType();
final BulkItemResultHolder responseHolder;
switch(itemRequest.opType()) {
case CREATE:
case INDEX:
responseHolder = executeIndexRequest((IndexRequest) itemRequest, request.items()[requestIndex], primary, mappingUpdater);
break;
case UPDATE:
responseHolder = executeUpdateRequest((UpdateRequest) itemRequest, primary, metaData, request, requestIndex, updateHelper, nowInMillisSupplier, mappingUpdater);
break;
case DELETE:
responseHolder = executeDeleteRequest((DeleteRequest) itemRequest, request.items()[requestIndex], primary);
break;
default:
throw new IllegalStateException("unexpected opType [" + itemRequest.opType() + "] found");
}
final BulkItemRequest replicaRequest = responseHolder.replicaRequest;
// update the bulk item request because update request execution can mutate the bulk item request
request.items()[requestIndex] = replicaRequest;
// Modify the replica request, if needed, and return a new translog location
location = updateReplicaRequest(responseHolder, opType, location, request);
assert replicaRequest.getPrimaryResponse() != null : "replica request must have a primary response";
return location;
}
use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.
the class DocWriteRequest method readDocumentRequest.
/** read a document write (index/delete/update) request */
static DocWriteRequest readDocumentRequest(StreamInput in) throws IOException {
byte type = in.readByte();
DocWriteRequest docWriteRequest;
if (type == 0) {
IndexRequest indexRequest = new IndexRequest();
indexRequest.readFrom(in);
docWriteRequest = indexRequest;
} else if (type == 1) {
DeleteRequest deleteRequest = new DeleteRequest();
deleteRequest.readFrom(in);
docWriteRequest = deleteRequest;
} else if (type == 2) {
UpdateRequest updateRequest = new UpdateRequest();
updateRequest.readFrom(in);
docWriteRequest = updateRequest;
} else {
throw new IllegalStateException("invalid request type [" + type + " ]");
}
return docWriteRequest;
}
use of org.elasticsearch.action.update.UpdateRequest in project elasticsearch by elastic.
the class BulkRequestTests method testBulkRequestWithRefresh.
// issue 7361
public void testBulkRequestWithRefresh() throws Exception {
BulkRequest bulkRequest = new BulkRequest();
// We force here a "id is missing" validation error
bulkRequest.add(new DeleteRequest("index", "type", null).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
// We force here a "type is missing" validation error
bulkRequest.add(new DeleteRequest("index", null, "id"));
bulkRequest.add(new DeleteRequest("index", "type", "id").setRefreshPolicy(RefreshPolicy.IMMEDIATE));
bulkRequest.add(new UpdateRequest("index", "type", "id").doc("{}", XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
bulkRequest.add(new IndexRequest("index", "type", "id").source("{}", XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE));
ActionRequestValidationException validate = bulkRequest.validate();
assertThat(validate, notNullValue());
assertThat(validate.validationErrors(), not(empty()));
assertThat(validate.validationErrors(), contains("RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.", "id is missing", "type is missing", "RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.", "RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead.", "RefreshPolicy is not supported on an item request. Set it on the BulkRequest instead."));
}
Aggregations