elasticsearch8 java 创建索引,mapping,修改mapping

2,754 阅读1分钟

上一篇小弟已经示范可以和es连上了。 今天试下创建索引以及修改mapping。

新增索引+mapping:

public static void createIndex() throws IOException {
    Map<String, Property> documentMap = new HashMap<>();
    documentMap.put("userName", Property.of(property ->
                    property.text(TextProperty.of(textProperty ->
                            //textProperty.index(true).analyzer("ik_max_word"))//这里设置分词
                            textProperty.index(true))
                    )
            )
    );
    documentMap.put("age", Property.of(property ->
                    property.integer(IntegerNumberProperty.of(integerNumberProperty
                            -> integerNumberProperty.index(true))
                    )
            )
    );

    documentMap.put("score", Property.of(property ->
                    property.integer(IntegerNumberProperty.of(integerNumberProperty
                            -> integerNumberProperty.index(true))
                    )
            )
    );

    Map<String, Property> addressMap = new HashMap<>();
    addressMap.put("province", Property.of(property ->
                    property.text(TextProperty.of(textProperty ->
                            //textProperty.index(true).analyzer("ik_max_word"))
                            textProperty.index(true))
                    )
            )
    );

    addressMap.put("city", Property.of(property ->
                    property.keyword(KeywordProperty.of(textProperty ->
                            textProperty.index(true)
                    )
            ))
    );

    documentMap.put("address", Property.of(property ->
                    property.nested(NestedProperty.of(na-> na.properties(addressMap)))
                    )
            );

    ClientClass client=null;

    try {
         client = getClient();
    } catch (KeyStoreException e) {
        throw new RuntimeException(e);
    } catch (CertificateException e) {
        throw new RuntimeException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }

    ElasticsearchClient elasticsearchClient = client.getElasticsearchClient();


    CreateIndexResponse createIndexResponse = elasticsearchClient.indices().create(createIndexBuilder ->
            createIndexBuilder.index("user").mappings(mappings ->
                            mappings.properties(documentMap))
                    .aliases("User",aliases ->
                            aliases.isWriteIndex(true))
    );
    Boolean acknowledged = createIndexResponse.acknowledged();
    System.out.println("acknowledged = " + acknowledged);

    client.close();
}

新增mapping字段: 注意:不能修改字段的属性,例如我想修改userName为textProperty.index(true).analyzer("ik_max_word")) 会报错:

failed: [illegal_argument_exception] Mapper for [userName] conflicts with existing mapper:

所以下面方法只能是新增字段或者减少字段:

public static void changeMapping() throws IOException {
        Map<String, Property> documentMap = new HashMap<>();
        //新加的属性
        documentMap.put("nickName", Property.of(property ->
                        property.text(TextProperty.of(textProperty ->
                                textProperty.index(true).analyzer("ik_max_word"))
                                //textProperty.index(true))
                        )
                )
        );
        documentMap.put("userName", Property.of(property ->
                        property.text(TextProperty.of(textProperty ->
                                //textProperty.index(true).analyzer("ik_max_word"))
                                textProperty.index(true))
                        )
                )
        );
        documentMap.put("age", Property.of(property ->
                        property.integer(IntegerNumberProperty.of(integerNumberProperty
                                -> integerNumberProperty.index(true))
                        )
                )
        );

        documentMap.put("score", Property.of(property ->
                        property.integer(IntegerNumberProperty.of(integerNumberProperty
                                -> integerNumberProperty.index(true))
                        )
                )
        );

        documentMap.put("id", Property.of(property ->
                        property.keyword(KeywordProperty.of(textProperty ->
                                        textProperty.index(true)
                                )
                        )
                )
        );

        Map<String, Property> addressMap = new HashMap<>();
        addressMap.put("province", Property.of(property ->
                        property.text(TextProperty.of(textProperty ->
                                //textProperty.index(true).analyzer("ik_max_word"))
                                textProperty.index(true))
                        )
                )
        );

        addressMap.put("city", Property.of(property ->
                property.keyword(KeywordProperty.of(textProperty ->
                                textProperty.index(true)
                        )
                ))
        );

        documentMap.put("address", Property.of(property ->
                        property.nested(NestedProperty.of(na-> na.properties(addressMap)))
                )
        );

        ClientClass client=null;

        try {
            client = getClient();
        } catch (KeyStoreException e) {
            throw new RuntimeException(e);
        } catch (CertificateException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (KeyManagementException e) {
            throw new RuntimeException(e);
        }

        ElasticsearchClient elasticsearchClient = client.getElasticsearchClient();


        PutMappingRequest putMappingRequest = PutMappingRequest.of(m -> m.index("user").properties(documentMap));

        PutMappingResponse putMappingResponse = elasticsearchClient.indices().putMapping(putMappingRequest);
//
//        CreateIndexResponse createIndexResponse = elasticsearchClient.indices().create(createIndexBuilder ->
//                createIndexBuilder.index("user").mappings(mappings ->
//                                mappings.properties(documentMap))
//                        .aliases("User",aliases ->
//                                aliases.isWriteIndex(true))
//        );
//        Boolean acknowledged = createIndexResponse.acknowledged();
//        System.out.println("acknowledged = " + acknowledged);

        boolean acknowledged = putMappingResponse.acknowledged();
        System.out.println("acknowledged = " + acknowledged);
        client.close();
    }

新加结果:

图片.png