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

79 lines
2.8 KiB
Java
Raw Normal View History

2021-11-09 00:05:26 +01:00
package it.cavallium.dbengine.lucene.searcher;
import it.cavallium.dbengine.client.query.current.data.TotalHitsCount;
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.lucene.LuceneUtils;
import it.cavallium.dbengine.utils.DBException;
2022-01-28 21:12:10 +01:00
import java.io.IOException;
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;
import org.apache.lucene.index.QueryTimeout;
import org.apache.lucene.index.QueryTimeoutImpl;
2022-02-26 03:28:20 +01:00
import org.jetbrains.annotations.Nullable;
2021-11-09 00:05:26 +01:00
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
public LuceneSearchResult collectMulti(LLIndexSearchers indexSearchers,
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, 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 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
var results = indexSearchers
.llShards()
.stream()
.map(searcher -> this.collect(searcher, queryParams, keyFieldName, transformer))
.toList();
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
var totalHitsCount = new TotalHitsCount(totalHitsCountValue, exactTotalHitsCount);
2021-11-09 00:05:26 +01:00
return new LuceneSearchResult(totalHitsCount, Stream.empty());
2021-11-09 00:05:26 +01:00
}
@Override
public LuceneSearchResult collect(LLIndexSearcher indexSearcher,
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, indexSearcher, queryParams, keyFieldName, transformer);
}
try {
var is = indexSearcher.getIndexSearcher();
is.setTimeout(new QueryTimeoutImpl(queryParams.timeout().toMillis()));
var count = is.count(queryParams.query());
return new LuceneSearchResult(TotalHitsCount.of(count, true), Stream.empty());
} catch (IOException e) {
throw new DBException(e);
2022-07-02 11:44:13 +02:00
}
2021-11-09 00:05:26 +01:00
}
@Override
public String getName() {
return "count";
}
}