Search in sources :

Example 1 with Property

use of co.elastic.clients.elasticsearch._types.mapping.Property in project weicoder by wdcode.

the class ElasticSearch method create.

/**
 * 创建索引
 *
 * @param <E>
 * @param index 索引对象
 * @throws IOException
 */
public void create(Index index) {
    try {
        // 创建索引
        // CreateIndexRequest request =  new CreateIndexRequest(getIndexName(index));
        CreateIndexRequest request = CreateIndexRequest.of(r -> r.index(getIndexName(index)).settings(s -> s.numberOfRoutingShards(index.shards()).numberOfReplicas(index.replica())));
        // 创建索引对象类型
        // Map<String, Object> properties = Maps.newMap();
        Map<String, Property> properties = Maps.newMap();
        // 获得所有索引字段 根据数据类型设置
        // BeanUtil.getFields(index.getClass())
        // .forEach(f -> properties.put(f.getName(), Maps.newMap("type", f.getType())));
        BeanUtil.getFields(index.getClass()).forEach(f -> properties.put(f.getName(), Property.of(p -> p.searchAsYouType(v -> v.name(f.getName())))));
        // f.getType())));xxxxxxx
        // request.mapping(JsonEngine.toJson(Maps.newMap("properties", properties)));//, XContentType.JSON
        request.mappings().properties().putAll(properties);
        // 创建索引
        // client.indices().create(request, RequestOptions.DEFAULT);
        client.indices().create(c -> c.index("products"));
    } catch (IOException e) {
        Logs.error(e);
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) RestClientTransport(co.elastic.clients.transport.rest_client.RestClientTransport) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) Maps(com.weicoder.common.lang.Maps) W(com.weicoder.common.W) U(com.weicoder.common.U) CreateIndexRequest(co.elastic.clients.elasticsearch.indices.CreateIndexRequest) Map(java.util.Map) IndexRequest(co.elastic.clients.elasticsearch.core.IndexRequest) BeanUtil(com.weicoder.common.util.BeanUtil) Property(co.elastic.clients.elasticsearch._types.mapping.Property) Lists(com.weicoder.common.lang.Lists) DeleteIndexRequest(co.elastic.clients.elasticsearch.indices.DeleteIndexRequest) ElasticSearchParams(com.weicoder.elasticsearch.params.ElasticSearchParams) JsonEngine(com.weicoder.json.JsonEngine) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) Index(com.weicoder.elasticsearch.annotation.Index) SourceConfig(co.elastic.clients.elasticsearch.core.search.SourceConfig) ElasticsearchClient(co.elastic.clients.elasticsearch.ElasticsearchClient) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) HttpHost(org.apache.http.HttpHost) Logs(com.weicoder.common.log.Logs) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) IOException(java.io.IOException) CreateIndexRequest(co.elastic.clients.elasticsearch.indices.CreateIndexRequest) Property(co.elastic.clients.elasticsearch._types.mapping.Property)

Example 2 with Property

use of co.elastic.clients.elasticsearch._types.mapping.Property in project elasticsearch-java by elastic.

the class BehaviorsTest method testShortcutProperty.

@Test
public void testShortcutProperty() {
    // All-in-one: a variant, wrapping a single-key dictionary with a shortcut property
    String json = "{\"term\":{\"some-field\":\"some-value\"}}";
    Query q = fromJson(json, Query.class);
    assertEquals("some-field", q.term().field());
    assertEquals("some-value", q.term().value().stringValue());
}
Also used : TermQuery(co.elastic.clients.elasticsearch._types.query_dsl.TermQuery) ShapeQuery(co.elastic.clients.elasticsearch._types.query_dsl.ShapeQuery) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) Test(org.junit.Test)

Example 3 with Property

use of co.elastic.clients.elasticsearch._types.mapping.Property 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 4 with Property

use of co.elastic.clients.elasticsearch._types.mapping.Property in project opensearch-java by opensearch-project.

the class VariantsTest method testInternalTag.

@Test
public void testInternalTag() {
    String expected = "{\"type\":\"ip\",\"fields\":{\"a-field\":{\"type\":\"float\",\"coerce\":true}},\"boost\":1" + ".0,\"index\":true}";
    Property p = Property.of(_0 -> _0.ip(_1 -> _1.index(true).boost(1.0).fields("a-field", _3 -> _3.float_(_4 -> _4.coerce(true)))));
    assertEquals(expected, toJson(p));
    Property property = fromJson(expected, Property.class);
    assertTrue(property.ip().index());
    assertEquals(1.0, property.ip().boost().doubleValue(), 0.09);
    assertTrue(property.ip().fields().get("a-field").float_().coerce());
}
Also used : SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) Query(org.opensearch.client.opensearch._types.query_dsl.Query) JsonData(org.opensearch.client.json.JsonData) Property(org.opensearch.client.opensearch._types.mapping.Property) QueryBuilders(org.opensearch.client.opensearch._types.query_dsl.QueryBuilders) Test(org.junit.Test) TypeMapping(org.opensearch.client.opensearch._types.mapping.TypeMapping) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) Property(org.opensearch.client.opensearch._types.mapping.Property) Test(org.junit.Test)

Example 5 with Property

use of co.elastic.clients.elasticsearch._types.mapping.Property in project opensearch-java by opensearch-project.

the class RequestTest method testGetMapping.

@Test
public void testGetMapping() throws Exception {
    // See also VariantsTest.testNestedTaggedUnionWithDefaultTag()
    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)));
    highLevelClient().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 = highLevelClient().indices().getMapping(mrb -> mrb.index(index));
    assertNotNull(mr.result().get(index));
    assertNotNull(mr.result().get(index).mappings().properties().get("name").object());
}
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) GetMappingResponse(org.opensearch.client.opensearch.indices.GetMappingResponse) Property(org.opensearch.client.opensearch._types.mapping.Property) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)10 Property (co.elastic.clients.elasticsearch._types.mapping.Property)5 Map (java.util.Map)5 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)3 GetMappingResponse (co.elastic.clients.elasticsearch.indices.GetMappingResponse)3 JsonData (co.elastic.clients.json.JsonData)3 IOException (java.io.IOException)3 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)2 ErrorCause (co.elastic.clients.elasticsearch._types.ErrorCause)2 TypeMapping (co.elastic.clients.elasticsearch._types.mapping.TypeMapping)2 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)2 QueryBuilders (co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 List (java.util.List)2 ElasticsearchAsyncClient (co.elastic.clients.elasticsearch.ElasticsearchAsyncClient)1 ElasticsearchTestServer (co.elastic.clients.elasticsearch.ElasticsearchTestServer)1 ElasticsearchException (co.elastic.clients.elasticsearch._types.ElasticsearchException)1 ErrorResponse (co.elastic.clients.elasticsearch._types.ErrorResponse)1 Refresh (co.elastic.clients.elasticsearch._types.Refresh)1