1、pom.xml文件
org.springframework.boot
spring-boot-starter-test
org.springframework.cloud
spring-cloud-starter-eureka
org.elasticsearch
elasticsearch
6.2.3
org.elasticsearch.client
transport
6.2.3
org.elasticsearch
elasticsearch
2、es配置(案例使用SpringCloud)
es.cluster-name=es
es.ip=127.0.0.1
es.port=9300
es.pool=5
3、es初始化
@Configuration
public class EsConfig {
@Autowired
private Environment env;
@SuppressWarnings("resource")
@Bean
public TransportClient transportClient() throws UnknownHostException{
Settings settings = Settings.builder()
.put("cluster.name", env.getProperty("es.cluster-name"))
.put("client.transport.sniff", true)
.put("thread_pool.search.size", Integer.valueOf(env.getProperty("es.pool")))
.build();
TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(env.getProperty("es.ip")), Integer.valueOf(env.getProperty("es.port")));
TransportClient esClient = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
return esClient;
}
}
4、es索引Controller
@RestController
public class EsController {
@Autowired
EsService esService;
/**
* 初始化es索引
*
* @param userCode
* @return
*/
@RequestMapping(value = "/es/initIndex", method = { RequestMethod.GET, RequestMethod.POST })
public void initIndex() {
esService.initIndex();
}}
5、es索引接口
public interface EsService {
/**
* es索引
* @return
*/
void initIndex();
}
6、es索引接口实现
@Service
public class EsServiceImpl implements EsService {
Log log = LogFactory.getLog(EsServiceImpl.class);
@Autowired
TransportClient client;
public void initIndex(){
try {
XContentBuilder builder = XContentFactory
.jsonBuilder()
.startObject()
.startObject("test")
.startObject("properties")
.startObject("id")
.field("type", "integer")
.endObject()
.startObject("um")
.field("type", "keyword")
.endObject()
.startObject("question")
.field("type", "text")
.field("analyzer", "ik")
.field("search_analyzer","ik")
.endObject()
.startObject("questionType")
.field("type", "integer")
.endObject()
.startObject("nlp")
.field("type", "keyword")
.endObject()
.startObject("isHit")
.field("type", "integer")
.endObject()
.startObject("isSatisfied")
.field("type", "integer")
.endObject()
.startObject("opinion")
.field("type", "keyword")
.endObject()
.startObject("label")
.field("type", "keyword")
.endObject()
.startObject("askTimes")
.field("type", "integer")
.endObject()
.startObject("createdBy")
.field("type", "keyword")
.endObject()
.startObject("updatedBy")
.field("type", "keyword")
.endObject()
.startObject("createdDate")
.field("type", "date")
//.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.startObject("updatedDate")
.field("type", "date")
//.field("format", "yyyy-MM-dd HH:mm:ss")
.endObject()
.endObject()
.endObject()
.endObject();
//副本、分片
//定义中文分词+停词
String settingsJson = "{"
+ "\"number_of_replicas\": 1, "
+ "\"number_of_shards\": 5, "
+ "\"analysis\": { "
+ "\"analyzer\": { "
+ "\"ik\": { "
+ "\"tokenizer\": \"ik_max_word\", "
+ "\"type\": \"standard\", \"stopwords\": [\"也\",\"了\",\"仍\",\"从\",\"以\",\"使\",\"则\",\"却\",\"又\",\"及\",\"对\",\"就\",\"并\",\"很\",\"或\",\"把\",\"是\",\"的\",\"着\",\"给\",\"而\",\"被\",\"让\",\"在\",\"还\",\"比\",\"等\",\"当\",\"与\",\"于\",\"但\"] "
+ "} "
+ "} "
+ "} "
+ "}";
//Builder settings = Settings.builder()
//.put("number_of_replicas","1")
//.put("number_of_shards","5");
String mappingStr = builder.string();
//判断索引是否存在
client.admin().indices().prepareCreate("test_index").setSettings(settingsJson, XContentType.JSON).execute().actionGet();
client.admin().indices().preparePutMapping("test_index").setType("test").setSource(mappingStr,XContentType.JSON).execute().actionGet();
} catch (Exception e) {
log.error(e.getMessage(), e);
e.printStackTrace();
}
}
}