操作 - Spring Data
# 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.0.1</version>
</dependency>
spring-boot-starter-data-elasticsearch
无需指定版本,由 Spring Boot 维护
# Yaml
spring:
elasticsearch:
uris: https://xxxxxxx:9200
username: xxxxx
password: xxxxx
# EsConfiguration
@Configuration
public class EsConfiguration {
@Bean
public ElasticsearchClient elasticsearchClient(RestHighLevelClient highLevelClient) {
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(
highLevelClient.getLowLevelClient(), new JacksonJsonpMapper());
// And create the API client
return new ElasticsearchClient(transport);
}
}
将 RestHighLevelClient 转为 ElasticsearchClient
笔记
在编写本文时使用的 spring-boot-starter-data-elasticsearch:2.7.1
虽然已经将 RestHighLevelClient
标为 @Deprecated
但依然会自动注入,由于前人已在项目中使用 RHLC,出于对配置尽可能少的改变,因此并没有使用 Spring Data 文档中 extends ReactiveElasticsearchConfiguration
的方式配置新版的客户端(同时也因为会出现 https 导致的 Caused by: java.net.UnknownHostException
问题)
# 开启调试日志
YAML
logging:
level:
tracer: TRACE
# 示例代码
点击查看
@Repository
@RequiredArgsConstructor
public class ReaderMessageRepositoryImpl implements ReaderMessageRepository {
private final ElasticsearchClient esClient;
@Override
public PageResp<ReaderMessage> page(ReaderMessageQuery query) {
int size = ObjectUtil.defaultIfNull(query.getSize(), 10);
int from = ObjectUtil.defaultIfNull(query.getCurrent(), 1) - 1;
SearchResponse<ReaderMessage> response = null;
try {
response = esClient.search(s -> s
// 索引
.index(ESUtil.READER_MSG_INDEX)
// 查询条件
.query(q -> q
.bool(b -> {
if (StrUtil.isNotBlank(query.getDyCode())) {
b.filter(f -> f.term(t -> t.field("dyCode.keyword").value(query.getDyCode())));
}
if (StrUtil.isNotBlank(query.getAnchorDyCode())) {
b.filter(f -> f.term(t -> t.field("anchorDyCode.keyword").value(query.getAnchorDyCode())));
}
if (StrUtil.isNotBlank(query.getType())) {
b.filter(f -> f.term(t -> t.field("type.keyword").value(query.getType())));
}
if (Objects.nonNull(query.getMinTimestamp()) && Objects.nonNull(query.getMaxTimestamp())) {
b.filter(f -> f.range(r -> r.field("timestamp")
.gte(JsonData.of(query.getMinTimestamp()))
.lte(JsonData.of(query.getMaxTimestamp()))
));
}
return b;
}
)
)
// 排序
.sort(sort -> {
if (StrUtil.isNotBlank(query.getAscs())) {
sort.field(f -> f.field(query.getAscs()).order(SortOrder.Asc));
} else if (StrUtil.isNotBlank(query.getDescs())) {
sort.field(f -> f.field(query.getDescs()).order(SortOrder.Desc));
}
return sort;
})
// 分页
.from(from * size)
.size(size)
, ReaderMessage.class
);
} catch (IOException e) {
throw new RuntimeException(e);
}
List<ReaderMessage> list = response.hits().hits().stream().map(Hit::source).collect(Collectors.toList());
PageResp<ReaderMessage> pageResp = new PageResp<>();
pageResp.setRecords(list);
pageResp.setCurrent(from + 1);
pageResp.setTotal(response.hits().total().value());
return pageResp;
}
}
# 参考资料
上次更新: 2023/05/20, 16:38:38