Search in sources :

Example 1 with ElasticsearchIO.parseResponse

use of org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.parseResponse 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)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 ElasticsearchIO.parseResponse (org.apache.beam.sdk.io.elasticsearch.ElasticsearchIO.parseResponse)1 Request (org.elasticsearch.client.Request)1 Response (org.elasticsearch.client.Response)1