Search in sources :

Example 1 with CreateIndexResponse

use of org.opensearch.client.opensearch.indices.CreateIndexResponse in project opensearch-java by opensearch-project.

the class CrudIT method testExists.

public void testExists() throws IOException {
    assertFalse(highLevelClient().indices().exists(b -> b.index("index")).value());
    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
    assertTrue(highLevelClient().indices().exists(b -> b.index(index)).value());
    highLevelClient().index(b -> b.index(index).id("id").document(Collections.singletonMap("foo", "bar")).refresh(Refresh.True));
    assertTrue(highLevelClient().exists(b -> b.index(index).id("id")).value());
    assertFalse(highLevelClient().exists(b -> b.index(index).id("random_id")).value());
    assertFalse(highLevelClient().exists(b -> b.index(index).id("random_id").version(1L)).value());
}
Also used : CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse)

Example 2 with CreateIndexResponse

use of org.opensearch.client.opensearch.indices.CreateIndexResponse 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 3 with CreateIndexResponse

use of org.opensearch.client.opensearch.indices.CreateIndexResponse in project opensearch-java by opensearch-project.

the class IndicesClientIT method testCreateIndex.

public void testCreateIndex() throws Exception {
    OpenSearchAsyncClient asyncClient = new OpenSearchAsyncClient(highLevelClient()._transport());
    CreateIndexResponse createResponse = highLevelClient().indices().create(b -> b.index("my-index"));
    assertTrue(createResponse.acknowledged());
    assertTrue(createResponse.shardsAcknowledged());
    // Find info about it, using the async client
    CompletableFuture<GetIndexResponse> futureResponse = asyncClient.indices().get(b -> b.index("my-index"));
    GetIndexResponse response = futureResponse.get(10, TimeUnit.SECONDS);
    Map<String, IndexState> indices = response.result();
    assertEquals(1, indices.size());
    assertNotNull(indices.get("my-index"));
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) GetIndexResponse(org.opensearch.client.opensearch.indices.GetIndexResponse) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) IndexState(org.opensearch.client.opensearch.indices.IndexState)

Example 4 with CreateIndexResponse

use of org.opensearch.client.opensearch.indices.CreateIndexResponse in project opensearch-java by opensearch-project.

the class ApiConventionsTest method builderLambdas.

@Test(expected = TransportException.class)
public void builderLambdas() throws Exception {
    OpenSearchClient client = new OpenSearchClient(transport);
    // tag::builder-lambdas
    CreateIndexResponse createResponse = client.indices().create(createIndexBuilder -> createIndexBuilder.index("my-index").aliases("foo", aliasBuilder -> aliasBuilder.isWriteIndex(true)));
// end::builder-lambdas
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) Alias(org.opensearch.client.opensearch.indices.Alias) Query(org.opensearch.client.opensearch._types.query_dsl.Query) CreateIndexRequest(org.opensearch.client.opensearch.indices.CreateIndexRequest) LogManager(java.util.logging.LogManager) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) HashMap(java.util.HashMap) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Logger(java.util.logging.Logger) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) TransportException(org.opensearch.client.transport.TransportException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) NodeStatistics(org.opensearch.client.opensearch._types.NodeStatistics) SortOrder(org.opensearch.client.opensearch._types.SortOrder) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) Assert(org.junit.Assert) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Test(org.junit.Test)

Example 5 with CreateIndexResponse

use of org.opensearch.client.opensearch.indices.CreateIndexResponse in project opensearch-java by opensearch-project.

the class ApiConventionsTest method builderLambdasShort.

@Test(expected = TransportException.class)
public void builderLambdasShort() throws Exception {
    OpenSearchClient client = new OpenSearchClient(transport);
    // tag::builder-lambdas-short
    CreateIndexResponse createResponse = client.indices().create(c -> c.index("my-index").aliases("foo", a -> a.isWriteIndex(true)));
// end::builder-lambdas-short
}
Also used : OpenSearchAsyncClient(org.opensearch.client.opensearch.OpenSearchAsyncClient) Arrays(java.util.Arrays) Aggregation(org.opensearch.client.opensearch._types.aggregations.Aggregation) Alias(org.opensearch.client.opensearch.indices.Alias) Query(org.opensearch.client.opensearch._types.query_dsl.Query) CreateIndexRequest(org.opensearch.client.opensearch.indices.CreateIndexRequest) LogManager(java.util.logging.LogManager) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) Test(org.junit.Test) HashMap(java.util.HashMap) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Logger(java.util.logging.Logger) SearchResponse(org.opensearch.client.opensearch.core.SearchResponse) OpenSearchTransport(org.opensearch.client.transport.OpenSearchTransport) TransportException(org.opensearch.client.transport.TransportException) List(java.util.List) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) NodeStatistics(org.opensearch.client.opensearch._types.NodeStatistics) SortOrder(org.opensearch.client.opensearch._types.SortOrder) ApiTypeHelper(org.opensearch.client.util.ApiTypeHelper) Map(java.util.Map) Assert(org.junit.Assert) OpenSearchClient(org.opensearch.client.opensearch.OpenSearchClient) CreateIndexResponse(org.opensearch.client.opensearch.indices.CreateIndexResponse) Test(org.junit.Test)

Aggregations

CreateIndexResponse (org.opensearch.client.opensearch.indices.CreateIndexResponse)7 Test (org.junit.Test)5 OpenSearchAsyncClient (org.opensearch.client.opensearch.OpenSearchAsyncClient)5 Map (java.util.Map)3 OpenSearchClient (org.opensearch.client.opensearch.OpenSearchClient)3 SearchResponse (org.opensearch.client.opensearch.core.SearchResponse)3 Alias (org.opensearch.client.opensearch.indices.Alias)3 GetIndexResponse (org.opensearch.client.opensearch.indices.GetIndexResponse)3 IndexState (org.opensearch.client.opensearch.indices.IndexState)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 List (java.util.List)2 LogManager (java.util.logging.LogManager)2 Logger (java.util.logging.Logger)2 Assert (org.junit.Assert)2 NodeStatistics (org.opensearch.client.opensearch._types.NodeStatistics)2 SortOrder (org.opensearch.client.opensearch._types.SortOrder)2 Aggregation (org.opensearch.client.opensearch._types.aggregations.Aggregation)2 Query (org.opensearch.client.opensearch._types.query_dsl.Query)2 SearchRequest (org.opensearch.client.opensearch.core.SearchRequest)2