2021-01-30 22:14:48 +01:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-01-29 17:19:01 +01:00
|
|
|
import it.cavallium.dbengine.database.LLKeyScore;
|
2021-01-30 22:14:48 +01:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneParallelStreamCollectorManager;
|
2021-02-04 22:42:57 +01:00
|
|
|
import it.cavallium.dbengine.lucene.LuceneParallelStreamCollectorResult;
|
2020-12-07 22:15:18 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
import org.apache.lucene.document.Document;
|
|
|
|
import org.apache.lucene.index.IndexableField;
|
|
|
|
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;
|
2020-12-07 22:15:18 +01:00
|
|
|
import org.apache.lucene.search.Sort;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsorted search (low latency and constant memory usage)
|
|
|
|
*/
|
|
|
|
public class ParallelCollectorStreamSearcher implements LuceneStreamSearcher {
|
|
|
|
|
2021-04-01 19:48:25 +02:00
|
|
|
private final CountStreamSearcher countStreamSearcher;
|
|
|
|
|
|
|
|
public ParallelCollectorStreamSearcher(CountStreamSearcher countStreamSearcher) {
|
|
|
|
this.countStreamSearcher = countStreamSearcher;
|
|
|
|
}
|
|
|
|
|
2020-12-07 22:15:18 +01:00
|
|
|
@Override
|
2021-04-01 19:48:25 +02:00
|
|
|
public LuceneSearchInstance search(IndexSearcher indexSearcher,
|
2020-12-07 22:15:18 +01:00
|
|
|
Query query,
|
2021-04-01 19:48:25 +02:00
|
|
|
int offset,
|
2020-12-07 22:15:18 +01:00
|
|
|
int limit,
|
|
|
|
@Nullable Sort luceneSort,
|
2021-01-29 17:19:01 +01:00
|
|
|
ScoreMode scoreMode,
|
2021-02-14 13:46:11 +01:00
|
|
|
@Nullable Float minCompetitiveScore,
|
2021-04-01 19:48:25 +02:00
|
|
|
String keyFieldName) throws IOException {
|
|
|
|
if (offset != 0) {
|
|
|
|
throw new IllegalArgumentException("ParallelCollectorStreamSearcher doesn't support a offset different than 0");
|
|
|
|
}
|
2020-12-07 22:15:18 +01:00
|
|
|
if (luceneSort != null) {
|
|
|
|
throw new IllegalArgumentException("ParallelCollectorStreamSearcher doesn't support sorted searches");
|
|
|
|
}
|
|
|
|
|
2021-04-01 19:48:25 +02:00
|
|
|
return new LuceneSearchInstance() {
|
2020-12-07 22:15:18 +01:00
|
|
|
|
2021-04-01 19:48:25 +02:00
|
|
|
long totalHitsCount = countStreamSearcher.countLong(indexSearcher, query);
|
|
|
|
|
|
|
|
@Override
|
2021-04-03 19:09:06 +02:00
|
|
|
public long getTotalHitsCount() {
|
2021-04-01 19:48:25 +02:00
|
|
|
return totalHitsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void getResults(ResultItemConsumer resultsConsumer) throws IOException {
|
|
|
|
AtomicInteger currentCount = new AtomicInteger();
|
|
|
|
|
|
|
|
LuceneParallelStreamCollectorResult result = indexSearcher.search(query,
|
|
|
|
LuceneParallelStreamCollectorManager.fromConsumer(scoreMode, minCompetitiveScore, (docId, score) -> {
|
|
|
|
if (currentCount.getAndIncrement() >= limit) {
|
|
|
|
return HandleResult.HALT;
|
2020-12-07 22:15:18 +01:00
|
|
|
} else {
|
2021-04-01 19:48:25 +02:00
|
|
|
Document d = indexSearcher.doc(docId, Set.of(keyFieldName));
|
|
|
|
if (d.getFields().isEmpty()) {
|
|
|
|
logger.error("The document docId: {} is empty.", docId);
|
|
|
|
var realFields = indexSearcher.doc(docId).getFields();
|
|
|
|
if (!realFields.isEmpty()) {
|
|
|
|
logger.error("Present fields:");
|
|
|
|
for (IndexableField field : realFields) {
|
|
|
|
logger.error(" - {}", field.name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var field = d.getField(keyFieldName);
|
|
|
|
if (field == null) {
|
|
|
|
logger.error("Can't get key of document docId: {}", docId);
|
|
|
|
} else {
|
|
|
|
if (resultsConsumer.accept(new LLKeyScore(field.stringValue(), score)) == HandleResult.HALT) {
|
|
|
|
return HandleResult.HALT;
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 00:13:57 +01:00
|
|
|
}
|
2021-04-01 19:48:25 +02:00
|
|
|
return HandleResult.CONTINUE;
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
2021-04-01 19:48:25 +02:00
|
|
|
}));
|
|
|
|
this.totalHitsCount = result.getTotalHitsCount();
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
2021-04-01 19:48:25 +02:00
|
|
|
};
|
2020-12-07 22:15:18 +01:00
|
|
|
}
|
|
|
|
}
|