Search in sources :

Example 31 with Request

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

the class NodeAdapterES7 method version.

@Override
public Optional<SearchVersion> version() {
    final Request request = new Request("GET", "/");
    final Optional<JsonNode> resp = Optional.of(jsonApi.perform(request, "Unable to retrieve cluster information"));
    final Optional<String> version = resp.map(r -> r.path("version")).map(r -> r.path("number")).map(JsonNode::textValue);
    final SearchVersion.Distribution distribution = resp.map(r -> r.path("version")).map(r -> r.path("distribution")).map(JsonNode::textValue).map(d -> d.toUpperCase(Locale.ROOT)).map(SearchVersion.Distribution::valueOf).orElse(SearchVersion.Distribution.ELASTICSEARCH);
    return version.map(this::parseVersion).map(v -> SearchVersion.create(distribution, v));
}
Also used : Inject(javax.inject.Inject) Locale(java.util.Locale) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) SearchVersion(org.graylog2.storage.SearchVersion) NodeAdapter(org.graylog2.indexer.cluster.NodeAdapter) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode) SearchVersion(org.graylog2.storage.SearchVersion)

Example 32 with Request

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

the class FieldMappingApi method fieldTypes.

public Map<String, FieldMapping> fieldTypes(String index) {
    final JsonNode result = client.execute((c, requestOptions) -> {
        final Response response = c.getLowLevelClient().performRequest(request(index));
        return objectMapper.readTree(response.getEntity().getContent());
    }, "Unable to retrieve field types of index " + index);
    final JsonNode fields = result.path(index).path("mappings").path("properties");
    // noinspection UnstableApiUsage
    return Streams.stream(fields.fields()).collect(Collectors.toMap(Map.Entry::getKey, entry -> FieldMapping.create(entry.getValue().path("type").asText(), entry.getValue().path("fielddata").asBoolean())));
}
Also used : Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) Inject(javax.inject.Inject) ElasticsearchClient(org.graylog.storage.elasticsearch7.ElasticsearchClient) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AutoValue(com.google.auto.value.AutoValue) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) Streams(com.google.common.collect.Streams) Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) Collectors(java.util.stream.Collectors) Nullable(javax.annotation.Nullable) JsonNode(com.fasterxml.jackson.databind.JsonNode) Map(java.util.Map)

Example 33 with Request

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

the class StatsApi method stats.

private JsonNode stats(Collection<String> indices, Collection<String> metrics, Consumer<Request> prepareRequest) {
    final StringBuilder endpoint = new StringBuilder();
    if (!indices.isEmpty()) {
        final String joinedIndices = String.join(",", indices);
        endpoint.append("/");
        endpoint.append(joinedIndices);
    }
    endpoint.append("/_stats");
    if (!metrics.isEmpty()) {
        final String joinedMetrics = String.join(",", metrics);
        endpoint.append("/");
        endpoint.append(joinedMetrics);
    }
    final Request request = new Request("GET", endpoint.toString());
    prepareRequest.accept(request);
    return client.execute((c, requestOptions) -> {
        request.setOptions(requestOptions);
        final Response response = c.getLowLevelClient().performRequest(request);
        return objectMapper.readTree(response.getEntity().getContent());
    }, "Unable to retrieve index stats for " + String.join(",", indices));
}
Also used : Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request)

Example 34 with Request

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

the class CatApi method nodes.

public List<NodeResponse> nodes() {
    final Request request = request("GET", "nodes");
    request.addParameter("h", "id,name,role,host,ip,fileDescriptorMax,diskUsed,diskTotal,diskUsedPercent");
    request.addParameter("full_id", "true");
    return perform(request, new TypeReference<List<NodeResponse>>() {
    }, "Unable to retrieve nodes list");
}
Also used : Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) List(java.util.List)

Example 35 with Request

use of org.elasticsearch.client.Request 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)

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