Java Code Examples for org.elasticsearch.client.Response#getEntity()

The following examples show how to use org.elasticsearch.client.Response#getEntity() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ForceMergeElasticService.java    From SkaETL with Apache License 2.0 6 votes vote down vote up
@Scheduled(cron = "0 0/30 * * * ?")
public void forceMergeAllIndexMetricsAndReferential(){
    log.info("start forceMerge All Index Metrics And Referential");
    try {
        Response response = client.getLowLevelClient().performRequest("GET", "_cat/indices?format=json");
        if(response.getStatusLine().getStatusCode() == 200){
            HttpEntity entity = response.getEntity();
            if(entity!=null) {
                String responseBody = EntityUtils.toString(response.getEntity());
                JsonNode jsonNode = JSONUtils.getInstance().parse(responseBody);
                if(jsonNode!=null) {
                    jsonNode.findValues("index").stream()
                            .filter(itemJsonNode -> itemJsonNode.asText().contains("-metrics-") || itemJsonNode.asText().contains("-referential-"))
                            .forEach(itemJsonNode -> forceMerge(itemJsonNode.asText()));
                }
            }
        }else{
            log.error("search all indices not response 200 {}",response.getStatusLine().getStatusCode());
        }
    } catch (IOException e) {
       log.error("Error during search all indices {}",e);
    }
}
 
Example 2
Source File: MarcElasticsearchClient.java    From metadata-qa-marc with GNU General Public License v3.0 5 votes vote down vote up
public HttpEntity rootRequest() throws IOException {
  Response response = restClient.performRequest(
        "GET", "/",
        Collections.singletonMap("pretty", "true"));
  // System.out.println(EntityUtils.toString(response.getEntity()));
  return response.getEntity();
}
 
Example 3
Source File: TestUtils.java    From CogStack-Pipeline with Apache License 2.0 5 votes vote down vote up
public String getStringInEsDoc(String id){
    try {
        Response response = esRestService.getRestClient().performRequest(
                "GET",
                "/"+env.getProperty("elasticsearch.index.name")+"/"
                        +env.getProperty("elasticsearch.type")+"/"+id,
                Collections.<String, String>emptyMap()
        );
        HttpEntity ent = response.getEntity();
        JsonObject json = new JsonParser().parse(EntityUtils.toString(ent)).getAsJsonObject();
        return  json.toString();
    } catch (IOException e) {
        throw new RuntimeException("GET request failed:", e);
    }
}
 
Example 4
Source File: ElasticsearchIndexer.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public String executeRaw(String method, String url, String rawJson) throws IOException {
    Request request = new Request(method, url);
    if (rawJson != null && !rawJson.isEmpty()) {
        request.setJsonEntity(rawJson);
    }
    Response response = client.getLowLevelClient().performRequest(request);
    if ("OPTIONS".equals(method)) {
        return response.getHeader("Allow");
    }
    HttpEntity entity = response.getEntity();
    return entity != null ? EntityUtils.toString(entity):null;
}
 
Example 5
Source File: IndexService.java    From elastic-rest-spring-wrapper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Refresh the provided indexes {@code names} in the cluster with the provided name {@code clusterName}
 *
 * @param clusterName The name of the cluster to refresh the indexes from
 * @param names The names of the indexes to be refreshed
 */
public void refreshIndexes(String clusterName, String... names) {
    try {
        String endpoint = "/_refresh";
        if (names.length > 0) {
            endpoint = "/" + String.join(",", names) + endpoint;
        }

        Response response = clusterManagementService.getRestClientForCluster(clusterName).performRequest(
                "POST",
                endpoint
        );

        if (response.getStatusLine().getStatusCode() > 399 && logger.isWarnEnabled()) {
            logger.warn("Problem while refreshing indexes: {}", String.join(",", names));
        }

        if (logger.isDebugEnabled()) {
            HttpEntity entity = response.getEntity();

            RefreshResponse refreshResponse = jacksonObjectMapper.readValue(entity.getContent(), RefreshResponse.class);
            Shards shards = refreshResponse.getShards();
            logger.debug("Shards refreshed: total {}, successfull {}, failed {}", shards.getTotal(), shards.getSuccessful(), shards.getFailed());
        }
    } catch (IOException e) {
        logger.warn("Problem while executing refresh request.", e);
        throw new ClusterApiException("Error when refreshing indexes." + e.getMessage());
    }

}
 
Example 6
Source File: ClusterService.java    From elastic-rest-spring-wrapper with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Returns the current health of the cluster as a {@link ClusterHealth} object.
 *
 * @param clusterName Name of the cluster to check the health for
 * @return ClusterHealth containing the basic properties of the health of the cluster
 */
public ClusterHealth checkClusterHealth(String clusterName) {
    try {
        Response response = clusterManagementService.getRestClientForCluster(clusterName).performRequest(GET, "/_cluster/health");

        HttpEntity entity = response.getEntity();

        return jacksonObjectMapper.readValue(entity.getContent(), ClusterHealth.class);

    } catch (IOException e) {
        logger.warn("Problem while executing request.", e);
        throw new ClusterApiException("Error when checking the health of the cluster");
    }
}
 
Example 7
Source File: JsonUtil.java    From elasticsearch-beyonder with Apache License 2.0 5 votes vote down vote up
public static Map<String, Object> asMap(Response response) {
    try {
        if (response.getEntity() == null) {
            return null;
        }
        return asMap(response.getEntity().getContent());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 8
Source File: ElasticsearchResponse.java    From components with Apache License 2.0 4 votes vote down vote up
public ElasticsearchResponse(Response response) throws IOException {
    this.statusLine = response.getStatusLine();
    if (response.getEntity() != null) {
        this.entity = parseResponse(response);
    }
}