use of 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()));
}
use of 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()));
}
use of 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()));
}
use of org.elasticsearch.client.Response in project elasticsearch by elastic.
the class ElasticsearchHostsSnifferTests method testSniffNodes.
public void testSniffNodes() throws IOException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
ElasticsearchHostsSniffer sniffer = new ElasticsearchHostsSniffer(restClient, sniffRequestTimeout, scheme);
try {
List<HttpHost> sniffedHosts = sniffer.sniffHosts();
if (sniffResponse.isFailure) {
fail("sniffNodes should have failed");
}
assertThat(sniffedHosts.size(), equalTo(sniffResponse.hosts.size()));
Iterator<HttpHost> responseHostsIterator = sniffResponse.hosts.iterator();
for (HttpHost sniffedHost : sniffedHosts) {
assertEquals(sniffedHost, responseHostsIterator.next());
}
} catch (ResponseException e) {
Response response = e.getResponse();
if (sniffResponse.isFailure) {
assertThat(e.getMessage(), containsString("GET " + httpHost + "/_nodes/http?timeout=" + sniffRequestTimeout + "ms"));
assertThat(e.getMessage(), containsString(Integer.toString(sniffResponse.nodesInfoResponseCode)));
assertThat(response.getHost(), equalTo(httpHost));
assertThat(response.getStatusLine().getStatusCode(), equalTo(sniffResponse.nodesInfoResponseCode));
assertThat(response.getRequestLine().toString(), equalTo("GET /_nodes/http?timeout=" + sniffRequestTimeout + "ms HTTP/1.1"));
} else {
fail("sniffNodes should have succeeded: " + response.getStatusLine());
}
}
}
}
use of org.elasticsearch.client.Response in project elasticsearch by elastic.
the class Netty4BadRequestIT method testBadRequest.
public void testBadRequest() throws IOException {
final Response response = client().performRequest("GET", "/_nodes/settings", Collections.emptyMap());
final ObjectPath objectPath = ObjectPath.createFromResponse(response);
final Map<String, Object> map = objectPath.evaluate("nodes");
int maxMaxInitialLineLength = Integer.MIN_VALUE;
final Setting<ByteSizeValue> httpMaxInitialLineLength = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
final String key = httpMaxInitialLineLength.getKey().substring("http.".length());
for (Map.Entry<String, Object> entry : map.entrySet()) {
@SuppressWarnings("unchecked") final Map<String, Object> settings = (Map<String, Object>) ((Map<String, Object>) entry.getValue()).get("settings");
final int maxIntialLineLength;
if (settings.containsKey("http")) {
@SuppressWarnings("unchecked") final Map<String, Object> httpSettings = (Map<String, Object>) settings.get("http");
if (httpSettings.containsKey(key)) {
maxIntialLineLength = ByteSizeValue.parseBytesSizeValue((String) httpSettings.get(key), key).bytesAsInt();
} else {
maxIntialLineLength = httpMaxInitialLineLength.getDefault(Settings.EMPTY).bytesAsInt();
}
} else {
maxIntialLineLength = httpMaxInitialLineLength.getDefault(Settings.EMPTY).bytesAsInt();
}
maxMaxInitialLineLength = Math.max(maxMaxInitialLineLength, maxIntialLineLength);
}
final String path = "/" + new String(new byte[maxMaxInitialLineLength], Charset.forName("UTF-8")).replace('\0', 'a');
final ResponseException e = expectThrows(ResponseException.class, () -> client().performRequest(randomFrom("GET", "POST", "PUT"), path, Collections.emptyMap()));
assertThat(e.getResponse().getStatusLine().getStatusCode(), equalTo(BAD_REQUEST.getStatus()));
assertThat(e, hasToString(containsString("too_long_frame_exception")));
assertThat(e, hasToString(matches("An HTTP line is larger than \\d+ bytes")));
}
Aggregations