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()));
}
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;
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.
the class ElasticsearchRestClient method update.
@Override
public Status update(final String table, final String key, final Map<String, ByteIterator> values) {
try {
final Response searchResponse = search(table, key);
final int statusCode = searchResponse.getStatusLine().getStatusCode();
if (statusCode == 404) {
return Status.NOT_FOUND;
} else if (statusCode != HttpStatus.SC_OK) {
return Status.ERROR;
}
final Map<String, Object> map = map(searchResponse);
@SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
final int total = (int) hits.get("total");
if (total == 0) {
return Status.NOT_FOUND;
}
@SuppressWarnings("unchecked") final Map<String, Object> hit = (Map<String, Object>) ((List<Object>) hits.get("hits")).get(0);
@SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) hit.get("_source");
for (final Map.Entry<String, String> entry : StringByteIterator.getStringMap(values).entrySet()) {
source.put(entry.getKey(), entry.getValue());
}
final Map<String, String> params = emptyMap();
final Response response = restClient.performRequest("PUT", "/" + indexKey + "/" + table + "/" + hit.get("_id"), params, new NStringEntity(new ObjectMapper().writeValueAsString(source), ContentType.APPLICATION_JSON));
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return Status.ERROR;
}
if (!isRefreshNeeded) {
synchronized (this) {
isRefreshNeeded = true;
}
}
return Status.OK;
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project YCSB by brianfrankcooper.
the class ElasticsearchRestClient method read.
@Override
public Status read(final String table, final String key, final Set<String> fields, final Map<String, ByteIterator> result) {
try {
final Response searchResponse = search(table, key);
final int statusCode = searchResponse.getStatusLine().getStatusCode();
if (statusCode == 404) {
return Status.NOT_FOUND;
} else if (statusCode != HttpStatus.SC_OK) {
return Status.ERROR;
}
final Map<String, Object> map = map(searchResponse);
@SuppressWarnings("unchecked") final Map<String, Object> hits = (Map<String, Object>) map.get("hits");
final int total = (int) hits.get("total");
if (total == 0) {
return Status.NOT_FOUND;
}
@SuppressWarnings("unchecked") final Map<String, Object> hit = (Map<String, Object>) ((List<Object>) hits.get("hits")).get(0);
@SuppressWarnings("unchecked") final Map<String, Object> source = (Map<String, Object>) hit.get("_source");
if (fields != null) {
for (final String field : fields) {
result.put(field, new StringByteIterator((String) source.get(field)));
}
} else {
for (final Map.Entry<String, Object> e : source.entrySet()) {
if (KEY.equals(e.getKey())) {
continue;
}
result.put(e.getKey(), new StringByteIterator((String) e.getValue()));
}
}
return Status.OK;
} catch (final Exception e) {
e.printStackTrace();
return Status.ERROR;
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project spring-boot by spring-projects.
the class ElasticsearchRestHealthIndicatorTests method elasticsearchIsOutOfServiceByStatus.
@Test
void elasticsearchIsOutOfServiceByStatus() throws IOException {
BasicHttpEntity httpEntity = new BasicHttpEntity();
httpEntity.setContent(new ByteArrayInputStream(createJsonResult(200, "red").getBytes()));
Response response = mock(Response.class);
StatusLine statusLine = mock(StatusLine.class);
given(statusLine.getStatusCode()).willReturn(200);
given(response.getStatusLine()).willReturn(statusLine);
given(response.getEntity()).willReturn(httpEntity);
given(this.restClient.performRequest(any(Request.class))).willReturn(response);
Health health = this.elasticsearchRestHealthIndicator.health();
assertThat(health.getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
assertHealthDetailsWithStatus(health.getDetails(), "red");
}
Aggregations