Search in sources :

Example 21 with Request

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

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

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

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

the class SearchRequestFactoryTest method searchIncludesTimerange.

@Test
void searchIncludesTimerange() {
    final SearchSourceBuilder search = this.searchRequestFactory.create(ScrollCommand.builder().indices(Collections.singleton("graylog_0")).range(AbsoluteRange.create(DateTime.parse("2020-07-23T11:03:32.243Z"), DateTime.parse("2020-07-23T11:08:32.243Z"))).build());
    assertJsonPath(search, request -> {
        request.jsonPathAsListOf("$.query.bool.filter..range.timestamp.from", String.class).containsExactly("2020-07-23 11:03:32.243");
        request.jsonPathAsListOf("$.query.bool.filter..range.timestamp.to", String.class).containsExactly("2020-07-23 11:08:32.243");
    });
}
Also used : SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) Test(org.junit.jupiter.api.Test)

Example 25 with Request

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

the class ClientES7 method templateExists.

@Override
public boolean templateExists(String templateName) {
    final GetIndexTemplatesRequest request = new GetIndexTemplatesRequest("*");
    final GetIndexTemplatesResponse result = client.execute((c, requestOptions) -> c.indices().getIndexTemplate(request, requestOptions));
    return result.getIndexTemplates().stream().anyMatch(indexTemplate -> indexTemplate.name().equals(templateName));
}
Also used : GetIndexTemplatesResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexTemplatesRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesRequest)

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