Search in sources :

Example 31 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project yyl_example by Relucent.

the class ElasticsearchTest method existsIndices.

// 查询索引是否存在
private static boolean existsIndices(RestClient client) throws Exception {
    Request request = new Request("HEAD", INDEX);
    Response response = client.performRequest(request);
    boolean exists = response.getStatusLine().getReasonPhrase().equals("OK");
    System.out.println(exists);
    return exists;
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Example 32 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project yyl_example by Relucent.

the class ElasticsearchTest method info.

// 验证ES集群是否搭建成功
private static void info(RestClient client) throws Exception {
    // curl -X GET "http://localhost:9200/_count?pretty"
    Request request = new Request("GET", "");
    request.addParameter("pretty", "true");
    Response response = client.performRequest(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
Also used : Response(org.elasticsearch.client.Response) Request(org.elasticsearch.client.Request)

Example 33 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project yyl_example by Relucent.

the class ElasticsearchTest method createIndex.

// 创建索引
private static void createIndex(RestClient client) throws Exception {
    Request request = new Request("PUT", 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)

Example 34 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project yyl_example by Relucent.

the class ElasticsearchTest method createDocumentPost.

// 创建文档
// POST方法:在这个类型下存储文档
private static void createDocumentPost(RestClient client) throws Exception {
    Request request = new Request("POST", INDEX + "/" + TYPE);
    request.setEntity(new NStringEntity(// 
    "{" + // 
    "\"name\":\"welcome\"," + // 
    "\"value\":\"universe\"" + "}", 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 35 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.

the class ElasticsearchRestClient method scan.

@Override
public Status scan(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
    try {
        final Response response;
        try (XContentBuilder builder = jsonBuilder()) {
            builder.startObject();
            builder.startObject("query");
            builder.startObject("range");
            builder.startObject(KEY);
            builder.field("gte", startkey);
            builder.endObject();
            builder.endObject();
            builder.endObject();
            builder.field("size", recordcount);
            builder.endObject();
            response = search(table, builder);
            @SuppressWarnings("unchecked") final Map<String, Object> map = map(response);
            @SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
            @SuppressWarnings("unchecked") final List<Map<String, Object>> list = (List<Map<String, Object>>) hits.get("hits");
            for (final Map<String, Object> hit : list) {
                @SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) hit.get("_source");
                final HashMap<String, ByteIterator> entry;
                if (fields != null) {
                    entry = new HashMap<>(fields.size());
                    for (final String field : fields) {
                        entry.put(field, new StringByteIterator((String) source.get(field)));
                    }
                } else {
                    entry = new HashMap<>(hit.size());
                    for (final Map.Entry<String, Object> field : source.entrySet()) {
                        if (KEY.equals(field.getKey())) {
                            continue;
                        }
                        entry.put(field.getKey(), new StringByteIterator((String) field.getValue()));
                    }
                }
                result.add(entry);
            }
        }
        return Status.OK;
    } catch (final Exception e) {
        e.printStackTrace();
        return Status.ERROR;
    }
}
Also used : IOException(java.io.IOException) DBException(site.ycsb.DBException) Response(org.elasticsearch.client.Response) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) StringByteIterator(site.ycsb.StringByteIterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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