Search in sources :

Example 1 with Request

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

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

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

the class ElasticsearchIOTestUtils method setDefaultTemplate.

public static void setDefaultTemplate(RestClient restClient) throws IOException {
    Request request = new Request("PUT", "/_template/default");
    NStringEntity body = new NStringEntity("{" + "\"order\": 0," + "\"index_patterns\": [\"*\"]," + "\"template\": \"*\"," + "\"settings\": {" + "   \"index.number_of_shards\": 1," + "   \"index.number_of_replicas\": 0," + "   \"index.store.stats_refresh_interval\": 0" + "  }" + "}", ContentType.APPLICATION_JSON);
    request.setEntity(body);
    restClient.performRequest(request);
}
Also used : NStringEntity(org.apache.http.nio.entity.NStringEntity) Request(org.elasticsearch.client.Request)

Example 4 with Request

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

the class ElasticsearchIOTestUtils method copyIndex.

/**
 * Synchronously deletes the target if it exists and then (re)creates it as a copy of source
 * synchronously.
 */
static void copyIndex(RestClient restClient, String source, String target) throws IOException {
    deleteIndex(restClient, target);
    HttpEntity entity = new NStringEntity(String.format("{\"source\" : { \"index\" : \"%s\" }, \"dest\" : { \"index\" : \"%s\" } }", source, target), ContentType.APPLICATION_JSON);
    Request request = new Request("POST", "/_reindex");
    flushAndRefreshAllIndices(restClient);
    request.setEntity(entity);
    restClient.performRequest(request);
}
Also used : NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) Request(org.elasticsearch.client.Request)

Example 5 with Request

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

Aggregations

Request (org.elasticsearch.client.Request)54 Response (org.elasticsearch.client.Response)34 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 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 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 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 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 Locale (java.util.Locale)4