Search in sources :

Example 1 with BooleanResponse

use of org.opensearch.client.transport.endpoints.BooleanResponse in project opensearch-java by opensearch-project.

the class RequestTest method testDataIngestion.

@Test
public void testDataIngestion() throws Exception {
    String index = "ingest-test";
    // Create an index
    CreateIndexResponse createIndexResponse = highLevelClient().indices().create(b -> b.index(index));
    assertEquals(index, createIndexResponse.index());
    // Check that it actually exists. Example of a boolean response
    BooleanResponse existsResponse = highLevelClient().indices().exists(b -> b.index(index));
    assertTrue(existsResponse.value());
    // Ingest some data
    AppData appData = new AppData();
    appData.setIntValue(1337);
    appData.setMsg("foo");
    String docId = highLevelClient().index(b -> b.index(index).id(// test with url-unsafe string
    "my/Id").document(appData).refresh(// Make it visible for search
    Refresh.True)).id();
    assertEquals("my/Id", docId);
    // Check auto-created mapping
    GetMappingResponse mapping = highLevelClient().indices().getMapping(b -> b.index(index));
    assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
    // Query by id
    AppData esData = highLevelClient().get(b -> b.index(index).id(docId), AppData.class).source();
    assertEquals(1337, esData.getIntValue());
    assertEquals("foo", esData.getMsg());
    // Query by id a non-existing document
    final GetResponse<AppData> notExists = highLevelClient().get(b -> b.index(index).id("some-random-id"), AppData.class);
    assertFalse(notExists.found());
    assertNull(notExists.source());
    // Search
    SearchResponse<AppData> search = highLevelClient().search(b -> b.index(index), AppData.class);
    long hits = search.hits().total().value();
    assertEquals(1, hits);
    esData = search.hits().hits().get(0).source();
    assertEquals(1337, esData.getIntValue());
    assertEquals("foo", esData.getMsg());
    RequestItem item = RequestItem.of(_1 -> _1.header(_2 -> _2.index("test")).body(_2 -> _2.size(4)));
    // MSearch: 1st search on an existing index, 2nd one on a non-existing index
    final MsearchResponse<AppData> msearch = highLevelClient().msearch(_0 -> _0.searches(_1 -> _1.header(_3 -> _3.index(index)).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))).searches(_1 -> _1.header(_3 -> _3.index("non-existing")).body(_3 -> _3.query(_4 -> _4.matchAll(_5 -> _5)))), AppData.class);
    assertEquals(2, msearch.responses().size());
    assertTrue(msearch.responses().get(0).isResult());
    assertEquals(1, msearch.responses().get(0).result().hits().hits().size());
    assertTrue(msearch.responses().get(1).isFailure());
    assertEquals(404, msearch.responses().get(1).failure().status());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Example 2 with BooleanResponse

use of org.opensearch.client.transport.endpoints.BooleanResponse in project opensearch-java by opensearch-project.

the class RequestTest method errorResponse.

@Test
public void errorResponse() throws Exception {
    BooleanResponse exists = highLevelClient().exists(_0 -> _0.index("doesnotexist").id("reallynot"));
    assertFalse(exists.value());
    OpenSearchException ex = assertThrows(OpenSearchException.class, () -> {
        GetResponse<String> response = highLevelClient().get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class);
    });
    assertEquals(404, ex.status());
    assertEquals("index_not_found_exception", ex.error().type());
    assertEquals("doesnotexist", ex.error().metadata().get("index").to(String.class));
    ExecutionException ee = assertThrows(ExecutionException.class, () -> {
        OpenSearchAsyncClient aClient = new OpenSearchAsyncClient(highLevelClient()._transport());
        GetResponse<String> response = aClient.get(_0 -> _0.index("doesnotexist").id("reallynot"), String.class).get();
    });
    ex = ((OpenSearchException) ee.getCause());
    assertEquals(404, ex.status());
    assertEquals("index_not_found_exception", ex.error().type());
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) HistogramAggregate(org.opensearch.client.opensearch._types.aggregations.HistogramAggregate) OperationType(org.opensearch.client.opensearch.core.bulk.OperationType) Property(org.opensearch.client.opensearch._types.mapping.Property) IndexResponse(org.opensearch.client.opensearch.core.IndexResponse) Map(java.util.Map) Refresh(org.opensearch.client.opensearch._types.Refresh) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) RequestItem(org.opensearch.client.opensearch.core.msearch.RequestItem) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) IndexState(org.opensearch.client.opensearch.indices.IndexState) Test(org.junit.Test) IOException(java.io.IOException) BulkResponse(org.opensearch.client.opensearch.core.BulkResponse) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) ClearScrollResponse(org.opensearch.client.opensearch.core.ClearScrollResponse) MsearchResponse(org.opensearch.client.opensearch.core.MsearchResponse) GetIndicesSettingsResponse(org.opensearch.client.opensearch.indices.GetIndicesSettingsResponse) ModelTestCase(org.opensearch.client.opensearch.model.ModelTestCase) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) NodesResponse(org.opensearch.client.opensearch.cat.NodesResponse) GetResponse(org.opensearch.client.opensearch.core.GetResponse) Collections(java.util.Collections) OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) OpenSearchException(org.opensearch.client.opensearch._types.OpenSearchException) ExecutionException(java.util.concurrent.ExecutionException) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Example 3 with BooleanResponse

use of org.opensearch.client.transport.endpoints.BooleanResponse in project opensearch-java by opensearch-project.

the class RestClientTransport method decodeResponse.

private <ResponseT> ResponseT decodeResponse(int statusCode, @Nullable HttpEntity entity, Response clientResp, Endpoint<?, ResponseT, ?> endpoint) throws IOException {
    if (endpoint instanceof BooleanEndpoint) {
        BooleanEndpoint<?> bep = (BooleanEndpoint<?>) endpoint;
        @SuppressWarnings("unchecked") ResponseT response = (ResponseT) new BooleanResponse(bep.getResult(statusCode));
        return response;
    } else if (endpoint instanceof JsonEndpoint) {
        @SuppressWarnings("unchecked") JsonEndpoint<?, ResponseT, ?> jsonEndpoint = (JsonEndpoint<?, ResponseT, ?>) endpoint;
        // Successful response
        ResponseT response = null;
        JsonpDeserializer<ResponseT> responseParser = jsonEndpoint.responseDeserializer();
        if (responseParser != null) {
            // Expecting a body
            if (entity == null) {
                throw new TransportException("Expecting a response body, but none was sent", new ResponseException(clientResp));
            }
            InputStream content = entity.getContent();
            try (JsonParser parser = mapper.jsonProvider().createParser(content)) {
                response = responseParser.deserialize(parser, mapper);
            }
            ;
        }
        return response;
    } else {
        throw new TransportException("Unhandled endpoint type: '" + endpoint.getClass().getName() + "'");
    }
}
Also used : JsonpDeserializer(org.opensearch.client.json.JsonpDeserializer) ResponseException(org.opensearch.client.ResponseException) InputStream(java.io.InputStream) BooleanEndpoint(org.opensearch.client.transport.endpoints.BooleanEndpoint) JsonEndpoint(org.opensearch.client.transport.JsonEndpoint) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse) TransportException(org.opensearch.client.transport.TransportException) JsonParser(jakarta.json.stream.JsonParser)

Example 4 with BooleanResponse

use of org.opensearch.client.transport.endpoints.BooleanResponse in project opensearch-java by opensearch-project.

the class PingAndInfoIT method testPing.

public void testPing() throws IOException {
    OpenSearchClient openSearchClient = highLevelClient();
    BooleanResponse ping = openSearchClient.ping();
    assertTrue(ping.value());
}
Also used : OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) BooleanResponse(org.opensearch.client.transport.endpoints.BooleanResponse)

Aggregations

BooleanResponse (org.opensearch.client.transport.endpoints.BooleanResponse)4 IOException (java.io.IOException)2 Collections (java.util.Collections)2 Map (java.util.Map)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeUnit (java.util.concurrent.TimeUnit)2 Test (org.junit.Test)2 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)2 OpenSearchException (org.opensearch.client.opensearch._types.OpenSearchException)2 Refresh (org.opensearch.client.opensearch._types.Refresh)2 HistogramAggregate (org.opensearch.client.opensearch._types.aggregations.HistogramAggregate)2 Property (org.opensearch.client.opensearch._types.mapping.Property)2 NodesResponse (org.opensearch.client.opensearch.cat.NodesResponse)2 BulkResponse (org.opensearch.client.opensearch.core.BulkResponse)2 ClearScrollResponse (org.opensearch.client.opensearch.core.ClearScrollResponse)2 GetResponse (org.opensearch.client.opensearch.core.GetResponse)2 IndexResponse (org.opensearch.client.opensearch.core.IndexResponse)2 MsearchResponse (org.opensearch.client.opensearch.core.MsearchResponse)2 SearchResponse (org.opensearch.client.opensearch.core.SearchResponse)2