shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.walker.support.es.config;
 
import com.walker.support.es.Constants;
import com.walker.support.es.SearchService;
import com.walker.support.es.impl.FullTextEmptyService;
import com.walker.support.es.impl.FullTextSearchService;
import com.walker.support.es.util.FullTextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class ElasticSearchConfig {
 
    protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());
 
    @Bean
    public ElasticSearchProperties elasticSearchProperties(){
        return new ElasticSearchProperties();
    }
 
    @Bean
    public SearchService fullTextSearchService(ElasticSearchProperties properties){
        if(!properties.isEnabled()){
            logger.warn("全文检索未启用,创建空服务");
            return new FullTextEmptyService();
        }
 
        FullTextSearchService service = new FullTextSearchService();
        try {
            service.setNodes(properties.getClusterNodes());
            service.afterPropertiesSet();
            logger.info("创建 FullTextSearchService 成功");
 
            // 启动时初始化es的pipeline和索引表结构
            // 1-pipeline
            service.autoCreatePipelineAttachment("attachment");
 
            // 2-表结构
            String tokenType = Constants.TOKEN_TYPE_SMART;
            boolean xm = service.createIndex(Constants.INDEX_NAME_DEFAULT, null, FullTextUtils.createIndexJson(null, tokenType));
            boolean file = service.createIndex(Constants.INDEX_NAME_FILE, null, FullTextUtils.createXmFileJson(null, tokenType));
            boolean recent = service.createIndex(Constants.INDEX_NAME_RECENT, null
                    , FullTextUtils.createSimpleIndex(tokenType, false));
            boolean simple = service.createIndex(Constants.INDEX_NAME_SIMPLE, null
                    , FullTextUtils.createSimpleIndex(tokenType, true));
 
        } catch (Exception e) {
            throw new RuntimeException("初始化全文检索服务失败(SearchService):" + e.getMessage(), e);
        }
        return service;
    }
}