2021-04-01 19:48:25 +02:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
2021-07-26 19:21:17 +02:00
|
|
|
import static it.cavallium.dbengine.lucene.searcher.PaginationInfo.ALLOW_UNSCORED_PAGINATION_MODE;
|
|
|
|
|
2021-10-13 00:23:56 +02:00
|
|
|
import it.cavallium.dbengine.lucene.collector.UnscoredCollector;
|
2021-04-01 19:48:25 +02:00
|
|
|
import org.apache.lucene.search.FieldDoc;
|
2021-07-05 15:43:19 +02:00
|
|
|
import org.apache.lucene.search.ScoreDoc;
|
2021-04-01 19:48:25 +02:00
|
|
|
import org.apache.lucene.search.Sort;
|
|
|
|
import org.apache.lucene.search.TopDocsCollector;
|
|
|
|
import org.apache.lucene.search.TopFieldCollector;
|
|
|
|
import org.apache.lucene.search.TopScoreDocCollector;
|
|
|
|
|
|
|
|
class TopDocsSearcher {
|
|
|
|
|
2021-07-08 18:54:53 +02:00
|
|
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
|
|
|
public static TopDocsCollector<ScoreDoc> getTopDocsCollector(Sort luceneSort,
|
2021-07-05 15:43:19 +02:00
|
|
|
int limit,
|
|
|
|
ScoreDoc after,
|
2021-07-26 19:21:17 +02:00
|
|
|
int totalHitsThreshold,
|
2021-07-27 19:34:51 +02:00
|
|
|
boolean allowPagination,
|
2021-07-26 19:21:17 +02:00
|
|
|
boolean computeScores) {
|
2021-07-08 18:54:53 +02:00
|
|
|
TopDocsCollector<ScoreDoc> collector;
|
2021-07-27 19:34:51 +02:00
|
|
|
if (after != null && !allowPagination) {
|
|
|
|
throw new IllegalArgumentException("\"allowPagination\" is false, but \"after\" is set");
|
|
|
|
}
|
2021-07-05 15:43:19 +02:00
|
|
|
if (luceneSort == null) {
|
|
|
|
if (after == null) {
|
2021-07-27 19:34:51 +02:00
|
|
|
if (computeScores || allowPagination || !ALLOW_UNSCORED_PAGINATION_MODE) {
|
2021-07-26 19:21:17 +02:00
|
|
|
collector = TopScoreDocCollector.create(limit, totalHitsThreshold);
|
|
|
|
} else {
|
2021-07-27 19:34:51 +02:00
|
|
|
collector = new UnscoredCollector(limit);
|
2021-07-26 19:21:17 +02:00
|
|
|
}
|
2021-07-05 15:43:19 +02:00
|
|
|
} else {
|
2021-07-27 19:34:51 +02:00
|
|
|
collector = TopScoreDocCollector.create(limit, after, totalHitsThreshold);
|
2021-07-05 15:43:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (after == null) {
|
2021-07-08 18:54:53 +02:00
|
|
|
collector = (TopDocsCollector<ScoreDoc>) (TopDocsCollector) TopFieldCollector.create(luceneSort, limit, totalHitsThreshold);
|
2021-07-05 15:43:19 +02:00
|
|
|
} else if (after instanceof FieldDoc afterFieldDoc) {
|
2021-07-08 18:54:53 +02:00
|
|
|
collector = (TopDocsCollector<ScoreDoc>) (TopDocsCollector) TopFieldCollector.create(luceneSort, limit, afterFieldDoc, totalHitsThreshold);
|
2021-07-05 15:43:19 +02:00
|
|
|
} else {
|
|
|
|
throw new UnsupportedOperationException("GetTopDocs with \"luceneSort\" != null requires \"after\" to be a FieldDoc");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return collector;
|
|
|
|
}
|
2021-04-01 19:48:25 +02:00
|
|
|
}
|