11package io .github .dunwu .javadb .elasticsearch .springboot ;
22
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 ;
37import io .github .dunwu .javadb .elasticsearch .springboot .entities .User ;
8+ import org .elasticsearch .action .ActionListener ;
49import org .elasticsearch .action .admin .indices .alias .Alias ;
5- import org .elasticsearch .action .admin .indices .alias .get .GetAliasesRequest ;
610import 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 ;
715import org .elasticsearch .action .index .IndexRequest ;
816import org .elasticsearch .action .index .IndexResponse ;
917import 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 ;
1120import org .elasticsearch .client .RequestOptions ;
1221import org .elasticsearch .client .RestHighLevelClient ;
1322import org .elasticsearch .client .indices .CreateIndexRequest ;
1423import org .elasticsearch .client .indices .GetIndexRequest ;
15- import org .elasticsearch .cluster .metadata .AliasMetadata ;
1624import org .elasticsearch .common .settings .Settings ;
1725import org .elasticsearch .xcontent .XContentType ;
1826import org .junit .jupiter .api .*;
1927import org .springframework .beans .factory .annotation .Autowired ;
2028import org .springframework .boot .test .context .SpringBootTest ;
2129
2230import java .io .IOException ;
23- import java .util .Map ;
24- import java .util .Set ;
2531
2632/**
2733 * @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
3036@ SpringBootTest
3137public class RestHighLevelClientDocumentApiTest {
3238
39+
3340 public static final String INDEX = "mytest" ;
3441 public static final String INDEX_ALIAS = "mytest_alias" ;
3542 /**
3643 * {@link User} 的 mapping 结构(json形式)
3744 */
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 " + "}" ;
10351
10452 @ Autowired
10553 private RestHighLevelClient client ;
@@ -111,19 +59,16 @@ public void init() throws IOException {
11159 CreateIndexRequest createIndexRequest = new CreateIndexRequest (INDEX );
11260
11361 // 设置索引的 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 ));
11864
11965 // 设置索引的 mapping
12066 createIndexRequest .mapping (MAPPING_JSON , XContentType .JSON );
12167
12268 // 设置索引的别名
12369 createIndexRequest .alias (new Alias (INDEX_ALIAS ));
12470
125- AcknowledgedResponse response =
126- client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
71+ AcknowledgedResponse response = client .indices ().create (createIndexRequest , RequestOptions .DEFAULT );
12772 Assertions .assertTrue (response .isAcknowledged ());
12873
12974 // 判断索引是否存在
@@ -142,29 +87,142 @@ public void destroy() throws IOException {
14287 }
14388
14489 @ 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+ });
152168 }
153169
154170 @ 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 );
157176 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 );
165182 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 ());
168226 }
169227
170228}
0 commit comments