Search in sources :

Example 26 with Request

use of org.elasticsearch.client.Request in project immutables by immutables.

the class IndexOps method mapping.

/**
 * Return mapping for current index
 */
Single<Mapping> mapping() {
    final String uri = String.format(Locale.ROOT, "/%s/_mapping", index);
    final Request request = new Request("GET", uri);
    return transport.execute(request).map(response -> mapper.readValue(response.getEntity().getContent(), ObjectNode.class)).map(root -> {
        ObjectNode properties = (ObjectNode) root.elements().next().get("mappings");
        if (properties == null) {
            throw new IllegalStateException(String.format("No mappings found for index %s (after request %s)", index, request));
        }
        ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
        Json.visitMappingProperties(properties, builder::put);
        return Mapping.ofElastic(builder.build());
    });
}
Also used : RestClient(org.elasticsearch.client.RestClient) ImmutableMap(com.google.common.collect.ImmutableMap) Completable(io.reactivex.Completable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Single(io.reactivex.Single) Request(org.elasticsearch.client.Request) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) Locale(java.util.Locale) Map(java.util.Map) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Request(org.elasticsearch.client.Request) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 27 with Request

use of org.elasticsearch.client.Request in project immutables by immutables.

the class IndexOps method exists.

Single<Boolean> exists() {
    final String uri = String.format(Locale.ROOT, "/%s/_mapping", index);
    final Request request = new Request("GET", uri);
    return transport.execute(request).map(x -> true).onErrorResumeNext(e -> Single.just(false));
}
Also used : RestClient(org.elasticsearch.client.RestClient) ImmutableMap(com.google.common.collect.ImmutableMap) Completable(io.reactivex.Completable) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) StringEntity(org.apache.http.entity.StringEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Single(io.reactivex.Single) Request(org.elasticsearch.client.Request) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects) Locale(java.util.Locale) Map(java.util.Map) Request(org.elasticsearch.client.Request)

Example 28 with Request

use of org.elasticsearch.client.Request in project immutables by immutables.

the class IndexOps method create.

/**
 * Creates index in elastic search given a mapping. Mapping can contain nested fields expressed
 * as dots({@code .}).
 *
 * <p>Example
 * <pre>
 *  {@code
 *     b.a: long
 *     b.b: keyword
 *  }
 * </pre>
 *
 * @param mapping field and field type mapping
 * @throws IOException if there is an error
 */
Completable create(Map<String, String> mapping) {
    Objects.requireNonNull(mapping, "mapping");
    ObjectNode mappings = mapper.createObjectNode();
    ObjectNode properties = mappings.with("mappings").with("properties");
    for (Map.Entry<String, String> entry : mapping.entrySet()) {
        applyMapping(properties, entry.getKey(), entry.getValue());
    }
    // create index and mapping
    try {
        final HttpEntity entity = new StringEntity(mapper.writeValueAsString(mappings), ContentType.APPLICATION_JSON);
        final Request r = new Request("PUT", "/" + index);
        r.setEntity(entity);
        return transport.execute(r).ignoreElement();
    } catch (JsonProcessingException e) {
        throw new UncheckedIOException(e);
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) HttpEntity(org.apache.http.HttpEntity) Request(org.elasticsearch.client.Request) UncheckedIOException(java.io.UncheckedIOException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 29 with Request

use of org.elasticsearch.client.Request in project immutables by immutables.

the class ElasticsearchOps method insertBulkInternal.

private Single<WriteResult> insertBulkInternal(List<ObjectNode> documents) throws JsonProcessingException {
    Objects.requireNonNull(documents, "documents");
    if (documents.isEmpty()) {
        // nothing to process
        return Single.just(WriteResult.empty());
    }
    final List<String> bulk = new ArrayList<>(documents.size() * 2);
    for (ObjectNode doc : documents) {
        final ObjectNode header = mapper.createObjectNode();
        header.with("index").put("_index", index);
        if (doc.has("_id")) {
            // check if document has already an _id
            header.with("index").set("_id", doc.get("_id"));
            doc.remove("_id");
        }
        bulk.add(header.toString());
        bulk.add(mapper().writeValueAsString(doc));
    }
    final StringEntity entity = new StringEntity(String.join("\n", bulk) + "\n", ContentType.APPLICATION_JSON);
    final Request r = new Request("POST", "/_bulk?refresh");
    r.setEntity(entity);
    return transport.execute(r).map(x -> WriteResult.unknown());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Request(org.elasticsearch.client.Request)

Example 30 with Request

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

the class ClusterAdapterES7 method clusterStats.

@Override
public ClusterStats clusterStats() {
    final Request request = new Request("GET", "/_cluster/stats/nodes/*");
    final JsonNode clusterStatsResponseJson = jsonApi.perform(request, "Couldn't read Elasticsearch cluster stats");
    final String clusterName = clusterStatsResponseJson.path("cluster_name").asText();
    String clusterVersion = null;
    if (clusterStatsResponseJson.path("nodes").path("versions").isArray()) {
        final ArrayNode versions = (ArrayNode) clusterStatsResponseJson.path("nodes").path("versions");
        // We just use the first version in the "versions" array. This is not correct if there are different
        // versions running in the cluster, but that is not recommended anyway.
        final JsonNode versionNode = versions.path(0);
        if (versionNode.getNodeType() != JsonNodeType.MISSING) {
            clusterVersion = versionNode.asText();
        }
    }
    final JsonNode countStats = clusterStatsResponseJson.path("nodes").path("count");
    final NodesStats nodesStats = NodesStats.create(countStats.path("total").asInt(-1), countStats.path("master_only").asInt(-1), countStats.path("data_only").asInt(-1), countStats.path("master_data").asInt(-1), countStats.path("client").asInt(-1));
    final JsonNode clusterIndicesStats = clusterStatsResponseJson.path("indices");
    final IndicesStats indicesStats = IndicesStats.create(clusterIndicesStats.path("count").asInt(-1), clusterIndicesStats.path("store").path("size_in_bytes").asLong(-1L), clusterIndicesStats.path("fielddata").path("memory_size_in_bytes").asLong(-1L));
    return ClusterStats.create(clusterName, clusterVersion, nodesStats, indicesStats);
}
Also used : NodesStats(org.graylog2.system.stats.elasticsearch.NodesStats) IndicesStats(org.graylog2.system.stats.elasticsearch.IndicesStats) ClusterGetSettingsRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.settings.ClusterGetSettingsRequest) GetIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) ClusterHealthRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

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