2021-10-15 22:03:53 +02:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
|
|
|
import io.net5.buffer.api.Send;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyScore;
|
|
|
|
import it.cavallium.dbengine.database.LLUtils;
|
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
|
|
|
import it.cavallium.dbengine.database.disk.LLTempLMDBEnv;
|
|
|
|
import it.cavallium.dbengine.lucene.FullDocs;
|
|
|
|
import it.cavallium.dbengine.lucene.LLFieldDoc;
|
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
|
|
|
import it.cavallium.dbengine.lucene.collector.LMDBFullFieldDocCollector;
|
|
|
|
import it.cavallium.dbengine.lucene.searcher.LLSearchTransformer.TransformerInput;
|
2021-12-17 01:48:49 +01:00
|
|
|
import org.apache.logging.log4j.LogManager;
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2021-10-15 22:03:53 +02:00
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
2021-10-16 14:35:04 +02:00
|
|
|
public class SortedScoredFullMultiSearcher implements MultiSearcher {
|
2021-10-15 22:03:53 +02:00
|
|
|
|
2021-12-17 01:48:49 +01:00
|
|
|
protected static final Logger logger = LogManager.getLogger(SortedScoredFullMultiSearcher.class);
|
2021-10-15 22:03:53 +02:00
|
|
|
|
|
|
|
private final LLTempLMDBEnv env;
|
|
|
|
|
2021-10-16 14:35:04 +02:00
|
|
|
public SortedScoredFullMultiSearcher(LLTempLMDBEnv env) {
|
|
|
|
this.env = env;
|
2021-10-15 22:03:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-11-08 11:17:52 +01:00
|
|
|
public Mono<LuceneSearchResult> collectMulti(Mono<Send<LLIndexSearchers>> indexSearchersMono,
|
2021-10-15 22:03:53 +02:00
|
|
|
LocalQueryParams queryParams,
|
|
|
|
String keyFieldName,
|
|
|
|
LLSearchTransformer transformer) {
|
|
|
|
Mono<LocalQueryParams> queryParamsMono;
|
|
|
|
if (transformer == LLSearchTransformer.NO_TRANSFORMATION) {
|
|
|
|
queryParamsMono = Mono.just(queryParams);
|
|
|
|
} else {
|
|
|
|
queryParamsMono = LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> transformer.transform(Mono
|
|
|
|
.fromSupplier(() -> new TransformerInput(indexSearchers, queryParams))), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return queryParamsMono.flatMap(queryParams2 -> LLUtils.usingSendResource(indexSearchersMono, indexSearchers -> this
|
|
|
|
// Search results
|
|
|
|
.search(indexSearchers.shards(), queryParams2)
|
|
|
|
// Compute the results
|
|
|
|
.transform(fullDocsMono -> this.computeResults(fullDocsMono, indexSearchers,
|
|
|
|
keyFieldName, queryParams2))
|
|
|
|
// Ensure that one LuceneSearchResult is always returned
|
|
|
|
.single(),
|
|
|
|
false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search effectively the raw results
|
|
|
|
*/
|
|
|
|
private Mono<FullDocs<LLFieldDoc>> search(Iterable<IndexSearcher> indexSearchers,
|
|
|
|
LocalQueryParams queryParams) {
|
|
|
|
return Mono
|
|
|
|
.fromCallable(() -> {
|
|
|
|
LLUtils.ensureBlocking();
|
|
|
|
var totalHitsThreshold = queryParams.getTotalHitsThresholdLong();
|
|
|
|
return LMDBFullFieldDocCollector.createSharedManager(env, queryParams.sort(), queryParams.limitInt(),
|
|
|
|
totalHitsThreshold);
|
|
|
|
})
|
|
|
|
.flatMap(sharedManager -> Flux
|
|
|
|
.fromIterable(indexSearchers)
|
|
|
|
.flatMap(shard -> Mono.fromCallable(() -> {
|
|
|
|
LLUtils.ensureBlocking();
|
|
|
|
|
|
|
|
var collector = sharedManager.newCollector();
|
2021-11-16 23:54:23 +01:00
|
|
|
assert queryParams.computePreciseHitsCount() == collector.scoreMode().isExhaustive();
|
2021-10-15 22:03:53 +02:00
|
|
|
|
|
|
|
shard.search(queryParams.query(), collector);
|
|
|
|
return collector;
|
|
|
|
}))
|
|
|
|
.collectList()
|
|
|
|
.flatMap(collectors -> Mono.fromCallable(() -> {
|
|
|
|
LLUtils.ensureBlocking();
|
|
|
|
return sharedManager.reduce(collectors);
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the results, extracting useful data
|
|
|
|
*/
|
2021-11-08 11:17:52 +01:00
|
|
|
private Mono<LuceneSearchResult> computeResults(Mono<FullDocs<LLFieldDoc>> dataMono,
|
2021-10-15 22:03:53 +02:00
|
|
|
LLIndexSearchers indexSearchers,
|
|
|
|
String keyFieldName,
|
|
|
|
LocalQueryParams queryParams) {
|
|
|
|
return dataMono.map(data -> {
|
|
|
|
var totalHitsCount = LuceneUtils.convertTotalHitsCount(data.totalHits());
|
|
|
|
|
|
|
|
Flux<LLKeyScore> hitsFlux = LuceneUtils
|
|
|
|
.convertHits(data.iterate(queryParams.offsetLong()).map(LLFieldDoc::toFieldDoc),
|
|
|
|
indexSearchers.shards(), keyFieldName, true)
|
|
|
|
.take(queryParams.limitLong(), true);
|
|
|
|
|
2021-11-08 11:17:52 +01:00
|
|
|
return new LuceneSearchResult(totalHitsCount, hitsFlux, indexSearchers::close);
|
2021-10-15 22:03:53 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "sorted scored full multi";
|
|
|
|
}
|
|
|
|
}
|