2021-12-23 02:13:51 +01:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
2022-01-28 21:12:10 +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;
|
2021-12-23 02:13:51 +01:00
|
|
|
|
2022-03-16 13:47:56 +01:00
|
|
|
import io.netty5.buffer.api.Send;
|
2021-12-23 02:13:51 +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.LLIndexSearchers;
|
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2022-02-11 13:32:50 +01:00
|
|
|
import it.cavallium.dbengine.lucene.MaxScoreAccumulator;
|
2022-06-14 13:10:38 +02:00
|
|
|
import java.io.IOException;
|
2021-12-23 02:13:51 +01:00
|
|
|
import java.util.List;
|
2022-04-09 02:45:42 +02:00
|
|
|
import it.cavallium.dbengine.lucene.hugepq.search.CustomHitsThresholdChecker;
|
2022-06-14 13:10:38 +02:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-12-23 02:13:51 +01:00
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
|
import org.apache.lucene.search.ScoreDoc;
|
2022-02-26 03:28:20 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-12-23 02:13:51 +01:00
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
2022-01-28 21:12:10 +01:00
|
|
|
import reactor.core.scheduler.Schedulers;
|
2021-12-23 02:13:51 +01:00
|
|
|
|
|
|
|
public class UnsortedStreamingMultiSearcher implements MultiSearcher {
|
|
|
|
|
2022-06-14 13:10:38 +02:00
|
|
|
|
|
|
|
protected static final Logger LOG = LogManager.getLogger(UnsortedStreamingMultiSearcher.class);
|
|
|
|
|
2021-12-23 02:13:51 +01:00
|
|
|
@Override
|
2022-06-14 13:10:38 +02:00
|
|
|
public Mono<LuceneSearchResult> collectMulti(Mono<LLIndexSearchers> indexSearchersMono,
|
2021-12-23 02:13:51 +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-06-14 17:46:49 +02:00
|
|
|
return singleOrClose(indexSearchersMono, indexSearchers -> {
|
2021-12-23 02:13:51 +01:00
|
|
|
Mono<LocalQueryParams> queryParamsMono;
|
2022-01-28 21:12:10 +01:00
|
|
|
if (transformer == GlobalQueryRewrite.NO_REWRITE) {
|
2021-12-23 02:13:51 +01:00
|
|
|
queryParamsMono = Mono.just(queryParams);
|
|
|
|
} else {
|
2022-01-28 21:12:10 +01:00
|
|
|
queryParamsMono = Mono
|
|
|
|
.fromCallable(() -> transformer.rewrite(indexSearchers, queryParams))
|
|
|
|
.subscribeOn(uninterruptibleScheduler(Schedulers.boundedElastic()));
|
2021-12-23 02:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return queryParamsMono.map(queryParams2 -> {
|
|
|
|
var localQueryParams = getLocalQueryParams(queryParams2);
|
|
|
|
if (queryParams2.isSorted() && queryParams2.limitLong() > 0) {
|
|
|
|
throw new UnsupportedOperationException("Sorted queries are not supported"
|
|
|
|
+ " by UnsortedContinuousLuceneMultiSearcher");
|
|
|
|
}
|
|
|
|
var shards = indexSearchers.shards();
|
|
|
|
|
|
|
|
Flux<ScoreDoc> scoreDocsFlux = getScoreDocs(localQueryParams, shards);
|
|
|
|
|
|
|
|
Flux<LLKeyScore> resultsFlux = LuceneUtils.convertHits(scoreDocsFlux, shards, keyFieldName, false);
|
|
|
|
|
|
|
|
var totalHitsCount = new TotalHitsCount(0, false);
|
|
|
|
Flux<LLKeyScore> mergedFluxes = resultsFlux
|
|
|
|
.skip(queryParams2.offsetLong())
|
|
|
|
.take(queryParams2.limitLong(), true);
|
|
|
|
|
2022-06-05 16:38:39 +02:00
|
|
|
return new LuceneSearchResult(totalHitsCount, mergedFluxes, () -> {
|
2022-06-14 13:10:38 +02:00
|
|
|
try {
|
2022-06-05 16:38:39 +02:00
|
|
|
indexSearchers.close();
|
2022-06-14 13:10:38 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
LOG.error("Can't close index searchers", e);
|
2022-06-05 16:38:39 +02:00
|
|
|
}
|
|
|
|
});
|
2021-12-23 02:13:51 +01:00
|
|
|
});
|
2022-06-14 13:10:38 +02:00
|
|
|
});
|
2021-12-23 02:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private Flux<ScoreDoc> getScoreDocs(LocalQueryParams localQueryParams, List<IndexSearcher> shards) {
|
2022-02-11 13:32:50 +01:00
|
|
|
return Flux.defer(() -> {
|
|
|
|
var hitsThreshold = CustomHitsThresholdChecker.createShared(localQueryParams.getTotalHitsThresholdLong());
|
|
|
|
MaxScoreAccumulator maxScoreAccumulator = new MaxScoreAccumulator();
|
|
|
|
return Flux.fromIterable(shards).index().flatMap(tuple -> {
|
|
|
|
var shardIndex = (int) (long) tuple.getT1();
|
|
|
|
var shard = tuple.getT2();
|
2022-02-11 21:46:05 +01:00
|
|
|
return LuceneGenerator.reactive(shard, localQueryParams, shardIndex);
|
2022-02-11 13:32:50 +01:00
|
|
|
});
|
|
|
|
});
|
2021-12-23 02:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private LocalQueryParams getLocalQueryParams(LocalQueryParams queryParams) {
|
|
|
|
return new LocalQueryParams(queryParams.query(),
|
|
|
|
0L,
|
|
|
|
queryParams.offsetLong() + queryParams.limitLong(),
|
|
|
|
queryParams.pageLimits(),
|
|
|
|
queryParams.sort(),
|
|
|
|
queryParams.computePreciseHitsCount(),
|
|
|
|
queryParams.timeout()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "unsorted streaming multi";
|
|
|
|
}
|
|
|
|
}
|