Search in sources :

Example 71 with Request

use of org.graylog.shaded.elasticsearch7.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 72 with Request

use of org.graylog.shaded.elasticsearch7.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)

Example 73 with Request

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

the class RestElasticSearchClient method countTotal.

@Override
public long countTotal(String indexName, Map<String, Object> requestData) throws IOException {
    final Request request = new Request(REQUEST_TYPE_GET, REQUEST_SEPARATOR + indexName + REQUEST_SEPARATOR + "_count");
    final byte[] requestDataBytes = mapper.writeValueAsBytes(requestData);
    if (log.isDebugEnabled()) {
        log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(requestData));
    }
    final Response response = performRequest(request, requestDataBytes);
    try (final InputStream inputStream = response.getEntity().getContent()) {
        return mapper.readValue(inputStream, RestCountResponse.class).getCount();
    }
}
Also used : RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) ESScriptResponse(org.janusgraph.diskstorage.es.script.ESScriptResponse) InputStream(java.io.InputStream) Request(org.elasticsearch.client.Request)

Example 74 with Request

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

the class RestElasticSearchClient method createStoredScript.

@Override
public void createStoredScript(String scriptName, Map<String, Object> script) throws IOException {
    Request request = new Request(REQUEST_TYPE_POST, REQUEST_SEPARATOR + "_scripts" + REQUEST_SEPARATOR + scriptName);
    performRequest(request, mapWriter.writeValueAsBytes(script));
}
Also used : Request(org.elasticsearch.client.Request)

Example 75 with Request

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

the class RestElasticSearchClient method search.

private RestSearchResponse search(Map<String, Object> requestData, String path) throws IOException {
    final Request request = new Request(REQUEST_TYPE_POST, path);
    final byte[] requestDataBytes = mapper.writeValueAsBytes(requestData);
    if (log.isDebugEnabled()) {
        log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(requestData));
    }
    final Response response = performRequest(request, requestDataBytes);
    try (final InputStream inputStream = response.getEntity().getContent()) {
        return mapper.readValue(inputStream, RestSearchResponse.class);
    }
}
Also used : RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) ESScriptResponse(org.janusgraph.diskstorage.es.script.ESScriptResponse) InputStream(java.io.InputStream) Request(org.elasticsearch.client.Request)

Aggregations

Request (org.elasticsearch.client.Request)54 Response (org.elasticsearch.client.Response)34 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 IOException (java.io.IOException)12 Request (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request)12 HttpEntity (org.apache.http.HttpEntity)11 NStringEntity (org.apache.http.nio.entity.NStringEntity)11 Map (java.util.Map)10 Collectors (java.util.stream.Collectors)10 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 InputStream (java.io.InputStream)6 StringEntity (org.apache.http.entity.StringEntity)6 RestClient (org.elasticsearch.client.RestClient)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 HashMap (java.util.HashMap)5 Locale (java.util.Locale)5 ContentType (org.apache.http.entity.ContentType)5