Search in sources :

Example 1 with GetMappingResponse

use of co.elastic.clients.elasticsearch.indices.GetMappingResponse in project elasticsearch-java by elastic.

the class RequestTest method testGetMapping.

@Test
public void testGetMapping() throws Exception {
    // See also VariantsTest.testNestedTaggedUnionWithDefaultTag()
    // and https://github.com/elastic/elasticsearch-java/issues/45
    String index = "testindex";
    Map<String, Property> fields = Collections.singletonMap("keyword", Property.of(p -> p.keyword(k -> k.ignoreAbove(256))));
    Property text = Property.of(p -> p.text(t -> t.fields(fields)));
    client.indices().create(c -> c.index(index).mappings(m -> m.properties("id", text).properties("name", p -> p.object(o -> o.properties("first", text).properties("last", text)))));
    GetMappingResponse mr = client.indices().getMapping(mrb -> mrb.index(index));
    assertNotNull(mr.result().get(index));
    assertNotNull(mr.result().get(index).mappings().properties().get("name").object());
}
Also used : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) BeforeClass(org.junit.BeforeClass) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) CompletableFuture(java.util.concurrent.CompletableFuture) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) DateTime(co.elastic.clients.util.DateTime) Map(java.util.Map) OperationType(co.elastic.clients.elasticsearch.core.bulk.OperationType) ModelTestCase(co.elastic.clients.elasticsearch.model.ModelTestCase) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Refresh(co.elastic.clients.elasticsearch._types.Refresh) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) NodesResponse(co.elastic.clients.elasticsearch.cat.NodesResponse) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ElasticsearchTestServer(co.elastic.clients.elasticsearch.ElasticsearchTestServer) Test(org.junit.Test) IOException(java.io.IOException) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) Instant(java.time.Instant) GetIndicesSettingsResponse(co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) MsearchResponse(co.elastic.clients.elasticsearch.core.MsearchResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClosePointInTimeResponse(co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) DiskUsageResponse(co.elastic.clients.elasticsearch.indices.DiskUsageResponse) ClearScrollResponse(co.elastic.clients.elasticsearch.core.ClearScrollResponse) Assert(org.junit.Assert) Collections(java.util.Collections) IndexResponse(co.elastic.clients.elasticsearch.core.IndexResponse) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Test(org.junit.Test)

Example 2 with GetMappingResponse

use of co.elastic.clients.elasticsearch.indices.GetMappingResponse in project elasticsearch-java by elastic.

the class VariantsTest method testNestedTaggedUnionWithDefaultTag.

@Test
public void testNestedTaggedUnionWithDefaultTag() {
    // https://github.com/elastic/elasticsearch-java/issues/45
    // Object fields don't really exist in ES and are based on a naming convention where field names
    // are dot-separated paths. The hierarchy is rebuilt from these names and ES doesn't send back
    // "type": "object" for object properties.
    // See https://www.elastic.co/guide/en/elasticsearch/reference/current/object.html
    // 
    // Mappings are therefore a hierarchy of internally-tagged unions based on the "type" property
    // with a default "object" tag value if the "type" property is missing.
    String json = "{\n" + "  \"testindex\" : {\n" + "    \"mappings\" : {\n" + "      \"properties\" : {\n" + "        \"id\" : {\n" + "          \"type\" : \"text\",\n" + "          \"fields\" : {\n" + "            \"keyword\" : {\n" + "              \"type\" : \"keyword\",\n" + "              \"ignore_above\" : 256\n" + "            }\n" + "          }\n" + "        },\n" + "        \"name\" : {\n" + "          \"properties\" : {\n" + "            \"first\" : {\n" + "              \"type\" : \"text\",\n" + "              \"fields\" : {\n" + "                \"keyword\" : {\n" + "                  \"type\" : \"keyword\",\n" + "                  \"ignore_above\" : 256\n" + "                }\n" + "              }\n" + "            },\n" + "            \"last\" : {\n" + "              \"type\" : \"text\",\n" + "              \"fields\" : {\n" + "                \"keyword\" : {\n" + "                  \"type\" : \"keyword\",\n" + "                  \"ignore_above\" : 256\n" + "                }\n" + "              }\n" + "            }\n" + "          }\n" + "        }\n" + "      }\n" + "    }\n" + "  }\n" + "}";
    GetMappingResponse response = fromJson(json, GetMappingResponse.class);
    TypeMapping mappings = response.get("testindex").mappings();
    assertTrue(mappings.properties().get("name").isObject());
    assertEquals(256, mappings.properties().get("name").object().properties().get("first").text().fields().get("keyword").keyword().ignoreAbove().longValue());
    assertTrue(mappings.properties().get("id").isText());
    assertEquals(256, mappings.properties().get("id").text().fields().get("keyword").keyword().ignoreAbove().longValue());
}
Also used : GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) TypeMapping(co.elastic.clients.elasticsearch._types.mapping.TypeMapping) Test(org.junit.Test)

Example 3 with GetMappingResponse

use of co.elastic.clients.elasticsearch.indices.GetMappingResponse in project elasticsearch-java by elastic.

the class RequestTest method testDataIngestion.

@Test
public void testDataIngestion() throws Exception {
    String index = "ingest-test";
    // Create an index
    CreateIndexResponse createIndexResponse = client.indices().create(b -> b.index(index));
    assertEquals(index, createIndexResponse.index());
    // Check that it actually exists. Example of a boolean response
    BooleanResponse existsResponse = client.indices().exists(b -> b.index(index));
    assertTrue(existsResponse.value());
    // Ingest some data
    AppData appData = new AppData();
    appData.setIntValue(1337);
    appData.setMsg("foo");
    String docId = client.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 = client.indices().getMapping(b -> b.index(index));
    assertEquals(Property.Kind.Long, mapping.get("ingest-test").mappings().properties().get("intValue")._kind());
    // Query by id
    AppData esData = client.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 = client.get(b -> b.index(index).id("some-random-id"), AppData.class);
    assertFalse(notExists.found());
    assertNull(notExists.source());
    // Search
    SearchResponse<AppData> search = client.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 = client.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 : CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) BeforeClass(org.junit.BeforeClass) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) CompletableFuture(java.util.concurrent.CompletableFuture) IndexState(co.elastic.clients.elasticsearch.indices.IndexState) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) DateTime(co.elastic.clients.util.DateTime) Map(java.util.Map) OperationType(co.elastic.clients.elasticsearch.core.bulk.OperationType) ModelTestCase(co.elastic.clients.elasticsearch.model.ModelTestCase) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Refresh(co.elastic.clients.elasticsearch._types.Refresh) ElasticsearchException(co.elastic.clients.elasticsearch._types.ElasticsearchException) HistogramAggregate(co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate) NodesResponse(co.elastic.clients.elasticsearch.cat.NodesResponse) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ElasticsearchTestServer(co.elastic.clients.elasticsearch.ElasticsearchTestServer) Test(org.junit.Test) IOException(java.io.IOException) GetIndexResponse(co.elastic.clients.elasticsearch.indices.GetIndexResponse) Instant(java.time.Instant) GetIndicesSettingsResponse(co.elastic.clients.elasticsearch.indices.GetIndicesSettingsResponse) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) MsearchResponse(co.elastic.clients.elasticsearch.core.MsearchResponse) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClosePointInTimeResponse(co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse) DateTimeFormatter(java.time.format.DateTimeFormatter) BulkResponse(co.elastic.clients.elasticsearch.core.BulkResponse) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) DiskUsageResponse(co.elastic.clients.elasticsearch.indices.DiskUsageResponse) ClearScrollResponse(co.elastic.clients.elasticsearch.core.ClearScrollResponse) Assert(org.junit.Assert) Collections(java.util.Collections) IndexResponse(co.elastic.clients.elasticsearch.core.IndexResponse) ElasticsearchAsyncClient(co.elastic.clients.elasticsearch.ElasticsearchAsyncClient) RequestItem(co.elastic.clients.elasticsearch.core.msearch.RequestItem) GetMappingResponse(co.elastic.clients.elasticsearch.indices.GetMappingResponse) CreateIndexResponse(co.elastic.clients.elasticsearch.indices.CreateIndexResponse) BooleanResponse(co.elastic.clients.transport.endpoints.BooleanResponse) Test(org.junit.Test)

Aggregations

GetMappingResponse (co.elastic.clients.elasticsearch.indices.GetMappingResponse)3 Test (org.junit.Test)3 ElasticsearchAsyncClient (co.elastic.clients.elasticsearch.ElasticsearchAsyncClient)2 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)2 ElasticsearchTestServer (co.elastic.clients.elasticsearch.ElasticsearchTestServer)2 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)2 Refresh (co.elastic.clients.elasticsearch._types.Refresh)2 HistogramAggregate (co.elastic.clients.elasticsearch._types.aggregations.HistogramAggregate)2 Property (co.elastic.clients.elasticsearch._types.mapping.Property)2 NodesResponse (co.elastic.clients.elasticsearch.cat.NodesResponse)2 BulkResponse (co.elastic.clients.elasticsearch.core.BulkResponse)2 ClearScrollResponse (co.elastic.clients.elasticsearch.core.ClearScrollResponse)2 ClosePointInTimeResponse (co.elastic.clients.elasticsearch.core.ClosePointInTimeResponse)2 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)2 IndexResponse (co.elastic.clients.elasticsearch.core.IndexResponse)2 MsearchResponse (co.elastic.clients.elasticsearch.core.MsearchResponse)2 SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)2 OperationType (co.elastic.clients.elasticsearch.core.bulk.OperationType)2 RequestItem (co.elastic.clients.elasticsearch.core.msearch.RequestItem)2 CreateIndexResponse (co.elastic.clients.elasticsearch.indices.CreateIndexResponse)2