2021-11-09 00:05:26 +01:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
2021-12-16 16:14:44 +01:00
|
|
|
import static it.cavallium.dbengine.client.UninterruptibleScheduler.uninterruptibleScheduler;
|
2022-06-14 17:46:49 +02:00
|
|
|
import static it.cavallium.dbengine.database.LLUtils.singleOrClose;
|
2022-07-02 11:44:13 +02:00
|
|
|
import static it.cavallium.dbengine.lucene.searcher.GlobalQueryRewrite.NO_REWRITE;
|
2021-12-16 16:14:44 +01:00
|
|
|
|
2022-03-16 13:47:56 +01:00
|
|
|
import io.netty5.buffer.api.Send;
|
2021-11-09 00:05:26 +01:00
|
|
|
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyScore;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearcher;
|
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
2022-07-02 11:44:13 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers.UnshardedIndexSearchers;
|
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2022-06-14 18:05:26 +02:00
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2022-01-28 21:12:10 +01:00
|
|
|
import java.io.IOException;
|
2022-06-30 13:54:55 +02:00
|
|
|
import java.io.UncheckedIOException;
|
2021-11-09 00:05:26 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-06-14 13:10:38 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2022-07-02 11:44:13 +02:00
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
2022-02-26 03:28:20 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-11-09 00:05:26 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
import reactor.core.scheduler.Schedulers;
|
|
|
|
|
|
|
|
public class CountMultiSearcher implements MultiSearcher {
|
|
|
|
|
2022-06-14 13:10:38 +02:00
|
|
|
protected static final Logger LOG = LogManager.getLogger(CountMultiSearcher.class);
|
2021-11-09 00:05:26 +01:00
|
|
|
|
|
|
|
@Override
|
2022-06-14 13:10:38 +02:00
|
|
|
public Mono<LuceneSearchResult> collectMulti(Mono<LLIndexSearchers> indexSearchersMono,
|
2021-11-09 00:05:26 +01:00
|
|
|
LocalQueryParams queryParams,
|
|
|
|
String keyFieldName,
|
2022-01-28 21:12:10 +01:00
|
|
|
GlobalQueryRewrite transformer) {
|
2022-07-02 11:44:13 +02:00
|
|
|
if (transformer != GlobalQueryRewrite.NO_REWRITE) {
|
|
|
|
return LuceneUtils.rewriteMulti(this, indexSearchersMono, queryParams, keyFieldName, transformer);
|
|
|
|
}
|
|
|
|
if (queryParams.isSorted() && queryParams.limitLong() > 0) {
|
|
|
|
throw new UnsupportedOperationException(
|
|
|
|
"Sorted queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
|
|
|
|
}
|
|
|
|
if (queryParams.needsScores() && queryParams.limitLong() > 0) {
|
|
|
|
throw new UnsupportedOperationException(
|
|
|
|
"Scored queries are not supported by SimpleUnsortedUnscoredLuceneMultiSearcher");
|
|
|
|
}
|
2021-11-09 00:05:26 +01:00
|
|
|
|
2022-07-02 11:44:13 +02:00
|
|
|
return Mono.usingWhen(indexSearchersMono, searchers -> Flux
|
|
|
|
.fromIterable(searchers.llShards())
|
|
|
|
.flatMap(searcher -> this.collect(Mono.just(searcher), queryParams, keyFieldName, transformer))
|
|
|
|
.collectList()
|
|
|
|
.map(results -> {
|
|
|
|
boolean exactTotalHitsCount = true;
|
|
|
|
long totalHitsCountValue = 0;
|
|
|
|
for (LuceneSearchResult result : results) {
|
|
|
|
exactTotalHitsCount &= result.totalHitsCount().exact();
|
|
|
|
totalHitsCountValue += result.totalHitsCount().value();
|
|
|
|
result.close();
|
|
|
|
}
|
2021-11-09 00:05:26 +01:00
|
|
|
|
2022-07-02 11:44:13 +02:00
|
|
|
var totalHitsCount = new TotalHitsCount(totalHitsCountValue, exactTotalHitsCount);
|
2021-11-09 00:05:26 +01:00
|
|
|
|
2022-07-02 11:44:13 +02:00
|
|
|
return new LuceneSearchResult(totalHitsCount, Flux.empty(), null);
|
|
|
|
})
|
2022-07-03 01:32:13 +02:00
|
|
|
.doOnDiscard(LuceneSearchResult.class, luceneSearchResult -> luceneSearchResult.close()),
|
|
|
|
LLUtils::finalizeResource);
|
2021-11-09 00:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-06-14 13:10:38 +02:00
|
|
|
public Mono<LuceneSearchResult> collect(Mono<LLIndexSearcher> indexSearcherMono,
|
2021-11-09 00:05:26 +01:00
|
|
|
LocalQueryParams queryParams,
|
2022-02-26 03:28:20 +01:00
|
|
|
@Nullable String keyFieldName,
|
2022-01-28 21:12:10 +01:00
|
|
|
GlobalQueryRewrite transformer) {
|
2022-07-02 11:44:13 +02:00
|
|
|
if (transformer != GlobalQueryRewrite.NO_REWRITE) {
|
|
|
|
return LuceneUtils.rewrite(this, indexSearcherMono, queryParams, keyFieldName, transformer);
|
|
|
|
}
|
2021-12-17 16:24:18 +01:00
|
|
|
|
2022-07-02 11:44:13 +02:00
|
|
|
return Mono.usingWhen(indexSearcherMono, indexSearcher -> Mono.fromCallable(() -> {
|
|
|
|
LLUtils.ensureBlocking();
|
|
|
|
return (long) indexSearcher.getIndexSearcher().count(queryParams.query());
|
|
|
|
}).subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic())), LLUtils::finalizeResource)
|
|
|
|
.publishOn(Schedulers.parallel())
|
|
|
|
.transform(TimeoutUtil.timeoutMono(queryParams.timeout()))
|
|
|
|
.map(count -> new LuceneSearchResult(TotalHitsCount.of(count, true), Flux.empty(), null));
|
2021-11-09 00:05:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "count";
|
|
|
|
}
|
|
|
|
}
|