use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project janusgraph by JanusGraph.
the class RestElasticSearchClient method search.
@Override
public RestSearchResponse search(String scrollId) throws IOException {
final String path;
final byte[] requestData;
if (ElasticMajorVersion.ONE == majorVersion) {
path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll" + REQUEST_PARAM_BEGINNING + "scroll=" + scrollKeepAlive;
requestData = scrollId.getBytes(UTF8_CHARSET);
} else {
path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll";
final Map<String, Object> request = new HashMap<>();
request.put("scroll", scrollKeepAlive);
request.put("scroll_id", scrollId);
requestData = mapper.writeValueAsBytes(request);
if (log.isDebugEnabled()) {
log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request));
}
}
final Response response = performRequest(REQUEST_TYPE_POST, path, requestData);
try (final InputStream inputStream = response.getEntity().getContent()) {
return mapper.readValue(inputStream, RestSearchResponse.class);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project nifi by apache.
the class ElasticSearchClientServiceImpl method search.
@Override
public SearchResponse search(String query, String index, String type) throws IOException {
Response response = runQuery(query, index, type);
Map<String, Object> parsed = parseResponse(response);
int took = (Integer) parsed.get("took");
boolean timedOut = (Boolean) parsed.get("timed_out");
Map<String, Object> aggregations = parsed.get("aggregations") != null ? (Map<String, Object>) parsed.get("aggregations") : new HashMap<>();
Map<String, Object> hitsParent = (Map<String, Object>) parsed.get("hits");
int count = (Integer) hitsParent.get("total");
List<Map<String, Object>> hits = (List<Map<String, Object>>) hitsParent.get("hits");
SearchResponse esr = new SearchResponse(hits, aggregations, count, took, timedOut);
if (getLogger().isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("******************");
sb.append(String.format("Took: %d", took));
sb.append(String.format("Timed out: %s", timedOut));
sb.append(String.format("Aggregation count: %d", aggregations.size()));
sb.append(String.format("Hit count: %d", hits.size()));
sb.append(String.format("Total found: %d", count));
sb.append("******************");
getLogger().debug(sb.toString());
}
return esr;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project components by Talend.
the class ElasticsearchBeamRuntimeTestIT method init.
@Before
public void init() throws IOException, ExecutionException, InterruptedException {
client = ElasticsearchTestUtils.createClient(ElasticsearchTestConstants.HOSTS.split(":")[0], ElasticsearchTestConstants.TRANSPORT_PORT, ElasticsearchTestConstants.CLUSTER_NAME);
datastoreProperties = new ElasticsearchDatastoreProperties("datastore");
datastoreProperties.init();
datastoreProperties.nodes.setValue(ElasticsearchTestConstants.HOSTS);
RestClient restClient = ElasticsearchConnection.createClient(datastoreProperties);
BasicHeader emptyHeader = new BasicHeader("", "");
Map<String, String> emptyParams = new HashMap<>();
ElasticsearchTestUtils.deleteIndex(INDEX_NAME, client);
Response checkExistsResponse = restClient.performRequest("HEAD", "/" + INDEX_NAME, emptyParams);
ElasticsearchResponse checkExists = new ElasticsearchResponse(checkExistsResponse);
if (!checkExists.isOk()) {
// create index for test, name is 'beam'
restClient.performRequest("PUT", "/" + INDEX_NAME, emptyHeader);
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project kylo by Teradata.
the class ElasticSearchRestService method deleteAll.
@Override
public int deleteAll(@Nonnull String indexName, @Nonnull String typeName) {
try (RestClient restClient = buildRestClient()) {
if ((restClientConfig.getEsversion() != null) && (restClientConfig.getEsversion().equals(VERSION_TWO))) {
log.info("Elasticsearch v2");
Response response = restClient.performRequest(DELETE_METHOD, getAllDocumentsDeleteEndPointEsV2(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDslV2());
if (response != null) {
try {
String entityString = EntityUtils.toString(response.getEntity());
JSONObject entityStringJsonObject = new JSONObject(entityString);
JSONObject indicesJsonObject = entityStringJsonObject.getJSONObject("_indices");
JSONObject allJsonObject = indicesJsonObject.getJSONObject("_all");
return (Integer) allJsonObject.get("deleted");
} catch (JSONException e) {
log.warn("Unable to get number of documents deleted. [{}]", e);
}
} else {
log.warn("Unable to identify number of documents deleted since null response received");
}
} else {
log.info("Elasticsearch v5 or above");
Response response = restClient.performRequest(POST_METHOD, getAllDocumentsDeleteEndPoint(indexName, typeName), new HashMap<>(), getAllDocumentsDeleteRequestBodyDsl());
if (response != null) {
try {
String entityString = EntityUtils.toString(response.getEntity());
JSONObject entityStringJsonObject = new JSONObject(entityString);
return (Integer) entityStringJsonObject.get("deleted");
} catch (JSONException e) {
log.warn("Unable to get number of documents deleted. [{}]", e);
}
} else {
log.warn("Unable to identify number of documents deleted since null response received");
}
}
log.info("Deleted all-documents for index={}, type={}", indexName, typeName);
} catch (ResponseException responseException) {
log.error("Index all-documents deletion encountered issues in Elasticsearch for index={}, type={} [{}]", indexName, typeName, responseException);
} catch (ClientProtocolException clientProtocolException) {
log.error("Http protocol error for delete all-documents for index={}, type={} [{}]", indexName, typeName, clientProtocolException);
} catch (IOException ioException) {
log.error("IO Error in rest client [{}]", ioException);
}
return 0;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project immutables by immutables.
the class ScrollingTest method assertNoActiveScrolls.
/**
* Ensures there are no pending scroll contexts in elastic search cluster.
* Queries {@code /_nodes/stats/indices/search} endpoint.
* @see <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html">Indices Stats</a>
*/
// for errorprone
@SuppressWarnings("unused")
private void assertNoActiveScrolls() throws Exception {
// get node stats
final Response response = restClient.performRequest(new Request("GET", "/_nodes/stats/indices/search"));
try (InputStream is = response.getEntity().getContent()) {
final ObjectNode node = backend().objectMapper.readValue(is, ObjectNode.class);
final String path = "/indices/search/scroll_current";
final JsonNode scrollCurrent = node.with("nodes").elements().next().at(path);
if (scrollCurrent.isMissingNode()) {
throw new IllegalStateException("Couldn't find node at " + path);
}
final int activeScrolls = scrollCurrent.asInt();
if (activeScrolls != 0) {
throw new AssertionError(String.format("Expected no active scrolls but got %d. " + "Current index stats %s", activeScrolls, node));
}
}
}
Aggregations