CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/luceneutil/CountStreamSearcher.java

46 lines
1.5 KiB
Java
Raw Normal View History

2021-01-16 16:35:11 +01:00
package it.cavallium.dbengine.database.luceneutil;
import it.cavallium.dbengine.database.LLKeyScore;
2021-01-16 16:35:11 +01:00
import java.io.IOException;
import java.util.function.Consumer;
import java.util.function.LongConsumer;
2021-01-16 16:35:11 +01:00
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreMode;
2021-01-16 16:35:11 +01:00
import org.apache.lucene.search.Sort;
import org.jetbrains.annotations.Nullable;
/**
* Search that only count approximate results without returning any result
*/
public class CountStreamSearcher implements LuceneStreamSearcher {
@Override
public void search(IndexSearcher indexSearcher,
2021-01-16 16:35:11 +01:00
Query query,
int limit,
@Nullable Sort luceneSort,
ScoreMode scoreMode,
2021-01-16 16:35:11 +01:00
String keyFieldName,
Consumer<LLKeyScore> resultsConsumer,
LongConsumer totalHitsConsumer) throws IOException {
2021-01-16 16:35:11 +01:00
if (limit != 0) {
throw new IllegalArgumentException("CountStream doesn't support a limit different than 0");
}
if (luceneSort != null) {
throw new IllegalArgumentException("CountStream doesn't support sorting");
}
if (resultsConsumer != null) {
2021-01-16 16:35:11 +01:00
throw new IllegalArgumentException("CountStream doesn't support a results consumer");
}
if (keyFieldName != null) {
throw new IllegalArgumentException("CountStream doesn't support a key field");
}
totalHitsConsumer.accept(count(indexSearcher, query));
2021-01-16 16:35:11 +01:00
}
public long count(IndexSearcher indexSearcher, Query query) throws IOException {
return indexSearcher.count(query);
}
}