自定义初始化ES实体类

/*
package com.dict.business.esinit;

import com.dict.business.domain.ESHomeSearch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Component;

@Component
@Order(value = 0)
@Slf4j
public class ESHomeSearchInitIndex implements CommandLineRunner {

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    */
/**
     * 项目启动的时候,如果elasticsearch已经存有索引,则不做任何操作
     * 如果没有索引,则新建索引
     *
     * @param args
     * @throws Exception
     *//*

    @Override
    public void run(String... args) throws Exception {
        boolean indexExists = elasticsearchRestTemplate.indexExists(ESHomeSearch.class);
        if (indexExists) {
            log.warn("存在索引");
        } else {
            log.warn("索引不存在。。。");
            try {
                boolean index = elasticsearchRestTemplate.createIndex(ESHomeSearch.class, EsData.DEFAULT_SETTING);
                boolean putMapping = elasticsearchRestTemplate.putMapping(ESHomeSearch.class);
                if (index && putMapping) {
                    log.info("索引创建成功。。。");
                } else {
                    log.warn("索引创建失败。。。");
                }
            } catch (Exception e) {
                log.error("error: {}", e.getLocalizedMessage());
            }
        }
    }
}
*/
View Code

ES  setting

package com.dict.business.esinit;

public class EsData {

    public static final String DEFAULT_SETTING = "{\n" +
            "        \"analysis\" : {\n" +
            "            \"analyzer\" : {\n" +
            "                \"tsconvert\" : {\n" +
            "                    \"tokenizer\" : \"tsconvert\"\n" +
            "                    }\n" +
            "            },\n" +
            "            \"tokenizer\" : {\n" +
            "                \"tsconvert\" : {\n" +
            "                    \"type\" : \"stconvert\",\n" +
            "                    \"delimiter\" : \"#\",\n" +
            "                    \"keep_both\" : false,\n" +
            "                    \"convert_type\" : \"t2s\"\n" +
            "                }\n" +
            "            },   \n" +
            "             \"filter\": {\n" +
            "               \"tsconvert\" : {\n" +
            "                     \"type\" : \"stconvert\",\n" +
            "                     \"delimiter\" : \"#\",\n" +
            "                     \"keep_both\" : false,\n" +
            "                     \"convert_type\" : \"t2s\"\n" +
            "                 }\n" +
            "             },\n" +
            "            \"char_filter\" : {\n" +
            "                \"tsconvert\" : {\n" +
            "                    \"type\" : \"stconvert\",\n" +
            "                    \"convert_type\" : \"t2s\"\n" +
            "                }\n" +
            "            }\n" +
            "        }\n" +
            "    }";

}
View Code

 

posted @ 2022-04-06 19:04  陆伟  阅读(67)  评论(0编辑  收藏  举报