Search in sources :

Example 36 with Response

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;
    }
}
Also used : Response(org.elasticsearch.client.Response) NStringEntity(org.apache.http.nio.entity.NStringEntity) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Example 37 with Response

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;
    }
}
Also used : Response(org.elasticsearch.client.Response) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) IOException(java.io.IOException) DBException(site.ycsb.DBException)

Example 38 with Response

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");
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) ByteArrayInputStream(java.io.ByteArrayInputStream) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) BasicHttpEntity(org.apache.http.entity.BasicHttpEntity) Test(org.junit.jupiter.api.Test)

Example 39 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project spring-boot by spring-projects.

the class ElasticsearchRestHealthIndicatorTests method elasticsearchIsDownByResponseCode.

@Test
void elasticsearchIsDownByResponseCode() throws IOException {
    Response response = mock(Response.class);
    StatusLine statusLine = mock(StatusLine.class);
    given(statusLine.getStatusCode()).willReturn(500);
    given(statusLine.getReasonPhrase()).willReturn("Internal server error");
    given(response.getStatusLine()).willReturn(statusLine);
    given(this.restClient.performRequest(any(Request.class))).willReturn(response);
    Health health = this.elasticsearchRestHealthIndicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails()).contains(entry("statusCode", 500), entry("reasonPhrase", "Internal server error"));
}
Also used : Response(org.elasticsearch.client.Response) StatusLine(org.apache.http.StatusLine) Health(org.springframework.boot.actuate.health.Health) Request(org.elasticsearch.client.Request) Test(org.junit.jupiter.api.Test)

Example 40 with Response

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response in project graylog2-server by Graylog2.

the class ElasticsearchBackendSearchTypesWithStreamsOverridesTest method setUp.

@Before
public void setUp() throws Exception {
    final MultiSearchResponse response = TestMultisearchResponse.fromFixture("successfulMultiSearchResponse.json");
    final List<MultiSearchResponse.Item> items = Arrays.stream(response.getResponses()).collect(Collectors.toList());
    when(client.msearch(any(), any())).thenReturn(items);
    when(indexLookup.indexNamesForStreamsInTimeRange(eq(ImmutableSet.of(stream1Id)), any())).thenReturn(ImmutableSet.of("index1", "index2"));
    when(indexLookup.indexNamesForStreamsInTimeRange(eq(ImmutableSet.of(stream2Id)), any())).thenReturn(ImmutableSet.of("index3"));
}
Also used : MultiSearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.MultiSearchResponse) Before(org.junit.Before)

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