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);
}
}
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);
}
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);
}
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);
}
}
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;
}
Aggregations