Search in sources :

Example 56 with Response

use of org.elasticsearch.client.Response 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) {
        final Map actionData = ImmutableMap.of(request.getRequestType().name().toLowerCase(), ImmutableMap.of("_index", request.getIndex(), "_type", request.getType(), "_id", request.getId()));
        outputStream.write(mapWriter.writeValueAsBytes(actionData));
        outputStream.write("\n".getBytes(UTF8_CHARSET));
        if (request.getSource() != null) {
            outputStream.write(mapWriter.writeValueAsBytes(request.getSource()));
            outputStream.write("\n".getBytes(UTF8_CHARSET));
        }
    }
    final StringBuilder builder = new StringBuilder();
    if (ingestPipeline != null) {
        APPEND_OP.apply(builder).append("pipeline=").append(ingestPipeline);
    }
    if (bulkRefresh != null && !bulkRefresh.toLowerCase().equals("false")) {
        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) RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) 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) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) 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) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) IndexMappings(org.janusgraph.diskstorage.es.IndexMappings) List(java.util.List) IndexMapping(org.janusgraph.diskstorage.es.IndexMappings.IndexMapping) Response(org.elasticsearch.client.Response) ObjectWriter(org.apache.tinkerpop.shaded.jackson.databind.ObjectWriter) ElasticMajorVersion(org.janusgraph.diskstorage.es.ElasticMajorVersion) Collections(java.util.Collections) InputStream(java.io.InputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 57 with Response

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

the class RestElasticSearchClient method getMajorVersion.

@Override
public ElasticMajorVersion getMajorVersion() {
    if (majorVersion != null) {
        return majorVersion;
    }
    majorVersion = DEFAULT_VERSION;
    try {
        final Response response = delegate.performRequest(REQUEST_TYPE_GET, REQUEST_SEPARATOR);
        try (final InputStream inputStream = response.getEntity().getContent()) {
            final ClusterInfo info = mapper.readValue(inputStream, ClusterInfo.class);
            majorVersion = ElasticMajorVersion.parse(info.getVersion() != null ? (String) info.getVersion().get("number") : null);
        }
    } catch (final IOException e) {
        log.warn("Unable to determine Elasticsearch server version. Default to {}.", majorVersion, e);
    }
    return majorVersion;
}
Also used : RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 58 with Response

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

the class RestElasticSearchClient method clusterHealthRequest.

@Override
public void clusterHealthRequest(String timeout) throws IOException {
    final Map<String, String> params = ImmutableMap.of("wait_for_status", "yellow", "timeout", timeout);
    final Response response = delegate.performRequest(REQUEST_TYPE_GET, REQUEST_SEPARATOR + "_cluster" + REQUEST_SEPARATOR + "health", params);
    try (final InputStream inputStream = response.getEntity().getContent()) {
        final Map<String, Object> values = mapReader.readValue(inputStream);
        if (!values.containsKey("timed_out")) {
            throw new IOException("Unexpected response for Elasticsearch cluster health request");
        } else if (!Objects.equals(values.get("timed_out"), false)) {
            throw new IOException("Elasticsearch timeout waiting for yellow status");
        }
    }
}
Also used : RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 59 with Response

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

the class RestElasticSearchClient method search.

@Override
public RestSearchResponse search(String scrollId) throws IOException {
    final String path;
    final byte[] requestData;
    if (ElasticMajorVersion.ONE == majorVersion) {
        path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll" + REQUEST_PARAM_BEGINNING + "scroll=" + scrollKeepAlive;
        requestData = scrollId.getBytes(UTF8_CHARSET);
    } else {
        path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll";
        final Map<String, Object> request = new HashMap<>();
        request.put("scroll", scrollKeepAlive);
        request.put("scroll_id", scrollId);
        requestData = mapper.writeValueAsBytes(request);
        if (log.isDebugEnabled()) {
            log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request));
        }
    }
    final Response response = performRequest(REQUEST_TYPE_POST, path, requestData);
    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) HashMap(java.util.HashMap) InputStream(java.io.InputStream)

Example 60 with Response

use of org.elasticsearch.client.Response in project nifi by apache.

the class ElasticSearchClientServiceImpl method search.

@Override
public SearchResponse search(String query, String index, String type) throws IOException {
    Response response = runQuery(query, index, type);
    Map<String, Object> parsed = parseResponse(response);
    int took = (Integer) parsed.get("took");
    boolean timedOut = (Boolean) parsed.get("timed_out");
    Map<String, Object> aggregations = parsed.get("aggregations") != null ? (Map<String, Object>) parsed.get("aggregations") : new HashMap<>();
    Map<String, Object> hitsParent = (Map<String, Object>) parsed.get("hits");
    int count = (Integer) hitsParent.get("total");
    List<Map<String, Object>> hits = (List<Map<String, Object>>) hitsParent.get("hits");
    SearchResponse esr = new SearchResponse(hits, aggregations, count, took, timedOut);
    if (getLogger().isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("******************");
        sb.append(String.format("Took: %d", took));
        sb.append(String.format("Timed out: %s", timedOut));
        sb.append(String.format("Aggregation count: %d", aggregations.size()));
        sb.append(String.format("Hit count: %d", hits.size()));
        sb.append(String.format("Total found: %d", count));
        sb.append("******************");
        getLogger().debug(sb.toString());
    }
    return esr;
}
Also used : Response(org.elasticsearch.client.Response) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Response (org.elasticsearch.client.Response)75 IOException (java.io.IOException)24 Request (org.elasticsearch.client.Request)19 BasicHeader (org.apache.http.message.BasicHeader)14 NStringEntity (org.apache.http.nio.entity.NStringEntity)14 HttpEntity (org.apache.http.HttpEntity)13 ResponseException (org.elasticsearch.client.ResponseException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 HashMap (java.util.HashMap)9 RestClient (org.elasticsearch.client.RestClient)8 Map (java.util.Map)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7 ArrayList (java.util.ArrayList)6 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)6 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 ParseException (org.json.simple.parser.ParseException)6 InputStream (java.io.InputStream)5 StringEntity (org.apache.http.entity.StringEntity)5