Elasticsearch Jest 实践

Jest

GitHub
Jest是 Elasticsearch 的 Java Http Rest 客户端。ElasticSearch 已经具备应用于 Elasticsearch 内部的 Java API,但是 Jest 弥补了ES自有API缺少Elasticsearch Http Rest接口客户端的不足。

springBoot 集成 Jest 依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>6.3.0</version>
</dependency>

使用介绍

jest 使用

实践

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
/**
* 创建查询表达式
* @param criterias
* @param page
* @return
*/
private SearchSourceBuilder buildSearch(List<Criteria> criterias, EqlPage page) {
//指定查询的库表
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

// 分页
if (Objects.nonNull(page)) {
searchSourceBuilder.from((page.getCurrentPage() - 1) * page.getPageRows()).size(page.getPageRows());
}

if (CollectionUtils.isNotEmpty(criterias)) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
criterias.stream().forEach(t -> {
if (SHOULD.equals(t.getFieldType())) {
// 设置 should 匹配条件
boolQueryBuilder.should(QueryBuilders.termQuery(t.getFieldName(), t.getFieldValue()));
}
if (MUST.equals(t.getFieldType())) {
// 设置 must 匹配条件
boolQueryBuilder.must(QueryBuilders.termQuery(t.getFieldName(), t.getFieldValue()));
}
if (RANGE.equals(t.getFieldType())) {
// 设置 range 区间匹配条件
boolQueryBuilder.must(
QueryBuilders.rangeQuery(t.getFieldName()).format(DATE_FORMAT).from(t.getFieldValue()).to(t.getFieldValue2()));
}
});
searchSourceBuilder.query(boolQueryBuilder);
}
return searchSourceBuilder;
}

/**
* es 执行查询
* @param searchSourceBuilder
* @return
*/
public List<OrderBean> search(SearchSourceBuilder searchSourceBuilder,EqlPage page) {

List<OrderBean> list = Lists.newArrayList();
try {
SearchResult result = client.execute(new Search.Builder(searchSourceBuilder.toString())
// multiple index or types can be added.
.addIndex(baseConfig.getEsIndex())
.addType(baseConfig.getEsType())
// 设置排序字段与排序方式
.addSort(new Sort(StringUtils.isBlank(page.getSortField()) ? "create_time" : page.getSortField(),
StringUtils.isEmpty(page.getSort())
? Sort.Sorting.DESC : Sort.Sorting.ASC))
.build());

result.getSourceAsObjectList(OrderBean.class, false);
List<SearchResult.Hit<Object, Void>> hits = result.getHits(Object.class);
if (CollectionUtils.isEmpty(hits)) {
return list;
}
log.info("es - list size :{}", hits.size());
// 将查询结果映射到java实体里
hits.stream().forEach(t -> {
String news = JSON.toJSONString(t.source);
OrderBean order = JSON.parseObject(news, OrderBean.class);
list.add(order);
});
// 获取查询总记录数
page.setTotalRows(result.getTotal().intValue());
// 设置总页数
page.setPageNum((page.getTotalRows() - 1) / page.getPageRows() + 1);
} catch (Exception e) {
log.info("es - 查询失败:{}",JSON.toJSONString(e.getMessage()));
throw new BusinessException("9999","es 查询失败:{}",e.getMessage());
}
return list;
}
-------------本文结束感谢您的阅读-------------
Thank you for your encouragement