Search in sources :

Example 26 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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 27 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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 28 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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 29 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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 30 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response 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

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