CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/searcher/UnsortedStreamingMultiSearcher.java

95 lines
3.1 KiB
Java
Raw Normal View History

2021-12-23 02:13:51 +01:00
package it.cavallium.dbengine.lucene.searcher;
import static com.google.common.collect.Streams.mapWithIndex;
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.disk.LLIndexSearchers;
import it.cavallium.dbengine.lucene.LuceneCloseable;
2021-12-23 02:13:51 +01:00
import it.cavallium.dbengine.lucene.LuceneUtils;
2022-06-14 13:10:38 +02:00
import java.io.IOException;
2021-12-23 02:13:51 +01:00
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
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
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
public LuceneSearchResult collectMulti(LLIndexSearchers indexSearchers,
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-07-02 11:44:13 +02:00
if (transformer != GlobalQueryRewrite.NO_REWRITE) {
return LuceneUtils.rewriteMulti(this, indexSearchers, queryParams, keyFieldName, transformer);
2022-07-02 11:44:13 +02:00
}
if (queryParams.isSorted() && queryParams.limitLong() > 0) {
throw new UnsupportedOperationException("Sorted queries are not supported" + " by UnsortedContinuousLuceneMultiSearcher");
}
var localQueryParams = getLocalQueryParams(queryParams);
2021-12-23 02:13:51 +01:00
var shards = indexSearchers.shards();
2021-12-23 02:13:51 +01:00
Stream<ScoreDoc> scoreDocsFlux = getScoreDocs(localQueryParams, shards);
2021-12-23 02:13:51 +01:00
Stream<LLKeyScore> resultsFlux = LuceneUtils.convertHits(scoreDocsFlux, shards, keyFieldName);
2021-12-23 02:13:51 +01:00
var totalHitsCount = new TotalHitsCount(0, false);
Stream<LLKeyScore> mergedFluxes = resultsFlux.skip(queryParams.offsetLong()).limit(queryParams.limitLong());
return new MyLuceneSearchResult(totalHitsCount, mergedFluxes, indexSearchers);
2021-12-23 02:13:51 +01:00
}
private Stream<ScoreDoc> getScoreDocs(LocalQueryParams localQueryParams, List<IndexSearcher> shards) {
return mapWithIndex(shards.stream(),
(shard, shardIndex) -> LuceneGenerator.reactive(shard, localQueryParams, (int) shardIndex))
.parallel()
.flatMap(Function.identity());
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";
}
private static class MyLuceneSearchResult extends LuceneSearchResult implements LuceneCloseable {
private final LLIndexSearchers indexSearchers;
public MyLuceneSearchResult(TotalHitsCount totalHitsCount,
Stream<LLKeyScore> hitsFlux,
LLIndexSearchers indexSearchers) {
super(totalHitsCount, hitsFlux);
this.indexSearchers = indexSearchers;
}
@Override
protected void onClose() {
try {
indexSearchers.close();
} catch (Throwable e) {
LOG.error("Can't close index searchers", e);
}
super.onClose();
}
}
2021-12-23 02:13:51 +01:00
}