Search in sources :

Example 51 with Request

use of org.elasticsearch.client.Request in project beam by apache.

the class ElasticsearchIOTestUtils method flushAndRefreshAllIndices.

static void flushAndRefreshAllIndices(RestClient restClient) throws IOException {
    Request request = new Request("POST", "/_flush");
    restClient.performRequest(request);
    request = new Request("POST", "/_refresh");
    restClient.performRequest(request);
}
Also used : Request(org.elasticsearch.client.Request)

Example 52 with Request

use of org.elasticsearch.client.Request in project beam by apache.

the class ElasticsearchIOTestUtils method closeIndex.

private static void closeIndex(RestClient restClient, String index) throws IOException {
    Request request = new Request("POST", String.format("/%s/_close", index));
    restClient.performRequest(request);
}
Also used : Request(org.elasticsearch.client.Request)

Example 53 with Request

use of org.elasticsearch.client.Request in project beam by apache.

the class ElasticsearchIOTestUtils method createIndex.

public static void createIndex(RestClient restClient, String indexName) throws IOException {
    deleteIndex(restClient, indexName);
    Request request = new Request("PUT", String.format("/%s", indexName));
    restClient.performRequest(request);
}
Also used : Request(org.elasticsearch.client.Request)

Example 54 with Request

use of org.elasticsearch.client.Request in project janusgraph by JanusGraph.

the class RestElasticSearchClient method createIndex.

@Override
public void createIndex(String indexName, Map<String, Object> settings) throws IOException {
    Request request = new Request(REQUEST_TYPE_PUT, REQUEST_SEPARATOR + indexName);
    if (majorVersion.getValue() > 6) {
        if (useMappingTypes) {
            request.addParameter(INCLUDE_TYPE_NAME_PARAMETER, "true");
        }
        if (settings != null && settings.size() > 0) {
            Map<String, Object> updatedSettings = new HashMap<>();
            updatedSettings.put("settings", settings);
            settings = updatedSettings;
        }
    }
    performRequest(request, mapWriter.writeValueAsBytes(settings));
}
Also used : HashMap(java.util.HashMap) Request(org.elasticsearch.client.Request)

Example 55 with Request

use of org.elasticsearch.client.Request in project janusgraph by JanusGraph.

the class RestElasticSearchClient method bulkRequest.

@Override
public void bulkRequest(List<ElasticSearchMutation> requests, String ingestPipeline) throws IOException {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    for (final ElasticSearchMutation request : requests) {
        Map<String, Object> requestData = new HashMap<>();
        if (useMappingTypes) {
            requestData.put("_index", request.getIndex());
            requestData.put("_type", request.getType());
            requestData.put("_id", request.getId());
        } else {
            requestData.put("_index", request.getIndex());
            requestData.put("_id", request.getId());
        }
        if (retryOnConflict != null && request.getRequestType() == ElasticSearchMutation.RequestType.UPDATE) {
            requestData.put(retryOnConflictKey, retryOnConflict);
        }
        outputStream.write(mapWriter.writeValueAsBytes(ImmutableMap.of(request.getRequestType().name().toLowerCase(), requestData)));
        outputStream.write(NEW_LINE_BYTES);
        if (request.getSource() != null) {
            outputStream.write(mapWriter.writeValueAsBytes(request.getSource()));
            outputStream.write(NEW_LINE_BYTES);
        }
    }
    final StringBuilder builder = new StringBuilder();
    if (ingestPipeline != null) {
        APPEND_OP.apply(builder).append("pipeline=").append(ingestPipeline);
    }
    if (bulkRefreshEnabled) {
        APPEND_OP.apply(builder).append("refresh=").append(bulkRefresh);
    }
    builder.insert(0, REQUEST_SEPARATOR + "_bulk");
    final Response response = performRequest(REQUEST_TYPE_POST, builder.toString(), outputStream.toByteArray());
    try (final InputStream inputStream = response.getEntity().getContent()) {
        final RestBulkResponse bulkResponse = mapper.readValue(inputStream, RestBulkResponse.class);
        final List<Object> errors = bulkResponse.getItems().stream().flatMap(item -> item.values().stream()).filter(item -> item.getError() != null && item.getStatus() != 404).map(RestBulkItemResponse::getError).collect(Collectors.toList());
        if (!errors.isEmpty()) {
            errors.forEach(error -> log.error("Failed to execute ES query: {}", error));
            throw new IOException("Failure(s) in Elasticsearch bulk request: " + errors);
        }
    }
}
Also used : ElasticSearchMutation(org.janusgraph.diskstorage.es.ElasticSearchMutation) JsonParseException(org.apache.tinkerpop.shaded.jackson.core.JsonParseException) ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) ObjectMapper(org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper) SimpleModule(org.apache.tinkerpop.shaded.jackson.databind.module.SimpleModule) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElasticSearchMutation(org.janusgraph.diskstorage.es.ElasticSearchMutation) ElasticSearchClient(org.janusgraph.diskstorage.es.ElasticSearchClient) LoggerFactory(org.slf4j.LoggerFactory) ObjectReader(org.apache.tinkerpop.shaded.jackson.databind.ObjectReader) HashMap(java.util.HashMap) Geoshape(org.janusgraph.core.attribute.Geoshape) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) Function(java.util.function.Function) UTF8_CHARSET(org.janusgraph.util.encoding.StringEncoding.UTF8_CHARSET) TypedIndexMappings(org.janusgraph.diskstorage.es.mapping.TypedIndexMappings) ImmutableList(com.google.common.collect.ImmutableList) JsonMappingException(org.apache.tinkerpop.shaded.jackson.databind.JsonMappingException) Map(java.util.Map) TypeReference(org.apache.tinkerpop.shaded.jackson.core.type.TypeReference) JsonIgnoreProperties(org.apache.tinkerpop.shaded.jackson.annotation.JsonIgnoreProperties) SerializationFeature(org.apache.tinkerpop.shaded.jackson.databind.SerializationFeature) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) IOException(java.io.IOException) Request(org.elasticsearch.client.Request) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TypelessIndexMappings(org.janusgraph.diskstorage.es.mapping.TypelessIndexMappings) List(java.util.List) Response(org.elasticsearch.client.Response) ESScriptResponse(org.janusgraph.diskstorage.es.script.ESScriptResponse) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) ElasticMajorVersion(org.janusgraph.diskstorage.es.ElasticMajorVersion) IndexMapping(org.janusgraph.diskstorage.es.mapping.IndexMapping) InputStream(java.io.InputStream) HashMap(java.util.HashMap) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) ESScriptResponse(org.janusgraph.diskstorage.es.script.ESScriptResponse)

Aggregations

Request (org.elasticsearch.client.Request)55 Response (org.elasticsearch.client.Response)35 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 IOException (java.io.IOException)12 HttpEntity (org.apache.http.HttpEntity)12 Request (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request)12 NStringEntity (org.apache.http.nio.entity.NStringEntity)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 StringEntity (org.apache.http.entity.StringEntity)7 RestClient (org.elasticsearch.client.RestClient)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 InputStream (java.io.InputStream)6 ContentType (org.apache.http.entity.ContentType)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 UncheckedIOException (java.io.UncheckedIOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4