Search in sources :

Example 21 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project storm by apache.

the class EsLookupBolt method lookupValuesInEs.

private Collection<Values> lookupValuesInEs(Tuple tuple) throws IOException {
    String index = tupleMapper.getIndex(tuple);
    String type = tupleMapper.getType(tuple);
    String id = tupleMapper.getId(tuple);
    Map<String, String> params = tupleMapper.getParams(tuple, new HashMap<>());
    Response response = client.performRequest("get", getEndpoint(index, type, id), params);
    return output.toValues(response);
}
Also used : Response(org.elasticsearch.client.Response)

Example 22 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project storm by apache.

the class EsTestUtil method generateMockResponseException.

/**
 * Generate a mock {@link ResponseException}.
 * @return a mock {@link ResponseException}.
 * @throws IOException
 */
public static ResponseException generateMockResponseException() throws IOException {
    Response response = mock(Response.class);
    RequestLine requestLine = mock(RequestLine.class);
    StatusLine statusLine = mock(StatusLine.class);
    when(response.getRequestLine()).thenReturn(requestLine);
    when(response.getStatusLine()).thenReturn(statusLine);
    return new ResponseException(response);
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) RequestLine(org.apache.http.RequestLine) ResponseException(org.elasticsearch.client.ResponseException)

Example 23 with Response

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

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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 25 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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

Response (org.elasticsearch.client.Response)111 Request (org.elasticsearch.client.Request)38 IOException (java.io.IOException)33 HttpEntity (org.apache.http.HttpEntity)24 NStringEntity (org.apache.http.nio.entity.NStringEntity)21 Test (org.junit.Test)20 HashMap (java.util.HashMap)17 Map (java.util.Map)14 BasicHeader (org.apache.http.message.BasicHeader)14 ResponseException (org.elasticsearch.client.ResponseException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 RestClient (org.elasticsearch.client.RestClient)11 ArrayList (java.util.ArrayList)10 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)10 StringEntity (org.apache.http.entity.StringEntity)9 SearchResponse (org.elasticsearch.action.search.SearchResponse)9 InputStream (java.io.InputStream)8 JSONObject (org.json.simple.JSONObject)8 MultiSearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7