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;
|
}
|
}
|