Search in sources :

Example 26 with Response

use of org.elasticsearch.client.Response in project graylog2-server by Graylog2.

the class ClusterStateApi method fields.

public Map<String, Set<String>> fields(Collection<String> indices) {
    final Request request = request(indices);
    final JsonNode jsonResponse = client.execute((c, requestOptions) -> {
        request.setOptions(requestOptions);
        final Response response = c.getLowLevelClient().performRequest(request);
        return objectMapper.readTree(response.getEntity().getContent());
    }, "Unable to retrieve fields from indices: " + String.join(",", indices));
    // noinspection UnstableApiUsage
    return Streams.stream(jsonResponse.path("metadata").path("indices").fields()).flatMap(index -> allFieldsFromIndex(index.getKey(), index.getValue())).collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, Collectors.toSet())));
}
Also used : Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) ElasticsearchClient(org.graylog.storage.elasticsearch7.ElasticsearchClient) Collection(java.util.Collection) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Set(java.util.Set) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) Inject(javax.inject.Inject) AbstractMap(java.util.AbstractMap) Stream(java.util.stream.Stream) Map(java.util.Map) Collectors.mapping(java.util.stream.Collectors.mapping) JsonNode(com.fasterxml.jackson.databind.JsonNode) Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 27 with Response

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

the class ElasticsearchIOTestUtils method refreshIndexAndGetCurrentNumDocs.

/**
 * Forces a refresh of the given index to make recently inserted documents available for search.
 *
 * @param restClient To use for issuing queries
 * @param index The Elasticsearch index
 * @param type The Elasticsearch type
 * @param urlParams Optional key/value pairs describing URL params for ES APIs
 * @return The number of docs in the index
 * @throws IOException On error communicating with Elasticsearch
 */
static long refreshIndexAndGetCurrentNumDocs(RestClient restClient, String index, String type, int backendVersion, @Nullable Map<String, String> urlParams) throws IOException {
    long result = 0;
    try {
        flushAndRefreshAllIndices(restClient);
        String endPoint = generateSearchPath(index, type);
        Request request = new Request("GET", endPoint);
        if (urlParams != null) {
            request.addParameters(urlParams);
        }
        Response response = restClient.performRequest(request);
        JsonNode searchResult = ElasticsearchIO.parseResponse(response.getEntity());
        if (backendVersion >= 7) {
            result = searchResult.path("hits").path("total").path("value").asLong();
        } else {
            result = searchResult.path("hits").path("total").asLong();
        }
    } catch (IOException e) {
        // In that cases index/type has not been created (created upon first doc insertion)
        if (!e.getMessage().contains("index_not_found_exception")) {
            throw e;
        }
    }
    return result;
}
Also used : Response(org.elasticsearch.client.Response) ElasticsearchIO.parseResponse(org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.parseResponse) Request(org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 28 with Response

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

the class ElasticsearchIOTestUtils method countByMatch.

/**
 * Executes a match query for given field/value and returns the count of results.
 *
 * @param connectionConfiguration Specifies the index and type
 * @param restClient To use to execute the call
 * @param field The field to query
 * @param value The value to match
 * @param urlParams Optional key/value pairs describing URL params for ES APIs
 * @param versionNumberCountPair Optional pair of [version_number, expected_num_doc_with_version]
 * @return The count of documents in the search result
 * @throws IOException On error communicating with Elasticsearch
 */
static int countByMatch(ConnectionConfiguration connectionConfiguration, RestClient restClient, String field, String value, @Nullable Map<String, String> urlParams, @Nullable KV<Integer, Long> versionNumberCountPair) throws IOException {
    String size = versionNumberCountPair == null ? "10" : versionNumberCountPair.getValue().toString();
    String requestBody = "{\n" + "\"size\": " + size + ",\n" + "\"version\" : true,\n" + "  \"query\" : {\"match\": {\n" + "    \"" + field + "\": \"" + value + "\"\n" + "  }}\n" + "}\n";
    String endPoint = generateSearchPath(connectionConfiguration);
    HttpEntity httpEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
    Request request = new Request("GET", endPoint);
    request.setEntity(httpEntity);
    if (urlParams != null) {
        request.addParameters(urlParams);
    }
    Response response = restClient.performRequest(request);
    JsonNode searchResult = parseResponse(response.getEntity());
    if (versionNumberCountPair != null) {
        int numHits = 0;
        for (JsonNode hit : searchResult.path("hits").path("hits")) {
            if (hit.path("_version").asInt() == versionNumberCountPair.getKey()) {
                numHits++;
            }
        }
        return numHits;
    }
    if (getBackendVersion(connectionConfiguration) >= 7) {
        return searchResult.path("hits").path("total").path("value").asInt();
    } else {
        return searchResult.path("hits").path("total").asInt();
    }
}
Also used : Response(org.elasticsearch.client.Response) ElasticsearchIO.parseResponse(org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.parseResponse) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) Request(org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 29 with Response

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

the class ElasticsearchIOTestUtils method insertTestDocuments.

/**
 * Inserts the given number of test documents into Elasticsearch.
 */
static void insertTestDocuments(ConnectionConfiguration connectionConfiguration, List<String> data, RestClient restClient) throws IOException {
    StringBuilder bulkRequest = new StringBuilder();
    int i = 0;
    for (String document : data) {
        bulkRequest.append(String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\", \"_id\" : \"%s\" } }%n%s%n", connectionConfiguration.getIndex(), connectionConfiguration.getType(), i++, document));
    }
    String endPoint = String.format("/%s/%s/_bulk", connectionConfiguration.getIndex(), connectionConfiguration.getType());
    HttpEntity requestBody = new NStringEntity(bulkRequest.toString(), ContentType.APPLICATION_JSON);
    Request request = new Request("POST", endPoint);
    request.setEntity(requestBody);
    Response response = restClient.performRequest(request);
    ElasticsearchIO.createWriteReport(response.getEntity(), Collections.emptySet(), true);
    flushAndRefreshAllIndices(restClient);
}
Also used : Response(org.elasticsearch.client.Response) ElasticsearchIO.parseResponse(org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.parseResponse) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) Request(org.elasticsearch.client.Request)

Example 30 with Response

use of org.elasticsearch.client.Response in project hazelcast by hazelcast.

the class ElasticCatClient method nodes.

/**
 * Returns list of nodes currently in ES cluster
 */
public List<Node> nodes() {
    try {
        Request r = new Request("GET", "/_cat/nodes");
        r.addParameter("format", "json");
        r.addParameter("full_id", "true");
        r.addParameter("h", "id,ip,name,http_address,master");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            List<Node> nodes = new ArrayList<>(array.size());
            for (JsonValue value : array) {
                Optional<Node> shard = convertToNode(value);
                shard.ifPresent(nodes::add);
            }
            LOG.fine("Nodes: " + nodes);
            return nodes;
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES cluster nodes", e);
    }
}
Also used : Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException)

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