1
1
package io .github .dunwu .javadb .elasticsearch .springboot ;
2
2
3
+ import cn .hutool .core .bean .BeanUtil ;
4
+ import cn .hutool .core .bean .copier .CopyOptions ;
5
+ import cn .hutool .json .JSONUtil ;
6
+ import io .github .dunwu .javadb .elasticsearch .springboot .entities .Product ;
3
7
import io .github .dunwu .javadb .elasticsearch .springboot .entities .User ;
8
+ import org .elasticsearch .action .ActionListener ;
4
9
import org .elasticsearch .action .admin .indices .alias .Alias ;
5
- import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
6
10
import org .elasticsearch .action .admin .indices .delete .DeleteIndexRequest ;
11
+ import org .elasticsearch .action .delete .DeleteRequest ;
12
+ import org .elasticsearch .action .delete .DeleteResponse ;
13
+ import org .elasticsearch .action .get .GetRequest ;
14
+ import org .elasticsearch .action .get .GetResponse ;
7
15
import org .elasticsearch .action .index .IndexRequest ;
8
16
import org .elasticsearch .action .index .IndexResponse ;
9
17
import org .elasticsearch .action .support .master .AcknowledgedResponse ;
10
- import org .elasticsearch .client .GetAliasesResponse ;
18
+ import org .elasticsearch .action .update .UpdateRequest ;
19
+ import org .elasticsearch .action .update .UpdateResponse ;
11
20
import org .elasticsearch .client .RequestOptions ;
12
21
import org .elasticsearch .client .RestHighLevelClient ;
13
22
import org .elasticsearch .client .indices .CreateIndexRequest ;
14
23
import org .elasticsearch .client .indices .GetIndexRequest ;
15
- import org .elasticsearch .cluster .metadata .AliasMetadata ;
16
24
import org .elasticsearch .common .settings .Settings ;
17
25
import org .elasticsearch .xcontent .XContentType ;
18
26
import org .junit .jupiter .api .*;
19
27
import org .springframework .beans .factory .annotation .Autowired ;
20
28
import org .springframework .boot .test .context .SpringBootTest ;
21
29
22
30
import java .io .IOException ;
23
- import java .util .Map ;
24
- import java .util .Set ;
25
31
26
32
/**
27
33
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
30
36
@ SpringBootTest
31
37
public class RestHighLevelClientDocumentApiTest {
32
38
39
+
33
40
public static final String INDEX = "mytest" ;
34
41
public static final String INDEX_ALIAS = "mytest_alias" ;
35
42
/**
36
43
* {@link User} 的 mapping 结构(json形式)
37
44
*/
38
- public static final String MAPPING_JSON = "{\n "
39
- + " \" properties\" : {\n "
40
- + " \" age\" : {\n "
41
- + " \" type\" : \" long\" \n "
42
- + " },\n "
43
- + " \" desc\" : {\n "
44
- + " \" type\" : \" text\" ,\n "
45
- + " \" fields\" : {\n "
46
- + " \" keyword\" : {\n "
47
- + " \" type\" : \" keyword\" ,\n "
48
- + " \" ignore_above\" : 256\n "
49
- + " }\n "
50
- + " }\n "
51
- + " },\n "
52
- + " \" email\" : {\n "
53
- + " \" type\" : \" text\" ,\n "
54
- + " \" fielddata\" : true\n "
55
- + " },\n "
56
- + " \" id\" : {\n "
57
- + " \" type\" : \" text\" ,\n "
58
- + " \" fields\" : {\n "
59
- + " \" keyword\" : {\n "
60
- + " \" type\" : \" keyword\" ,\n "
61
- + " \" ignore_above\" : 256\n "
62
- + " }\n "
63
- + " }\n "
64
- + " },\n "
65
- + " \" password\" : {\n "
66
- + " \" type\" : \" text\" ,\n "
67
- + " \" fields\" : {\n "
68
- + " \" keyword\" : {\n "
69
- + " \" type\" : \" keyword\" ,\n "
70
- + " \" ignore_above\" : 256\n "
71
- + " }\n "
72
- + " }\n "
73
- + " },\n "
74
- + " \" title\" : {\n "
75
- + " \" type\" : \" text\" ,\n "
76
- + " \" fields\" : {\n "
77
- + " \" keyword\" : {\n "
78
- + " \" type\" : \" keyword\" ,\n "
79
- + " \" ignore_above\" : 256\n "
80
- + " }\n "
81
- + " }\n "
82
- + " },\n "
83
- + " \" user\" : {\n "
84
- + " \" type\" : \" text\" ,\n "
85
- + " \" fields\" : {\n "
86
- + " \" keyword\" : {\n "
87
- + " \" type\" : \" keyword\" ,\n "
88
- + " \" ignore_above\" : 256\n "
89
- + " }\n "
90
- + " }\n "
91
- + " },\n "
92
- + " \" username\" : {\n "
93
- + " \" type\" : \" text\" ,\n "
94
- + " \" fields\" : {\n "
95
- + " \" keyword\" : {\n "
96
- + " \" type\" : \" keyword\" ,\n "
97
- + " \" ignore_above\" : 256\n "
98
- + " }\n "
99
- + " }\n "
100
- + " }\n "
101
- + " }\n "
102
- + "}" ;
45
+ public static final String MAPPING_JSON =
46
+ "{\n " + " \" properties\" : {\n " + " \" _class\" : {\n " + " \" type\" : \" keyword\" ,\n "
47
+ + " \" index\" : false,\n " + " \" doc_values\" : false\n " + " },\n " + " \" description\" : {\n "
48
+ + " \" type\" : \" text\" ,\n " + " \" fielddata\" : true\n " + " },\n " + " \" enabled\" : {\n "
49
+ + " \" type\" : \" boolean\" \n " + " },\n " + " \" name\" : {\n " + " \" type\" : \" text\" ,\n "
50
+ + " \" fielddata\" : true\n " + " }\n " + " }\n " + "}" ;
103
51
104
52
@ Autowired
105
53
private RestHighLevelClient client ;
@@ -111,19 +59,16 @@ public void init() throws IOException {
111
59
CreateIndexRequest createIndexRequest = new CreateIndexRequest (INDEX );
112
60
113
61
// 设置索引的 settings
114
- createIndexRequest .settings (Settings .builder ()
115
- .put ("index.number_of_shards" , 3 )
116
- .put ("index.number_of_replicas" , 2 )
117
- );
62
+ createIndexRequest .settings (
63
+ Settings .builder ().put ("index.number_of_shards" , 3 ).put ("index.number_of_replicas" , 2 ));
118
64
119
65
// 设置索引的 mapping
120
66
createIndexRequest .mapping (MAPPING_JSON , XContentType .JSON );
121
67
122
68
// 设置索引的别名
123
69
createIndexRequest .alias (new Alias (INDEX_ALIAS ));
124
70
125
- AcknowledgedResponse response =
126
- client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
71
+ AcknowledgedResponse response = client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
127
72
Assertions .assertTrue (response .isAcknowledged ());
128
73
129
74
// 判断索引是否存在
@@ -142,29 +87,142 @@ public void destroy() throws IOException {
142
87
}
143
88
144
89
@ Test
145
- @ DisplayName ("列出所有索引" )
146
- public void listAllIndex () throws IOException {
147
- GetAliasesRequest request = new GetAliasesRequest ();
148
- GetAliasesResponse getAliasesResponse = client .indices ().getAlias (request , RequestOptions .DEFAULT );
149
- Map <String , Set <AliasMetadata >> map = getAliasesResponse .getAliases ();
150
- Set <String > indices = map .keySet ();
151
- indices .forEach (System .out ::println );
90
+ @ DisplayName ("同步新建文档" )
91
+ public void index () throws IOException {
92
+ IndexRequest request = new IndexRequest (INDEX_ALIAS );
93
+ request .id ("1" );
94
+ Product product = new Product ();
95
+ product .setName ("机器人" );
96
+ product .setDescription ("人工智能机器人" );
97
+ product .setEnabled (true );
98
+ String jsonString = JSONUtil .toJsonStr (product );
99
+ request .source (jsonString , XContentType .JSON );
100
+
101
+ // 同步执行
102
+ IndexResponse response = client .index (request , RequestOptions .DEFAULT );
103
+ System .out .println (response );
104
+ }
105
+
106
+ @ Test
107
+ @ DisplayName ("异步新建文档" )
108
+ public void indexAsync () {
109
+ IndexRequest request = new IndexRequest (INDEX_ALIAS );
110
+ Product product = new Product ();
111
+ product .setName ("机器人" );
112
+ product .setDescription ("人工智能机器人" );
113
+ product .setEnabled (true );
114
+ String jsonString = JSONUtil .toJsonStr (product );
115
+ request .source (jsonString , XContentType .JSON );
116
+
117
+ // 异步执行
118
+ client .indexAsync (request , RequestOptions .DEFAULT , new ActionListener <IndexResponse >() {
119
+ @ Override
120
+ public void onResponse (IndexResponse indexResponse ) {
121
+ System .out .println (indexResponse );
122
+ }
123
+
124
+ @ Override
125
+ public void onFailure (Exception e ) {
126
+ System .out .println ("执行失败" );
127
+ }
128
+ });
129
+ }
130
+
131
+ @ Test
132
+ @ DisplayName ("删除文档" )
133
+ public void delete () throws IOException {
134
+
135
+ // 创建文档请求
136
+ IndexRequest request = new IndexRequest (INDEX_ALIAS );
137
+ request .id ("1" );
138
+ Product product = new Product ();
139
+ product .setName ("机器人" );
140
+ product .setDescription ("人工智能机器人" );
141
+ product .setEnabled (true );
142
+ String jsonString = JSONUtil .toJsonStr (product );
143
+ request .source (jsonString , XContentType .JSON );
144
+
145
+ // 同步执行创建操作
146
+ IndexResponse response = client .index (request , RequestOptions .DEFAULT );
147
+ System .out .println (response );
148
+
149
+ // 删除文档请求
150
+ DeleteRequest deleteRequest = new DeleteRequest (INDEX_ALIAS , "1" );
151
+
152
+ // 同步执行删除操作
153
+ // DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
154
+ // System.out.println(deleteResponse);
155
+
156
+ // 异步执行删除操作
157
+ client .deleteAsync (deleteRequest , RequestOptions .DEFAULT , new ActionListener <DeleteResponse >() {
158
+ @ Override
159
+ public void onResponse (DeleteResponse deleteResponse ) {
160
+ System .out .println (deleteResponse );
161
+ }
162
+
163
+ @ Override
164
+ public void onFailure (Exception e ) {
165
+ System .out .println ("执行失败" );
166
+ }
167
+ });
152
168
}
153
169
154
170
@ Test
155
- public void method () throws IOException {
156
- IndexRequest request = new IndexRequest (INDEX );
171
+ @ DisplayName ("更新文档" )
172
+ public void update () throws IOException {
173
+
174
+ // 创建文档请求
175
+ IndexRequest request = new IndexRequest (INDEX_ALIAS );
157
176
request .id ("1" );
158
- String jsonString = "{\n "
159
- + " \" id\" : \" 1\" ,\n "
160
- + " \" userName\" : \" Jack\" ,\n "
161
- + " \" age\" : 12,\n "
162
- + " \" password\" : \" 123456\" ,\n "
163
- + " \" email\" : \" jack@xxx.com\" \n "
164
- + "}" ;
177
+ Product product = new Product ();
178
+ product .setName ("机器人" );
179
+ product .setDescription ("人工智能机器人" );
180
+ product .setEnabled (true );
181
+ String jsonString = JSONUtil .toJsonStr (product );
165
182
request .source (jsonString , XContentType .JSON );
166
- IndexResponse indexResponse = client .index (request , RequestOptions .DEFAULT );
167
- System .out .println ("indexResponse: " + indexResponse .getResult ());
183
+
184
+ // 同步执行创建操作
185
+ IndexResponse response = client .index (request , RequestOptions .DEFAULT );
186
+ System .out .println (response );
187
+
188
+ // 查询文档操作
189
+ GetRequest getRequest = new GetRequest (INDEX_ALIAS , "1" );
190
+ GetResponse getResponse = client .get (getRequest , RequestOptions .DEFAULT );
191
+ Product product2 = BeanUtil .mapToBean (getResponse .getSource (), Product .class , true , CopyOptions .create ());
192
+ System .out .println ("product2: " + product2 );
193
+ Assertions .assertEquals (product .getName (), product2 .getName ());
194
+
195
+ // 更新文档请求
196
+ UpdateRequest updateRequest = new UpdateRequest (INDEX_ALIAS , "1" );
197
+ Product product3 = new Product ();
198
+ product3 .setName ("扫地机器人" );
199
+ product3 .setDescription ("人工智能扫地机器人" );
200
+ product3 .setEnabled (true );
201
+ String jsonString2 = JSONUtil .toJsonStr (product3 );
202
+ updateRequest .doc (jsonString2 , XContentType .JSON );
203
+
204
+ // 同步执行更新操作
205
+ UpdateResponse updateResponse = client .update (updateRequest , RequestOptions .DEFAULT );
206
+ System .out .println (updateResponse );
207
+
208
+ // 异步执行更新操作
209
+ // client.updateAsync(updateRequest, RequestOptions.DEFAULT, new ActionListener<UpdateResponse>() {
210
+ // @Override
211
+ // public void onResponse(UpdateResponse updateResponse) {
212
+ // System.out.println(updateResponse);
213
+ // }
214
+ //
215
+ // @Override
216
+ // public void onFailure(Exception e) {
217
+ // System.out.println("执行失败");
218
+ // }
219
+ // });
220
+
221
+ // 查询文档操作
222
+ GetResponse getResponse2 = client .get (getRequest , RequestOptions .DEFAULT );
223
+ Product product4 = BeanUtil .mapToBean (getResponse2 .getSource (), Product .class , true , CopyOptions .create ());
224
+ System .out .println ("product4: " + product4 );
225
+ Assertions .assertEquals (product3 .getName (), product4 .getName ());
168
226
}
169
227
170
228
}
0 commit comments