CavalliumDBEngine/src/main/java/it/cavallium/dbengine/lucene/searcher/TopDocsSearcher.java

117 lines
3.6 KiB
Java
Raw Normal View History

2021-04-01 19:48:25 +02:00
package it.cavallium.dbengine.lucene.searcher;
import java.io.IOException;
2021-07-05 15:43:19 +02:00
import org.apache.lucene.search.Collector;
2021-04-01 19:48:25 +02:00
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
2021-07-05 15:43:19 +02:00
import org.apache.lucene.search.MultiCollectorManager.Collectors;
2021-04-01 19:48:25 +02:00
import org.apache.lucene.search.Query;
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.TopDocs;
import org.apache.lucene.search.TopDocsCollector;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.TopScoreDocCollector;
class TopDocsSearcher {
private final boolean doDocScores;
private final IndexSearcher indexSearcher;
private final Query query;
2021-07-04 01:34:17 +02:00
private final Sort luceneSort;
private final int limit;
private final FieldDoc after;
private final int totalHitsThreshold;
2021-04-01 19:48:25 +02:00
2021-07-04 01:34:17 +02:00
@Deprecated
2021-04-01 19:48:25 +02:00
public TopDocsSearcher(IndexSearcher indexSearcher,
Query query,
Sort luceneSort,
int limit,
FieldDoc after,
boolean doDocScores,
2021-07-04 01:34:17 +02:00
int totalHitsThreshold) {
2021-04-01 19:48:25 +02:00
this.indexSearcher = indexSearcher;
this.query = query;
2021-07-04 01:34:17 +02:00
this.luceneSort = luceneSort;
this.limit = limit;
this.after = after;
2021-04-01 19:48:25 +02:00
this.doDocScores = doDocScores;
2021-07-04 01:34:17 +02:00
this.totalHitsThreshold = totalHitsThreshold;
}
/**
* This method must not be called more than once!
*/
@Deprecated
public TopDocs getTopDocs(int offset, int limit) throws IOException {
return getTopDocs(indexSearcher, query, luceneSort, limit, after, doDocScores, totalHitsThreshold, offset, limit);
2021-04-01 19:48:25 +02:00
}
2021-04-14 02:37:03 +02:00
/**
* This method must not be called more than once!
*/
2021-07-04 01:34:17 +02:00
@Deprecated
2021-04-14 02:37:03 +02:00
public TopDocs getTopDocs() throws IOException {
2021-07-04 01:34:17 +02:00
return getTopDocs(indexSearcher, query, luceneSort, limit, after, doDocScores, totalHitsThreshold);
}
public static TopDocs getTopDocs(IndexSearcher indexSearcher,
Query query,
Sort luceneSort,
int limit,
2021-07-05 15:43:19 +02:00
ScoreDoc after,
2021-07-04 01:34:17 +02:00
boolean doDocScores,
int totalHitsThreshold,
int topDocsStartOffset,
int topDocsHowMany) throws IOException {
2021-07-05 15:43:19 +02:00
TopDocsCollector<?> collector = getTopDocsCollector(luceneSort, limit, after, totalHitsThreshold);
2021-07-05 18:55:11 +02:00
indexSearcher.search(query, collector);
2021-07-04 01:34:17 +02:00
TopDocs topDocs = collector.topDocs(topDocsStartOffset, topDocsHowMany);
2021-04-14 02:37:03 +02:00
if (doDocScores) {
TopFieldCollector.populateScores(topDocs.scoreDocs, indexSearcher, query);
}
return topDocs;
}
2021-07-04 01:34:17 +02:00
public static TopDocs getTopDocs(IndexSearcher indexSearcher,
Query query,
Sort luceneSort,
int limit,
2021-07-05 15:43:19 +02:00
ScoreDoc after,
2021-07-04 01:34:17 +02:00
boolean doDocScores,
int totalHitsThreshold) throws IOException {
2021-07-05 15:43:19 +02:00
TopDocsCollector<?> collector = getTopDocsCollector(luceneSort, limit, after, totalHitsThreshold);
indexSearcher.search(query, collector);
2021-07-04 01:34:17 +02:00
TopDocs topDocs = collector.topDocs();
2021-04-01 19:48:25 +02:00
if (doDocScores) {
TopFieldCollector.populateScores(topDocs.scoreDocs, indexSearcher, query);
}
return topDocs;
}
2021-07-05 15:43:19 +02:00
public static TopDocsCollector<? extends ScoreDoc> getTopDocsCollector(Sort luceneSort,
2021-07-05 15:43:19 +02:00
int limit,
ScoreDoc after,
int totalHitsThreshold) {
TopDocsCollector<? extends ScoreDoc> collector;
2021-07-05 15:43:19 +02:00
if (luceneSort == null) {
if (after == null) {
collector = TopScoreDocCollector.create(limit, totalHitsThreshold);
} else {
collector = TopScoreDocCollector.create(limit, after, totalHitsThreshold);
}
} else {
if (after == null) {
collector = TopFieldCollector.create(luceneSort, limit, totalHitsThreshold);
} else if (after instanceof FieldDoc afterFieldDoc) {
collector = TopFieldCollector.create(luceneSort, limit, afterFieldDoc, totalHitsThreshold);
} else {
throw new UnsupportedOperationException("GetTopDocs with \"luceneSort\" != null requires \"after\" to be a FieldDoc");
}
}
return collector;
}
2021-04-01 19:48:25 +02:00
}