|
@ -13,10 +13,13 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@ -24,23 +27,16 @@ import java.util.Map;
|
|
|
/**
|
|
|
* Created by janseny on 2018/9/17.
|
|
|
*/
|
|
|
@Component
|
|
|
public class ElasticSearchHandler {
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(ElasticSearchHandler.class);
|
|
|
|
|
|
public static final String fieldType = "type";
|
|
|
@Value("${elasticsearch.cluster-name}")
|
|
|
private String clusterName;
|
|
|
@Value("${elasticsearch.cluster-nodes}")
|
|
|
private String ip;
|
|
|
private int port;
|
|
|
|
|
|
public ElasticSearchHandler() {
|
|
|
}
|
|
|
|
|
|
public void setField(String clusterName, String ip, int port) {
|
|
|
this.clusterName = clusterName;
|
|
|
this.ip = ip;
|
|
|
this.port = port;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 取得实例
|
|
@ -53,7 +49,7 @@ public class ElasticSearchHandler {
|
|
|
/* .put("client.transport.sniff", true)*/
|
|
|
.put("client.transport.ping_timeout", "30s").build();
|
|
|
client = TransportClient.builder().settings(settings).build()
|
|
|
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), port));
|
|
|
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), 9300));
|
|
|
} catch (UnknownHostException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
@ -128,7 +124,7 @@ public class ElasticSearchHandler {
|
|
|
public void createIndexAndCreateMapping(String index, String type,List<FieldInfo> fieldInfoList,TransportClient client) {
|
|
|
XContentBuilder mapping = null;
|
|
|
try {
|
|
|
CreateIndexRequestBuilder cib=client.admin()
|
|
|
CreateIndexRequestBuilder cib = client.admin()
|
|
|
.indices().prepareCreate(index);
|
|
|
mapping = XContentFactory.jsonBuilder()
|
|
|
.startObject()
|
|
@ -145,14 +141,19 @@ public class ElasticSearchHandler {
|
|
|
if("string".equals(dataType)){
|
|
|
if(participle == 1) {
|
|
|
mapping.startObject(field)
|
|
|
.field("type","text")
|
|
|
.field("analyzer","ik_smart")
|
|
|
.field("type","string")
|
|
|
.field("index","not_analyzed")
|
|
|
.endObject();
|
|
|
}else if(participle == 2){
|
|
|
mapping.startObject(field)
|
|
|
.field("type","text")
|
|
|
.field("analyzer","ik_max_word")
|
|
|
.endObject();
|
|
|
}else if(participle == 3){
|
|
|
mapping.startObject(field)
|
|
|
.field("type","text")
|
|
|
.field("analyzer","ik_smart")
|
|
|
.endObject();
|
|
|
}else {
|
|
|
mapping.startObject(field)
|
|
|
.field("type","keyword")
|
|
@ -183,16 +184,17 @@ public class ElasticSearchHandler {
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 创建索引与mapping模板
|
|
|
* 根据模板自动创建索引与mapping
|
|
|
* @param index 索引字段
|
|
|
* @param type 类型
|
|
|
* @param client 客户端
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
public void createMapping(String index, String type,TransportClient client) throws IOException {
|
|
|
public void createMappingByTemplate(String index, String type,TransportClient client) throws IOException {
|
|
|
|
|
|
CreateIndexRequestBuilder cib=client.admin()
|
|
|
.indices().prepareCreate(index);
|
|
|
|
|
|
XContentBuilder mapping = XContentFactory.jsonBuilder()
|
|
|
.startObject()
|
|
|
.startObject("properties") //设置之定义字段
|