Search in sources :

Example 1 with Property

use of org.opensearch.client.opensearch._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 org.opensearch.client.opensearch._types.mapping.Property in project opensearch-java by opensearch-project.

the class VariantsTest method testNestedTaggedUnionWithDefaultTag.

@Test
public void testNestedTaggedUnionWithDefaultTag() {
    // 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.
    // 
    // 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(org.opensearch.client.opensearch.indices.GetMappingResponse) TypeMapping(org.opensearch.client.opensearch._types.mapping.TypeMapping) Test(org.junit.Test)

Example 3 with Property

use of org.opensearch.client.opensearch._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 4 with Property

use of org.opensearch.client.opensearch._types.mapping.Property in project opensearch-java by opensearch-project.

the class BehaviorsTest method testAdditionalProperties.

@Test
public void testAdditionalProperties() {
    // Check that additional property map is initialized even if not set explicitly
    ErrorCause err = new ErrorCause.Builder().reason("Foo").type("Bar").build();
    assertEquals(0, err.metadata().size());
    err = new ErrorCause.Builder().reason("Some failure").type("Some type").metadata(MapBuilder.of("index", JsonData.of("test"), "retries", JsonData.of(1))).build();
    err = checkJsonRoundtrip(err, "{\"index\":\"test\",\"retries\":1,\"type\":\"Some type\",\"reason\":\"Some failure\"}");
    assertEquals("Some failure", err.reason());
    assertEquals(1, err.metadata().get("retries").to(int.class).intValue());
    assertEquals("test", err.metadata().get("index").to(String.class));
}
Also used : ErrorCause(org.opensearch.client.opensearch._types.ErrorCause) Test(org.junit.Test)

Example 5 with Property

use of org.opensearch.client.opensearch._types.mapping.Property in project opensearch-java by opensearch-project.

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 : Query(org.opensearch.client.opensearch._types.query_dsl.Query) ShapeQuery(org.opensearch.client.opensearch._types.query_dsl.ShapeQuery) TermQuery(org.opensearch.client.opensearch._types.query_dsl.TermQuery) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 Map (java.util.Map)3 IOException (java.io.IOException)2 Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 JsonData (org.opensearch.client.json.JsonData)2 ErrorCause (org.opensearch.client.opensearch._types.ErrorCause)2 Property (org.opensearch.client.opensearch._types.mapping.Property)2 TypeMapping (org.opensearch.client.opensearch._types.mapping.TypeMapping)2 Query (org.opensearch.client.opensearch._types.query_dsl.Query)2 SearchRequest (org.opensearch.client.opensearch.core.SearchRequest)2 GetMappingResponse (org.opensearch.client.opensearch.indices.GetMappingResponse)2 ElasticsearchClient (co.elastic.clients.elasticsearch.ElasticsearchClient)1 Property (co.elastic.clients.elasticsearch._types.mapping.Property)1 QueryBuilders (co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders)1 IndexRequest (co.elastic.clients.elasticsearch.core.IndexRequest)1 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)1 SourceConfig (co.elastic.clients.elasticsearch.core.search.SourceConfig)1 CreateIndexRequest (co.elastic.clients.elasticsearch.indices.CreateIndexRequest)1