Search in sources :

Example 81 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project janusgraph by JanusGraph.

the class RestElasticSearchClient method search.

@Override
public RestSearchResponse search(String scrollId) throws IOException {
    final String path;
    final byte[] requestData;
    if (ElasticMajorVersion.ONE == majorVersion) {
        path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll" + REQUEST_PARAM_BEGINNING + "scroll=" + scrollKeepAlive;
        requestData = scrollId.getBytes(UTF8_CHARSET);
    } else {
        path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll";
        final Map<String, Object> request = new HashMap<>();
        request.put("scroll", scrollKeepAlive);
        request.put("scroll_id", scrollId);
        requestData = mapper.writeValueAsBytes(request);
        if (log.isDebugEnabled()) {
            log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request));
        }
    }
    final Response response = performRequest(REQUEST_TYPE_POST, path, requestData);
    try (final InputStream inputStream = response.getEntity().getContent()) {
        return mapper.readValue(inputStream, RestSearchResponse.class);
    }
}
Also used : RestBulkItemResponse(org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse) Response(org.elasticsearch.client.Response) HashMap(java.util.HashMap) InputStream(java.io.InputStream)

Example 82 with Response

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

the class ElasticSearchClientServiceImpl method search.

@Override
public SearchResponse search(String query, String index, String type) throws IOException {
    Response response = runQuery(query, index, type);
    Map<String, Object> parsed = parseResponse(response);
    int took = (Integer) parsed.get("took");
    boolean timedOut = (Boolean) parsed.get("timed_out");
    Map<String, Object> aggregations = parsed.get("aggregations") != null ? (Map<String, Object>) parsed.get("aggregations") : new HashMap<>();
    Map<String, Object> hitsParent = (Map<String, Object>) parsed.get("hits");
    int count = (Integer) hitsParent.get("total");
    List<Map<String, Object>> hits = (List<Map<String, Object>>) hitsParent.get("hits");
    SearchResponse esr = new SearchResponse(hits, aggregations, count, took, timedOut);
    if (getLogger().isDebugEnabled()) {
        StringBuilder sb = new StringBuilder();
        sb.append("******************");
        sb.append(String.format("Took: %d", took));
        sb.append(String.format("Timed out: %s", timedOut));
        sb.append(String.format("Aggregation count: %d", aggregations.size()));
        sb.append(String.format("Hit count: %d", hits.size()));
        sb.append(String.format("Total found: %d", count));
        sb.append("******************");
        getLogger().debug(sb.toString());
    }
    return esr;
}
Also used : Response(org.elasticsearch.client.Response) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 83 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project components by Talend.

the class ElasticsearchBeamRuntimeTestIT method init.

@Before
public void init() throws IOException, ExecutionException, InterruptedException {
    client = ElasticsearchTestUtils.createClient(ElasticsearchTestConstants.HOSTS.split(":")[0], ElasticsearchTestConstants.TRANSPORT_PORT, ElasticsearchTestConstants.CLUSTER_NAME);
    datastoreProperties = new ElasticsearchDatastoreProperties("datastore");
    datastoreProperties.init();
    datastoreProperties.nodes.setValue(ElasticsearchTestConstants.HOSTS);
    RestClient restClient = ElasticsearchConnection.createClient(datastoreProperties);
    BasicHeader emptyHeader = new BasicHeader("", "");
    Map<String, String> emptyParams = new HashMap<>();
    ElasticsearchTestUtils.deleteIndex(INDEX_NAME, client);
    Response checkExistsResponse = restClient.performRequest("HEAD", "/" + INDEX_NAME, emptyParams);
    ElasticsearchResponse checkExists = new ElasticsearchResponse(checkExistsResponse);
    if (!checkExists.isOk()) {
        // create index for test, name is 'beam'
        restClient.performRequest("PUT", "/" + INDEX_NAME, emptyHeader);
    }
}
Also used : Response(org.elasticsearch.client.Response) HashMap(java.util.HashMap) RestClient(org.elasticsearch.client.RestClient) BasicHeader(org.apache.http.message.BasicHeader) ElasticsearchDatastoreProperties(org.talend.components.elasticsearch.ElasticsearchDatastoreProperties) Before(org.junit.Before)

Example 84 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project kylo by Teradata.

the class ElasticSearchRestService method deleteAll.

@Override
public int deleteAll(@Nonnull String indexName, @Nonnull String typeName) {
    try (RestClient restClient = buildRestClient()) {
        if ((restClientConfig.getEsversion() != null) && (restClientConfig.getEsversion().equals(VERSION_TWO))) {
            log.info("Elasticsearch v2");
            Response response = restClient.performRequest(DELETE_METHOD, getAllDocumentsDeleteEndPointEsV2(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDslV2());
            if (response != null) {
                try {
                    String entityString = EntityUtils.toString(response.getEntity());
                    JSONObject entityStringJsonObject = new JSONObject(entityString);
                    JSONObject indicesJsonObject = entityStringJsonObject.getJSONObject("_indices");
                    JSONObject allJsonObject = indicesJsonObject.getJSONObject("_all");
                    return (Integer) allJsonObject.get("deleted");
                } catch (JSONException e) {
                    log.warn("Unable to get number of documents deleted. [{}]", e);
                }
            } else {
                log.warn("Unable to identify number of documents deleted since null response received");
            }
        } else {
            log.info("Elasticsearch v5 or above");
            Response response = restClient.performRequest(POST_METHOD, getAllDocumentsDeleteEndPoint(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDsl());
            if (response != null) {
                try {
                    String entityString = EntityUtils.toString(response.getEntity());
                    JSONObject entityStringJsonObject = new JSONObject(entityString);
                    return (Integer) entityStringJsonObject.get("deleted");
                } catch (JSONException e) {
                    log.warn("Unable to get number of documents deleted. [{}]", e);
                }
            } else {
                log.warn("Unable to identify number of documents deleted since null response received");
            }
        }
        log.info("Deleted all-documents for index={}, type={}", indexName, typeName);
    } catch (ResponseException responseException) {
        log.error("Index all-documents deletion encountered issues in Elasticsearch for index={}, type={} [{}]", indexName, typeName, responseException);
    } catch (ClientProtocolException clientProtocolException) {
        log.error("Http protocol error for delete all-documents for index={}, type={} [{}]", indexName, typeName, clientProtocolException);
    } catch (IOException ioException) {
        log.error("IO Error in rest client [{}]", ioException);
    }
    return 0;
}
Also used : ElasticSearchRestSearchResponse(com.thinkbiganalytics.search.rest.model.es.ElasticSearchRestSearchResponse) Response(org.elasticsearch.client.Response) JSONObject(org.codehaus.jettison.json.JSONObject) ResponseException(org.elasticsearch.client.ResponseException) RestClient(org.elasticsearch.client.RestClient) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 85 with Response

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

the class ScrollingTest method assertNoActiveScrolls.

/**
 * Ensures there are no pending scroll contexts in elastic search cluster.
 * Queries {@code /_nodes/stats/indices/search} endpoint.
 * @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html">Indices Stats</a>
 */
// for errorprone
@SuppressWarnings("unused")
private void assertNoActiveScrolls() throws Exception {
    // get node stats
    final Response response = restClient.performRequest(new Request("GET", "/_nodes/stats/indices/search"));
    try (InputStream is = response.getEntity().getContent()) {
        final ObjectNode node = backend().objectMapper.readValue(is, ObjectNode.class);
        final String path = "/indices/search/scroll_current";
        final JsonNode scrollCurrent = node.with("nodes").elements().next().at(path);
        if (scrollCurrent.isMissingNode()) {
            throw new IllegalStateException("Couldn't find node at " + path);
        }
        final int activeScrolls = scrollCurrent.asInt();
        if (activeScrolls != 0) {
            throw new AssertionError(String.format("Expected no active scrolls but got %d. " + "Current index stats %s", activeScrolls, node));
        }
    }
}
Also used : Response(org.elasticsearch.client.Response) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) Request(org.elasticsearch.client.Request) JsonNode(com.fasterxml.jackson.databind.JsonNode)

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