Add count stream searcher

This commit is contained in:
Andrea Cavalli 2021-01-16 16:35:11 +01:00
parent 572684972e
commit bfe6af4088
2 changed files with 45 additions and 1 deletions

View File

@ -15,11 +15,13 @@ public class AdaptiveStreamSearcher implements LuceneStreamSearcher {
private final SimpleStreamSearcher simpleStreamSearcher;
private final ParallelCollectorStreamSearcher parallelCollectorStreamSearcher;
private final PagedStreamSearcher pagedStreamSearcher;
private final CountStreamSearcher countStreamSearcher;
public AdaptiveStreamSearcher() {
this.simpleStreamSearcher = new SimpleStreamSearcher();
this.parallelCollectorStreamSearcher = new ParallelCollectorStreamSearcher();
this.pagedStreamSearcher = new PagedStreamSearcher(simpleStreamSearcher);
this.countStreamSearcher = new CountStreamSearcher();
}
@Override
@ -29,7 +31,9 @@ public class AdaptiveStreamSearcher implements LuceneStreamSearcher {
@Nullable Sort luceneSort,
String keyFieldName,
Consumer<String> consumer) throws IOException {
if (luceneSort == null) {
if (limit == 0) {
return countStreamSearcher.count(indexSearcher, query);
} else if (luceneSort == null) {
return parallelCollectorStreamSearcher.streamSearch(indexSearcher, query, limit, null, keyFieldName, consumer);
} else {
if (limit > PagedStreamSearcher.MAX_ITEMS_PER_PAGE) {

View File

@ -0,0 +1,40 @@
package it.cavallium.dbengine.database.luceneutil;
import java.io.IOException;
import java.util.function.Consumer;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
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 Long streamSearch(IndexSearcher indexSearcher,
Query query,
int limit,
@Nullable Sort luceneSort,
String keyFieldName,
Consumer<String> consumer) throws IOException {
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 (consumer != null) {
throw new IllegalArgumentException("CountStream doesn't support a results consumer");
}
if (keyFieldName != null) {
throw new IllegalArgumentException("CountStream doesn't support a key field");
}
return count(indexSearcher, query);
}
public long count(IndexSearcher indexSearcher, Query query) throws IOException {
return indexSearcher.count(query);
}
}