Elasticsearch 文档 Doc 基本操作

  1. 创建文档
  • 单个创建
  • 批量创建 - BulkOperation
  1. 查看文档
  • 查看指定文档
  • 查看索引下所有文档
  1. 修改文档
  • 覆盖式修改
  • 部分修改 - map
  1. 删除文档
  • 指定删除
    • 批量删除 - BulkOperation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package com.wzh.elasticsearch.doc;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.bulk.BulkResponseItem;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import com.wzh.elasticsearch.model.User;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @author 75654
* @date 2023/6/5 15:48
* <p>
* 文档基本操作 - doc:增、删、改、查
*/
public class DocTest {

/**
* 正常获取
*
* @throws IOException
*/
@Test
public void searchDoc() throws IOException {
// 客户端
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);

// 查询操作
GetRequest.Builder builder = new GetRequest.Builder();
builder.id("101");
builder.index("user");
GetResponse<User> getResponse = esClient.get(builder.build(), User.class);

// 结果
System.out.println("found==" + getResponse.found());
System.out.println("id==" + getResponse.id());
System.out.println("index==" + getResponse.index());
System.out.println("primaryTerm==" + getResponse.primaryTerm());
System.out.println("seqNo==" + getResponse.seqNo());
System.out.println("source==" + getResponse.source());
System.out.println("version==" + getResponse.version());
System.out.println("toString==" + getResponse.toString());

restClient.close();
}

/**
* 采用 lambda
*
* @throws IOException
*/
@Test
public void searchDoc2() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 采用 lambda
GetResponse<User> getResponse = esClient.get(s -> s.index("user").id("101"), User.class);
System.out.println(getResponse.toString());
}

/**
* 批量查询
* @throws IOException
*/
@Test
public void searchAllDoc() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 查询
int size = 10;
SearchResponse<User> searchResponse = esClient.search(s -> s.index("user").query(
q -> q.matchAll(
a -> a.queryName("search01")
)
).size(size), User.class);
System.out.println(searchResponse.toString());
List<Hit<User>> hits = searchResponse.hits().hits();
for (Hit<User> hit : hits) {
System.out.println(hit);
}

restClient.close();
}

/**
* 新增文档
*
* @throws IOException
*/
@Test
public void addDoc() throws IOException {
// 获取客户端
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper()));

// 文档内容
User user = new User();
user.setUid("101");
user.setUname("张三");
user.setAge(23);
user.setGender("男");

// 创建文档
IndexRequest.Builder<User> userBuilder = new IndexRequest.Builder<>();
userBuilder.index("user");
userBuilder.id(user.getUid());
userBuilder.document(user);

// 获取结果
IndexResponse indexResponse = esClient.index(userBuilder.build());
System.out.println("result==" + indexResponse.result());
System.out.println("version==" + indexResponse.version());
System.out.println("id==" + indexResponse.id());
System.out.println("index==" + indexResponse.index());
System.out.println("seqNo==" + indexResponse.seqNo());
System.out.println("type==" + indexResponse.type());

restClient.close();
}

/**
* 修改文档信息 - 覆盖
*
* @throws IOException
*/
@Test
public void updateDoc() throws IOException {
// 客户端
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper()));
// 文档
UpdateRequest.Builder<Object, Object> builder = new UpdateRequest.Builder<>();
builder.id("101");
builder.index("user");

// 修改内容
User user = new User();
user.setUid("101");
user.setUname("lisi");
user.setAge(24);
user.setGender("女");
builder.doc(user);

// 执行
UpdateResponse<Object> updateResponse = esClient.update(builder.build(), User.class);
System.out.println(updateResponse.toString());

restClient.close();
}

/**
* 通过传递 map 集合,实现局部修改
*
* @throws IOException
*/
@Test
public void updateDoc2() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 通过 map 传递需要修改的内容
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("uname", "wangwu");

// 修改
UpdateResponse<User> updateResponse = esClient.update(
s -> s.index("user").id("101").doc(hashMap), User.class);
System.out.println(updateResponse.toString());
}

/**
* 删除文档
*
* @throws IOException
*/
@Test
public void deleteDoc() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 删除文档
DeleteRequest.Builder builder = new DeleteRequest.Builder();
builder.index("user");
builder.id("101");
// 方式2: DeleteResponse deleteResponse1 = esClient.delete(s -> s.index("user").id("101"));
// 结果
DeleteResponse deleteResponse = esClient.delete(builder.build());
System.out.println(deleteResponse.toString());
}

/**
* 批量插入文档 - 使用 BulkOperation
*
* @throws IOException
*/
@Test
public void bulkAddDoc() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 构建文档数据
ArrayList<User> users = new ArrayList<>();
users.add(new User("1001", "张三", 23, "1999-11-11"));
users.add(new User("1002", "李四", 24, "1999-11-11"));
users.add(new User("1003", "王五", 25, "1999-11-11"));

// 通过 bulkRequest 实现批量插入
ArrayList<BulkOperation> bulkOperations = new ArrayList<>();
for (User user : users) {
// TODO BulkOperation.of() 什么意思???
bulkOperations.add(BulkOperation.of(
s -> s.index(
// 存放数据
v -> v.id(user.getUid()).document(user)
)
));
}
BulkRequest.Builder builder = new BulkRequest.Builder();
builder.index("user");
builder.operations(bulkOperations);
// 结果
BulkResponse bulkResponse = esClient.bulk(builder.build());
// 方法2: BulkResponse bulkResponse = esClient.bulk(s -> s.index("user").operations(bulkOperations));
List<BulkResponseItem> items = bulkResponse.items();
for (BulkResponseItem item : items) {
// 展示各个数据结果
System.out.println(item.result());
}
// 错误
System.out.println(bulkResponse.errors());

restClient.close();
}

/**
* 批量删除 - BulkOperation
* @throws IOException
*/
@Test
public void bulkDeleteDoc() throws IOException {
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200)).build();
ElasticsearchClient esClient = new ElasticsearchClient(
new RestClientTransport(restClient, new JacksonJsonpMapper())
);
// 使用 BulkOperation ids 要删除文档 id 集合
List<String> ids = new ArrayList<>();
ids.add("1002");
ids.add("1003");

List<BulkOperation> bulkOperations = new ArrayList<>();
for (String id : ids) {
bulkOperations.add(BulkOperation.of(
s -> s.delete(
v -> v.id(id)
)
));
}
// 结果
BulkRequest.Builder br = new BulkRequest.Builder();
br.index("user");
br.operations(bulkOperations);
BulkResponse bulkResponse = esClient.bulk(br.build());

List<BulkResponseItem> items = bulkResponse.items();
items.forEach(s -> System.out.println(s.result()));

restClient.close();
}
}