/
EsIndexOperate.java
118 lines (95 loc) · 3.38 KB
/
EsIndexOperate.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.github.bjlhx15.servicees;
import com.alibaba.fastjson.JSON;
import com.github.bjlhx15.eshelper.EsBase543Utils;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author lihongxu
* @since 2018/11/20 下午5:56
*/
//@RunWith(SpringRunner.class)
//@SpringBootTest(classes = MainServiceApplication.class)
//@WebAppConfiguration
public class EsIndexOperate {
private EsBase543Utils esBaseUtil;
@Before
public void init() throws UnknownHostException {
System.out.println("--------init--------");
Settings settings = Settings.builder()
.put("cluster.name", "jiesi-5.4")
.build();
// TransportClient client = new PreBuiltTransportClient(settings)//Settings.EMPTY
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.11"), 20101))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.12"), 20101))
// .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.182.13"), 20101));
TransportClient client = new PreBuiltTransportClient(settings)//Settings.EMPTY
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
esBaseUtil = new EsBase543Utils(client);
// esBaseUtil = new EsBaseUtil();
// esBaseUtil.createClient();
}
class Data {
String name;
String address;
String age;
long num;
public Data(String name, String address, String age, long num) {
this.name = name;
this.address = address;
this.age = age;
this.num = num;
}
public long getNum() {
return num;
}
public void setNum(long num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
//添加数据同时 添加一个索引,在插入的时候 有新的字段 同时会自动更新
@Test
public void esWriteOne() {
Data a = new Data("张四3", "benjing3", "33", 12L);
String jsonString = JSON.toJSONString(a);
esBaseUtil.addDocument("bt_middle_data_test4", "bt_middle_data","form", jsonString);
}
@Test
public void addAliasIndexName() {
esBaseUtil.addAliasIndex("bt_middle_data_test1", "bt_middle_data");
}
//删除index
@Test
public void esDelete() {
esBaseUtil.deleteIndex("bt_middle_data_test");
}
@After
public void finish() {
System.out.println("--------finish--------");
esBaseUtil.getClient().close();
}
}