Search in sources :

Example 6 with Request

use of org.elasticsearch.client.Request in project hazelcast by hazelcast.

the class ElasticCatClient method nodes.

/**
 * Returns list of nodes currently in ES cluster
 */
public List<Node> nodes() {
    try {
        Request r = new Request("GET", "/_cat/nodes");
        r.addParameter("format", "json");
        r.addParameter("full_id", "true");
        r.addParameter("h", "id,ip,name,http_address,master");
        Response res = withRetry(() -> client.performRequest(r), retries);
        try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
            JsonArray array = Json.parse(reader).asArray();
            List<Node> nodes = new ArrayList<>(array.size());
            for (JsonValue value : array) {
                Optional<Node> shard = convertToNode(value);
                shard.ifPresent(nodes::add);
            }
            LOG.fine("Nodes: " + nodes);
            return nodes;
        }
    } catch (IOException e) {
        throw new JetException("Could not get ES cluster nodes", e);
    }
}
Also used : Response(org.elasticsearch.client.Response) JsonArray(com.hazelcast.internal.json.JsonArray) InputStreamReader(java.io.InputStreamReader) Request(org.elasticsearch.client.Request) ArrayList(java.util.ArrayList) JsonValue(com.hazelcast.internal.json.JsonValue) IOException(java.io.IOException) JetException(com.hazelcast.jet.JetException)

Example 7 with Request

use of org.elasticsearch.client.Request in project yyl_example by Relucent.

the class ElasticsearchTest method createDocument.

// 创建文档
// PUT方法 :在这个URL中存储文档
private static void createDocument(RestClient client) throws Exception {
    Request request = new Request("PUT", INDEX + "/" + TYPE + "/1");
    request.setEntity(new NStringEntity(// 
    "{" + // 
    "\"name\":\"hello\"," + // 
    "\"value\":\"world\"" + "}", ContentType.APPLICATION_JSON));
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) Request(org.elasticsearch.client.Request)

Example 8 with Request

use of org.elasticsearch.client.Request in project yyl_example by Relucent.

the class ElasticsearchTest method queryByField.

// 获取文档
private static void queryByField(RestClient client) throws Exception {
    Request request = new Request("GET", INDEX + "/" + TYPE + "/_search");
    request.setEntity(new NStringEntity(// 
    "{" + // 
    " \"query\": {" + // 
    " \"match\": {" + // 
    " \"name\": \"welcome\"" + // 
    " }" + // 
    " }" + "}", ContentType.APPLICATION_JSON));
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) Request(org.elasticsearch.client.Request)

Example 9 with Request

use of org.elasticsearch.client.Request in project yyl_example by Relucent.

the class ElasticsearchTest method queryAll.

// 获取文档
private static void queryAll(RestClient client) throws Exception {
    Request request = new Request("GET", INDEX + "/" + TYPE + "/_search");
    request.setEntity(new NStringEntity(// 
    "{" + // 
    " \"query\": {" + // 
    " \"match_all\": {}" + // 
    " }\n" + "}", ContentType.APPLICATION_JSON));
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) Request(org.elasticsearch.client.Request)

Example 10 with Request

use of org.elasticsearch.client.Request in project yyl_example by Relucent.

the class ElasticsearchTest method deleteIndex.

// 删除索引
private static void deleteIndex(RestClient client) throws Exception {
    Request request = new Request("DELETE", INDEX);
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Aggregations

Request (org.elasticsearch.client.Request)54 Response (org.elasticsearch.client.Response)34 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 IOException (java.io.IOException)12 Request (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request)12 HttpEntity (org.apache.http.HttpEntity)11 NStringEntity (org.apache.http.nio.entity.NStringEntity)11 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 InputStream (java.io.InputStream)6 StringEntity (org.apache.http.entity.StringEntity)6 RestClient (org.elasticsearch.client.RestClient)6 ContentType (org.apache.http.entity.ContentType)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 UncheckedIOException (java.io.UncheckedIOException)4 ArrayList (java.util.ArrayList)4 Locale (java.util.Locale)4