【发布时间】:2020-12-09 16:45:55
【问题描述】:
我在响应式弹性搜索中的索引名称如下:
logs-2020.08.18
logs-2020.08.17
logs-2020.08.16
它将每天创建。
我想获取最新的索引名称并使用 reactiveElasticsearchClient 或 spring 数据获取日志。 有可能吗?
我在我的 spring webflux 应用程序中尝试了以下方式:
我有以下代码 sn-p 来查找索引可用性:
public Flux<Log> getLogFromLatestIndex(String serialId) {
Calendar cal = Calendar.getInstance();
String currentIndex = StringUtils.EMPTY;
boolean indexExists = false;
while (!indexExists) {
currentIndex = String.format("logs-%s”, format(cal.getTime(), "yyyy.MM.dd"));
indexExists = isIndexExists(currentIndex).block();
cal.add(Calendar.DATE, -1); // Decrease day 1 until you find index
}
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchQuery("serialId", serialId))
.withIndices(currentIndex)
.build();
return reactiveElasticsearchTemplate.find(searchQuery, Log.class);
}
public Mono<Boolean> isIndexExists(String indexName) {
return reactiveElasticsearchClient.indices().existsIndex(new GetIndexRequest().indices(indexName));
}
如何在不使用块的情况下获取布尔值
indexExists = isIndexExists(currentIndex).block();
显然我会得到以下错误:
java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-2
【问题讨论】:
-
你的应用是 Spring WebFlux 应用吗?你想最后返回一个
Mono<String>,这是最新索引的名称吗? -
是的,它是 spring webflux 应用程序。我需要最新的索引名称来搜索一些数据。如下所示: SearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(matchQuery("serialId", serialId)) .withIndices(currentIndex) .build(); return reactiveElasticsearchTemplate.find(searchQuery, Log.class); // 最后我会返回flux。
-
@MartinTarjány 我已经更新了我的问题
-
cal变量来自哪里? -
抱歉,现在更新
标签: java elasticsearch spring-webflux project-reactor reactive