use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest 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.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project wonderdog by infochimps-labs.
the class ElasticSearchStreamingRecordWriter method index.
private void index(String json) throws IOException {
Map<String, Object> record = mapper.readValue(json, Map.class);
IndexRequest request = null;
if (record.containsKey(idFieldName)) {
Object idValue = record.get(idFieldName);
request = Requests.indexRequest(indexNameForRecord(record)).id(String.valueOf(idValue)).type(mappingNameForRecord(record)).create(false).source(json);
} else {
request = Requests.indexRequest(indexNameForRecord(record)).type(mappingNameForRecord(record)).source(json);
}
if (record.containsKey(routingFieldName)) {
Object routingValue = record.get(routingFieldName);
request.routing(String.valueOf(routingValue));
}
currentRequest.add(request);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project nutch by apache.
the class ElasticIndexWriter method write.
@Override
public void write(NutchDocument doc) throws IOException {
String id = (String) doc.getFieldValue("id");
String type = doc.getDocumentMeta().get("type");
if (type == null)
type = "doc";
// Add each field of this doc to the index builder
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
for (final Map.Entry<String, NutchField> e : doc) {
final List<Object> values = e.getValue().getValues();
if (values.size() > 1) {
builder.array(e.getKey(), values);
} else {
Object value = values.get(0);
if (value instanceof java.util.Date) {
value = DateTimeFormatter.ISO_INSTANT.format(((java.util.Date) value).toInstant());
}
builder.field(e.getKey(), value);
}
}
builder.endObject();
IndexRequest request = new IndexRequest(defaultIndex).id(id).source(builder);
request.opType(DocWriteRequest.OpType.INDEX);
bulkProcessor.add(request);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project sonarqube by SonarSource.
the class EsRequestDetailsTest method should_format_IndexRequest.
@Test
public void should_format_IndexRequest() {
IndexRequest indexRequest = new IndexRequest().index("index-1").type("type-1").id("id-1");
assertThat(EsRequestDetails.computeDetailsAsString(indexRequest)).isEqualTo("ES index request for key 'id-1' on index 'index-1' on type 'type-1'");
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest in project vorto by eclipse.
the class ElasticSearchService method createIndexRequest.
private IndexRequest createIndexRequest(ModelInfo modelInfo, String tenantId) {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put(TENANT_ID, tenantId);
for (IIndexFieldExtractor extractor : fieldExtractors) {
extractor.extractFields(modelInfo).forEach((key, value) -> {
jsonMap.put(key, value);
});
}
return new IndexRequest(VORTO_INDEX, DOC, modelInfo.getId().getPrettyFormat()).source(jsonMap);
}
Aggregations