2023-02-22 22:31:36 +01:00
|
|
|
package it.cavallium.dbengine.tests;
|
2021-09-19 12:35:48 +02:00
|
|
|
|
2022-01-28 21:12:10 +01:00
|
|
|
import static it.cavallium.dbengine.lucene.searcher.GlobalQueryRewrite.NO_REWRITE;
|
|
|
|
|
2023-02-22 22:31:36 +01:00
|
|
|
import com.google.common.collect.Streams;
|
2021-09-19 12:35:48 +02:00
|
|
|
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
|
|
|
|
import it.cavallium.dbengine.database.LLKeyScore;
|
2021-09-20 12:51:27 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLIndexSearchers;
|
2022-07-23 02:42:48 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneCloseable;
|
2022-07-02 11:44:13 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneUtils;
|
2022-01-28 21:12:10 +01:00
|
|
|
import it.cavallium.dbengine.lucene.searcher.GlobalQueryRewrite;
|
2021-11-09 00:05:26 +01:00
|
|
|
import it.cavallium.dbengine.lucene.searcher.LocalQueryParams;
|
|
|
|
import it.cavallium.dbengine.lucene.searcher.LocalSearcher;
|
|
|
|
import it.cavallium.dbengine.lucene.searcher.LuceneSearchResult;
|
|
|
|
import it.cavallium.dbengine.lucene.searcher.MultiSearcher;
|
2022-06-14 18:05:26 +02:00
|
|
|
import it.cavallium.dbengine.utils.SimpleResource;
|
2022-06-30 13:54:55 +02:00
|
|
|
import java.io.UncheckedIOException;
|
2021-09-19 12:35:48 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2023-02-26 21:41:20 +01:00
|
|
|
import java.util.function.Function;
|
2023-02-22 22:31:36 +01:00
|
|
|
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;
|
2023-02-26 21:41:20 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2021-09-19 12:35:48 +02:00
|
|
|
|
2021-10-13 12:25:32 +02:00
|
|
|
public class UnsortedUnscoredSimpleMultiSearcher implements MultiSearcher {
|
2021-09-19 12:35:48 +02:00
|
|
|
|
2022-06-14 13:10:38 +02:00
|
|
|
private static final Logger LOG = LogManager.getLogger(UnsortedUnscoredSimpleMultiSearcher.class);
|
|
|
|
|
2021-10-13 12:25:32 +02:00
|
|
|
private final LocalSearcher localSearcher;
|
2021-09-19 12:35:48 +02:00
|
|
|
|
2021-10-13 12:25:32 +02:00
|
|
|
public UnsortedUnscoredSimpleMultiSearcher(LocalSearcher localSearcher) {
|
2021-09-19 12:35:48 +02:00
|
|
|
this.localSearcher = localSearcher;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-02-22 22:31:36 +01:00
|
|
|
public LuceneSearchResult collectMulti(LLIndexSearchers indexSearchers,
|
2021-09-19 12:35:48 +02:00
|
|
|
LocalQueryParams queryParams,
|
2023-02-26 21:41:20 +01:00
|
|
|
@Nullable String keyFieldName,
|
|
|
|
GlobalQueryRewrite transformer,
|
|
|
|
Function<Stream<LLKeyScore>, Stream<LLKeyScore>> filterer) {
|
2022-07-02 11:44:13 +02:00
|
|
|
if (transformer != NO_REWRITE) {
|
2023-02-26 21:41:20 +01:00
|
|
|
return LuceneUtils.rewriteMulti(this, indexSearchers, queryParams, keyFieldName, transformer, filterer);
|
2022-07-02 11:44:13 +02:00
|
|
|
}
|
|
|
|
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-09-19 12:35:48 +02:00
|
|
|
|
2023-02-22 22:31:36 +01:00
|
|
|
var localQueryParams = getLocalQueryParams(queryParams);
|
|
|
|
var results = indexSearchers.llShards().stream()
|
2023-02-26 21:41:20 +01:00
|
|
|
.map(searcher -> localSearcher.collect(searcher, localQueryParams, keyFieldName, transformer, filterer))
|
2023-02-22 22:31:36 +01:00
|
|
|
.toList();
|
|
|
|
List<Stream<LLKeyScore>> resultsFluxes = new ArrayList<>(results.size());
|
|
|
|
boolean exactTotalHitsCount = true;
|
|
|
|
long totalHitsCountValue = 0;
|
|
|
|
for (LuceneSearchResult result : results) {
|
2023-02-26 21:41:20 +01:00
|
|
|
resultsFluxes.add(result.results().stream());
|
2023-02-22 22:31:36 +01:00
|
|
|
exactTotalHitsCount &= result.totalHitsCount().exact();
|
|
|
|
totalHitsCountValue += result.totalHitsCount().value();
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalHitsCount = new TotalHitsCount(totalHitsCountValue, exactTotalHitsCount);
|
|
|
|
//noinspection unchecked
|
|
|
|
Stream<LLKeyScore> mergedFluxes = (Stream<LLKeyScore>) (Stream) Streams.concat(resultsFluxes.toArray(Stream<?>[]::new))
|
|
|
|
.skip(queryParams.offsetLong())
|
|
|
|
.limit(queryParams.limitLong());
|
|
|
|
|
2023-02-26 21:41:20 +01:00
|
|
|
return new LuceneSearchResult(totalHitsCount, mergedFluxes.toList());
|
2021-09-22 11:03:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private LocalQueryParams getLocalQueryParams(LocalQueryParams queryParams) {
|
|
|
|
return new LocalQueryParams(queryParams.query(),
|
2021-10-15 22:03:53 +02:00
|
|
|
0L,
|
|
|
|
queryParams.offsetLong() + queryParams.limitLong(),
|
2021-09-22 11:03:39 +02:00
|
|
|
queryParams.pageLimits(),
|
|
|
|
queryParams.sort(),
|
2021-12-12 23:40:30 +01:00
|
|
|
queryParams.computePreciseHitsCount(),
|
|
|
|
queryParams.timeout()
|
2021-09-20 12:51:27 +02:00
|
|
|
);
|
2021-09-19 12:35:48 +02:00
|
|
|
}
|
2021-10-13 00:23:56 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getName() {
|
2021-10-13 12:25:32 +02:00
|
|
|
return "unsorted unscored simple multi";
|
2021-10-13 00:23:56 +02:00
|
|
|
}
|
2021-09-19 12:35:48 +02:00
|
|
|
}
|