发送请求

    1. Map<String, String> params = Collections.singletonMap("pretty", "true");
    2. Response response = restClient.performRequest("GET", "/", params); //发送一个带参数的请求
    1. Map<String, String> params = Collections.emptyMap();
    2. String jsonString = "{" +
    3. "\"user\":\"kimchy\"," +
    4. "\"postDate\":\"2013-01-30\"," +
    5. "\"message\":\"trying out Elasticsearch\"" +
    6. "}";
    7. HttpEntity entity = new NStringEntity(jsonString, ContentType.APPLICATION_JSON);// org.apache.http.HttpEntity 为了让Elasticsearch 能够解析,需要设置ContentType。
    8. Response response = restClient.performRequest("PUT", "/posts/doc/1", params, entity);
    1. ResponseListener responseListener = new ResponseListener() {
    2. @Override
    3. public void onSuccess(Response response) {
    4. // 请求成功回调
    5. @Override
    6. public void onFailure(Exception exception) {
    7. }
    8. };
    9. restClient.performRequestAsync("GET", "/", responseListener); //发送异步请求
    1. Map<String, String> params = Collections.singletonMap("pretty", "true");
    2. restClient.performRequestAsync("GET", "/", params, responseListener); // 发送带参数的异步请求
    1. HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory consumerFactory =
    2. new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(30 * 1024 * 1024);
    3. restClient.performRequestAsync("GET", "/posts/_search", params, null, consumerFactory, responseListener);
    1. final CountDownLatch latch = new CountDownLatch(documents.length);
    2. for (int i = 0; i < documents.length; i++) {
    3. restClient.performRequestAsync(
    4. "PUT",
    5. "/posts/doc/" + i,
    6. Collections.<String, String>emptyMap(),
    7. //let's assume that the documents are stored in an HttpEntity array
    8. documents[i],
    9. new ResponseListener() {
    10. latch.countDown();//处理返回响应
    11. }
    12. @Override
    13. public void onFailure(Exception exception) {
    14. latch.countDown();//处理失败响应,exception 里带错误码
    15. }
    16. }
    17. );
    18. }
    19. latch.await();
    1. Header[] headers = {
    2. new BasicHeader("header1", "value1"),
    3. new BasicHeader("header2", "value2")
    4. };
    5. restClient.performRequestAsync("GET", "/", responseListener, headers);