springboot-elasticsearch简单使用

pom.xml

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
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.3.7.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<artifactId>HdrHistogram</artifactId>
<groupId>org.hdrhistogram</groupId>
</exclusion>
<exclusion>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.6.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>

单元测试

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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package cn.idea360.httpclient;

import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.SearchScrollHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.*;

import java.io.Serializable;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.data.elasticsearch.core.query.UpdateResponse.Result.UPDATED;

/**
* @author cuishiying
* @date 2022-10-20
*/
public class EsTest {

private static ElasticsearchRestTemplate elasticsearchRestTemplate;

private static IndexCoordinates index;

public static class User implements Serializable {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@BeforeAll
static void init() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("127.0.0.1:9200".split(","))
.withConnectTimeout(Duration.ofSeconds(5)).withSocketTimeout(Duration.ofSeconds(90)).build();
RestHighLevelClient elasticsearchClient = RestClients.create(clientConfiguration).rest();
elasticsearchRestTemplate = new ElasticsearchRestTemplate(elasticsearchClient);
index = IndexCoordinates.of("es_test");
}

/**
* 创建索引
*/
@Test
void createIndex() {
boolean b = elasticsearchRestTemplate.indexOps(index).create();
assertEquals(b, elasticsearchRestTemplate.indexOps(index).exists());
}

/**
* 检查索引是否存在
*/
@Test
void indexExists() {
assertTrue(elasticsearchRestTemplate.indexOps(index).exists());
}

/**
* 删除索引
*/
@Test
void deleteIndex() {
assertTrue(elasticsearchRestTemplate.indexOps(index).delete());
}

/**
* 插入文档
*/
@Test
void insertDocument() {
for (int i = 0; i < 2; i++) {
User user = new User();
user.setName("es-name-" + i);
User save = elasticsearchRestTemplate.save(user, index);
assertEquals(save.getName(), user.getName());
}
}

/**
* 批量插入文档
*/
@Test
void batchInsertDocument() {
List<IndexQuery> indexQueryList = new ArrayList<>();
for (int i = 0; i < 2; i++) {
User user = new User();
user.setName("es-name-" + (i + 2));
indexQueryList.add(new IndexQueryBuilder()
.withId(UUID.randomUUID().toString().replace("-", ""))
.withObject(user)
.build());
}
List<String> bulkIndex = elasticsearchRestTemplate.bulkIndex(indexQueryList, index);
assertEquals(2, bulkIndex.size());
}

/**
* 删除文档
*/
@Test
void deleteDocument() {
String id = "c5598601e6e34b4eae0b4b36aa0ed3e0";
String delete = elasticsearchRestTemplate.delete(id, index);
assertEquals(delete, id);
}

/**
* 批量删除文档
*/
@Test
void batchDeleteDocument() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.termsQuery("_id", Arrays.asList("9a6c0ae827e84e17acd5130607cc6a19",
"46c5a486aa824db2983d130bcc80808c",
"e1ce00e363cb42a58425f53c6bb29d05",
"edf55009172047adbbbe49b977232806",
"6bc42bcae63d4900ae3e4dd3f458e37e",
"f563f8b59b6b4a07b3bc5931cbecb106",
"e37cfcf475834257bd039cee14d570b3")))
.build();
elasticsearchRestTemplate.delete(nativeSearchQuery, User.class, index);
}

/**
* 更新文档
*/
@Test
void updateDocument() {
Document document = Document.create();
document.put("name", "es-name-3-update");
UpdateResponse response = elasticsearchRestTemplate.update(UpdateQuery.builder("d2c1a0efa64b416da052b5f62cc101b4").withDocument(document).build(), index);
assertEquals(UPDATED, response.getResult());
}

/**
* 根据id获取文档
*/
@Test
void getDocument() {
String id = "d2c1a0efa64b416da052b5f62cc101b4";
User document = elasticsearchRestTemplate.get(id, User.class, index);
assertNotNull(document);
assertEquals("es-name-3-update", document.getName());
}

/**
* field[name] like es
*/
@Test
void queryDocument1() {
Query query = new NativeSearchQueryBuilder()
.withQuery(matchQuery("name", "es").operator(Operator.AND)).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(4, documents.size());
}

/**
* field[_id] like es||field[name] like es
*/
@Test
void queryDocument2() {
Query query = new NativeSearchQueryBuilder()
.withQuery(multiMatchQuery("es", "name", "_id")).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(4, documents.size());
}

/**
* 某字段按字符串模糊查询(eg: 中国人=[中国, 人, 中国人]), must表示同时满足:
* (field[name] like es||name||0||es-name||name-0||es-name-0) && (field[name] like update)
*/
@Test
void queryDocument3() {
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
final List<QueryBuilder> must = queryBuilder.must();
must.add(QueryBuilders.matchQuery("name", "es-name-0"));
must.add(QueryBuilders.matchQuery("name", "update"));
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(1, documents.size());
}

/**
* 短语匹配搜索
* (field[name] like es-name-0) && (field[name] like update)
*/
@Test
void queryDocument4() {
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
final List<QueryBuilder> must = queryBuilder.must();
must.add(QueryBuilders.matchPhraseQuery("name", "es-name-0"));
must.add(QueryBuilders.matchPhraseQuery("name", "update"));
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(0, documents.size());
}

/**
* should表示或
* (field[name] like es||name||0||es-name||name-0||es-name-0) || (field[name] like update)
*/
@Test
void queryDocument5() {
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
final List<QueryBuilder> should = queryBuilder.should();
should.add(QueryBuilders.matchQuery("name", "es-name-0"));
should.add(QueryBuilders.matchQuery("name", "update"));
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(4, documents.size());
}

/**
* (field[name] like es||name||0||es-name||name-0||es-name-0) && (field[name] like update)
*/
@Test
void queryDocument6() {
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
List<QueryBuilder> must = queryBuilder.must();
must.add(QueryBuilders.matchQuery("name", "es-name-0"));
must.add(QueryBuilders.matchPhraseQuery("name", "update"));
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(1, documents.size());
}

/**
* (field[name] like es||name||0||es-name||name-0||es-name-0) && ((field[name] like update) && (field[name] like 3))
*/
@Test
void queryDocument7() {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
List<QueryBuilder> must = queryBuilder.must();
must.add(QueryBuilders.matchQuery("name", "es-name-0"));

List<QueryBuilder> filter = queryBuilder.filter();
filter.add(QueryBuilders.matchPhraseQuery("name", "update"));
filter.add(QueryBuilders.matchPhraseQuery("name", "3"));
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(1, documents.size());
}

/**
* 时间格式化
*/
@Test
void dateTimeFormat() {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime beginTime = LocalDateTime.parse("2021-01-04 12:00:00", dateTimeFormatter);
String format = beginTime.format(dateTimeFormatter);
assertEquals("2021-01-04 12:00:00", format);
}

/**
* 滚动查询
*/
@Test
void scrollQuery() {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
List<QueryBuilder> filter = queryBuilder.filter();
filter.add(QueryBuilders.matchPhraseQuery("name", "es"));
filter.add(QueryBuilders.matchPhraseQuery("name", "name"));
Query query = new NativeSearchQueryBuilder().withPageable(PageRequest.of(0, 1000))
.withQuery(queryBuilder).withTrackScores(true).build();
SearchScrollHits<User> searchHits = elasticsearchRestTemplate.searchScrollStart(30000, query,
User.class, index);
String scrollId = searchHits.getScrollId();

List<User> data = new ArrayList<>();
while (searchHits.hasSearchHits()) {
List<User> list = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
searchHits = elasticsearchRestTemplate.searchScrollContinue(scrollId, 30000, User.class,
index);
data.addAll(list);
}
// 清除 scroll
elasticsearchRestTemplate.searchScrollClear(Collections.singletonList(scrollId));
scrollId = null;
assertEquals(4, data.size());
}

/**
* (field[name] like es-name-0 && field[name] like update) || (field[name] like es-name-3 && field[name] like update)
*/
@Test
void filter() {
BoolQueryBuilder boolQuery1 = QueryBuilders.boolQuery();
List<QueryBuilder> filter1 = boolQuery1.filter();
filter1.add(QueryBuilders.matchPhraseQuery("name", "es-name-0"));
filter1.add(QueryBuilders.matchPhraseQuery("name", "update"));

BoolQueryBuilder boolQuery2 = QueryBuilders.boolQuery();
List<QueryBuilder> filter2 = boolQuery2.filter();
filter2.add(QueryBuilders.matchPhraseQuery("name", "es-name-3"));
filter2.add(QueryBuilders.matchPhraseQuery("name", "update"));

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
List<QueryBuilder> should = boolQueryBuilder.should();
should.add(boolQuery1);
should.add(boolQuery2);

Query query = new NativeSearchQueryBuilder().withPageable(PageRequest.of(0, 10)).withQuery(boolQueryBuilder)
.withTrackScores(true).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(1, documents.size());
}

/**
* 正则查询
*/
@Test
void regexpQuery() {
RegexpQueryBuilder query = QueryBuilders.regexpQuery("name",
".*es.*");
NativeSearchQuery regexpQuery = new NativeSearchQueryBuilder().withQuery(query).build();
SearchHits<User> searchHits = elasticsearchRestTemplate.search(regexpQuery, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(4, documents.size());
}

/**
* (field[_id] like YMB45ihkmA3WUmv && [name] like es-name0)
* || (field[_id] like YMB45ihkmA3W0lT && [name] like es-name-1)
* || (field[_id] like 7bdb35c9f1a4402eb43b0c8f5ec44804 && [name] like es-name-2)
* || (field[_id] like d2c1a0efa64b416da052b5f62cc101b4 && field[name] like es-name-3)
*/
@Test
void criteriaQuery() {
Criteria criteria1 = Criteria.where("_id").is("YMB45ihkmA3WUmv").and("name").is("es-name-0");
Criteria criteria2 = Criteria.where("_id").is("YMB45ihkmA3W0lT").and("name").is("es-name-1");
Criteria criteria3 = Criteria.where("_id").is("7bdb35c9f1a4402eb43b0c8f5ec44804").and(Criteria.where("name").is("es-name-2"));
Criteria criteria4 = Criteria.where("_id").is("d2c1a0efa64b416da052b5f62cc101b4").and(Criteria.where("name").is("es-name-3"));
Criteria criteria = new Criteria().or(criteria1).or(criteria2).or(criteria3).or(criteria4);

CriteriaQuery query = new CriteriaQuery(criteria);
SearchHits<User> searchHits = elasticsearchRestTemplate.search(query, User.class, index);
List<User> documents = searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.collect(Collectors.toList());
assertEquals(4, documents.size());
}
}

CriteriaQuery基本使用

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
@Repository
public class ESDAO implements DataDAO {

private static final String ID = "id";

@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;

@Override
public Page<Document> pageQuery(String index, Query query, Map<String, String> fieldMapping, int pageNo,
int pageSize) {
Page<Document> page = new Page<>(pageNo, pageSize);
CriteriaQuery parse = parse(query);
long total = elasticsearchRestTemplate.count(parse, org.springframework.data.elasticsearch.core.document.Document.class, IndexCoordinates.of(index));
page.setTotal(total);
if (total == 0) {
page.setRows(Collections.emptyList());
} else {
parse.setPageable(PageRequest.of(pageNo - 1, pageSize)).addSort(parseSort(query.getSorts()));
SearchHits<org.springframework.data.elasticsearch.core.document.Document> searchHits = elasticsearchRestTemplate.search(parse, org.springframework.data.elasticsearch.core.document.Document.class, IndexCoordinates.of(index));
List<Document> rows = searchHits2List(searchHits);
page.setRows(rows);
}
return page;
}

@Override
public List<Document> query(String index, Query query, Map<String, String> fieldMapping) {
CriteriaQuery parse = parse(query);
parse.addSort(parseSort(query.getSorts()));
SearchHits<org.springframework.data.elasticsearch.core.document.Document> searchHits = elasticsearchRestTemplate.search(parse, org.springframework.data.elasticsearch.core.document.Document.class, IndexCoordinates.of(index));
return searchHits2List(searchHits);
}

private List<Document> searchHits2List(SearchHits<org.springframework.data.elasticsearch.core.document.Document> searchHits) {
return searchHits.getSearchHits().parallelStream().map(SearchHit::getContent)
.map(d -> {
Document document = new Document();
document.putAll(d);
return document;
})
.collect(Collectors.toList());
}

private static org.springframework.data.domain.Sort parseSort(Sort[] sorts) {
if (sorts != null && sorts.length > 0) {
List<org.springframework.data.domain.Sort.Order> list = new ArrayList<>();
for (Sort sort : sorts) {
if (sort.getDirection() == Sort.ASC) {
org.springframework.data.domain.Sort.Order order = new org.springframework.data.domain.Sort.Order(org.springframework.data.domain.Sort.Direction.ASC, sort.getKey());
list.add(order);
}
else {
org.springframework.data.domain.Sort.Order order = new org.springframework.data.domain.Sort.Order(org.springframework.data.domain.Sort.Direction.DESC, sort.getKey());
list.add(order);
}
}
return org.springframework.data.domain.Sort.by(list);
}
else {
return org.springframework.data.domain.Sort.by(ID);
}
}

private CriteriaQuery parse(Query query) {
return new CriteriaQuery(parse(query.getCriteria()));
}

private org.springframework.data.elasticsearch.core.query.Criteria parse(Criteria criteria) {
org.springframework.data.elasticsearch.core.query.Criteria criteria1 = null;
switch (criteria.getOperator()) {
case Criteria.OPERATOR_EQ:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).is(criteria.getValue());
break;
case Criteria.OPERATOR_NOT_EQ:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).is(criteria.getValue()).not();
break;
case Criteria.OPERATOR_LIKE:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).startsWith((String) criteria.getValue());
break;
case Criteria.OPERATOR_GT:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).greaterThan(criteria.getValue());
break;
case Criteria.OPERATOR_GTE:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).greaterThanEqual(criteria.getValue());
break;
case Criteria.OPERATOR_LT:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).lessThan(criteria.getValue());
break;
case Criteria.OPERATOR_LTE:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).lessThanEqual(criteria.getValue());
break;
case Criteria.OPERATOR_IN:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).in(asArray(criteria.getValue()));
break;
case Criteria.OPERATOR_NOT_IN:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).in(asArray(criteria.getValue())).not();
break;
case Criteria.OPERATOR_ELEMMATCH:
criteria1 = org.springframework.data.elasticsearch.core.query.Criteria.where(criteria.getKey()).contains((String) criteria.getValue());
break;
default:
throw new InvalidDataAccessApiUsageException("Illegal criteria found '" + criteria.getOperator() + "'.");
}

if (!criteria.getChains().isEmpty()) {
for (Criteria c2 : criteria.getChains()) {
switch (c2.getLogicalOperation()) {
case Criteria.LOGICAL_OPERATION_AND:
criteria1.and(parse(c2));
break;
case Criteria.LOGICAL_OPERATION_OR:
criteria1.or(parse(c2));
break;
}
}
return criteria1;
} else {
return criteria1;
}
}

private Object[] asArray(Object o) {
if (o instanceof Collection) {
return ((Collection<?>) o).toArray();
} else if (o.getClass().isArray()) {
return (Object[]) o;
}
return new Object[] { o };
}

}