2021-01-16 16:35:11 +01:00
|
|
|
package it.cavallium.dbengine.database.luceneutil;
|
|
|
|
|
2021-01-29 17:19:01 +01:00
|
|
|
import it.cavallium.dbengine.database.LLKeyScore;
|
2021-01-16 16:35:11 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.function.Consumer;
|
2021-01-29 17:19:01 +01:00
|
|
|
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;
|
2021-01-29 17:19:01 +01:00
|
|
|
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
|
2021-01-29 17:19:01 +01:00
|
|
|
public void search(IndexSearcher indexSearcher,
|
2021-01-16 16:35:11 +01:00
|
|
|
Query query,
|
|
|
|
int limit,
|
|
|
|
@Nullable Sort luceneSort,
|
2021-01-29 17:19:01 +01:00
|
|
|
ScoreMode scoreMode,
|
2021-01-16 16:35:11 +01:00
|
|
|
String keyFieldName,
|
2021-01-29 17:19:01 +01:00
|
|
|
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");
|
|
|
|
}
|
2021-01-29 17:19:01 +01:00
|
|
|
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");
|
|
|
|
}
|
2021-01-29 17:19:01 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|