在Reactor中实现while循环以获取最新的Elasticsearch索引

问题描述

我在反应式弹性搜索中的索引名称如下:

logs-2020.08.18
logs-2020.08.17
logs-2020.08.16

它将每天创建一次。

我想获取最新的索引名称,并使用reactElasticsearchClient或spring数据获取日志。 有可能吗?

我在spring webflux应用程序中尝试了以下方式:

我有下面的代码段来查找索引的可用性:

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)