Search in sources :

Example 86 with IndexRequest

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;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) ContentType(org.apache.http.entity.ContentType) XContentType(org.elasticsearch.xcontent.XContentType) HttpEntity(org.apache.http.HttpEntity) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IndexRequest(org.elasticsearch.action.index.IndexRequest) VersionType(org.elasticsearch.index.VersionType) XContentType(org.elasticsearch.xcontent.XContentType) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) XContentBuilder(org.elasticsearch.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef) XContentParser(org.elasticsearch.xcontent.XContentParser)

Example 87 with IndexRequest

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);
}
Also used : IndexRequest(org.elasticsearch.action.index.IndexRequest)

Example 88 with IndexRequest

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);
}
Also used : NutchField(org.apache.nutch.indexer.NutchField) IndexRequest(org.elasticsearch.action.index.IndexRequest) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 89 with IndexRequest

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'");
}
Also used : CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Test(org.junit.Test)

Example 90 with IndexRequest

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);
}
Also used : IIndexFieldExtractor(org.eclipse.vorto.repository.search.extractor.IIndexFieldExtractor) IndexRequest(org.elasticsearch.action.index.IndexRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Aggregations

IndexRequest (org.elasticsearch.action.index.IndexRequest)187 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)37 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)37 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)34 IOException (java.io.IOException)30 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)27 Test (org.junit.Test)27 ElasticsearchException (org.elasticsearch.ElasticsearchException)21 IndexResponse (org.elasticsearch.action.index.IndexResponse)20 HashMap (java.util.HashMap)17 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)17 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)17 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)17 Map (java.util.Map)15 GetRequest (org.elasticsearch.action.get.GetRequest)14 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)12 BytesReference (org.elasticsearch.common.bytes.BytesReference)12 ArrayList (java.util.ArrayList)11 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)10 BulkItemResponse (org.elasticsearch.action.bulk.BulkItemResponse)9