Search in sources :

Example 61 with Response

use of 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 62 with Response

use of org.elasticsearch.client.Response in project timbuctoo by HuygensING.

the class ElasticSearchFilter method query.

@Override
public EsFilterResult query(String dataSetId, String fieldName, String elasticSearchQuery, String token, int preferredPageSize) throws IOException {
    String endpoint = dataSetId + (fieldName != null && !fieldName.isEmpty() ? "/" + fieldName : "") + "/_search";
    JsonNode queryNode = elaborateQuery(elasticSearchQuery, token, preferredPageSize);
    Map<String, String> params = Collections.singletonMap("pretty", "true");
    HttpEntity entity = new NStringEntity(queryNode.toString(), ContentType.APPLICATION_JSON);
    Response response = restClient.performRequest(METHOD_GET, endpoint, params, entity);
    JsonNode responseNode = mapper.readTree(response.getEntity().getContent());
    return new EsFilterResult(queryNode, responseNode);
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HttpEntity(org.apache.http.HttpEntity) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 63 with Response

use of org.elasticsearch.client.Response in project presto by prestodb.

the class ElasticsearchClient method doRequest.

private <T> T doRequest(String path, ResponseHandler<T> handler) {
    checkArgument(path.startsWith("/"), "path must be an absolute path");
    Response response;
    try {
        response = client.getLowLevelClient().performRequest("GET", path);
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, e);
    }
    String body;
    try {
        body = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_INVALID_RESPONSE, e);
    }
    return handler.process(body);
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(org.elasticsearch.client.Response) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException)

Example 64 with Response

use of org.elasticsearch.client.Response in project presto by prestodb.

the class ElasticsearchClient method count.

public long count(String index, int shard, QueryBuilder query) {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource().query(query);
    LOG.debug("Count: %s:%s, query: %s", index, shard, sourceBuilder);
    Response response;
    try {
        response = client.getLowLevelClient().performRequest("GET", format("/%s/_count?preference=_shards:%s", index, shard), ImmutableMap.of(), new StringEntity(sourceBuilder.toString()), new BasicHeader("Content-Type", "application/json"));
    } catch (ResponseException e) {
        throw propagate(e);
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, e);
    }
    try {
        return COUNT_RESPONSE_CODEC.fromJson(EntityUtils.toByteArray(response.getEntity())).getCount();
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_INVALID_RESPONSE, e);
    }
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(org.elasticsearch.client.Response) StringEntity(org.apache.http.entity.StringEntity) ResponseException(org.elasticsearch.client.ResponseException) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) BasicHeader(org.apache.http.message.BasicHeader) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 65 with Response

use of org.elasticsearch.client.Response in project presto by prestodb.

the class ElasticsearchClient method executeQuery.

public String executeQuery(String index, String query) {
    String path = format("/%s/_search", index);
    Response response;
    try {
        response = client.getLowLevelClient().performRequest("GET", path, ImmutableMap.of(), new ByteArrayEntity(query.getBytes(UTF_8)), new BasicHeader("Content-Type", "application/json"), new BasicHeader("Accept-Encoding", "application/json"));
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_CONNECTION_ERROR, e);
    }
    String body;
    try {
        body = EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        throw new PrestoException(ELASTICSEARCH_INVALID_RESPONSE, e);
    }
    return body;
}
Also used : SearchResponse(org.elasticsearch.action.search.SearchResponse) Response(org.elasticsearch.client.Response) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) PrestoException(com.facebook.presto.spi.PrestoException) IOException(java.io.IOException) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

Response (org.elasticsearch.client.Response)75 IOException (java.io.IOException)24 Request (org.elasticsearch.client.Request)19 BasicHeader (org.apache.http.message.BasicHeader)14 NStringEntity (org.apache.http.nio.entity.NStringEntity)14 HttpEntity (org.apache.http.HttpEntity)13 ResponseException (org.elasticsearch.client.ResponseException)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)9 HashMap (java.util.HashMap)9 RestClient (org.elasticsearch.client.RestClient)8 Map (java.util.Map)7 Response (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response)7 ArrayList (java.util.ArrayList)6 RestBulkItemResponse (org.janusgraph.diskstorage.es.rest.RestBulkResponse.RestBulkItemResponse)6 JSONArray (org.json.simple.JSONArray)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 ParseException (org.json.simple.parser.ParseException)6 InputStream (java.io.InputStream)5 StringEntity (org.apache.http.entity.StringEntity)5