2021-04-01 19:48:25 +02:00
|
|
|
package it.cavallium.dbengine.lucene.searcher;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import org.apache.lucene.search.FieldDoc;
|
|
|
|
import org.apache.lucene.search.IndexSearcher;
|
|
|
|
import org.apache.lucene.search.Query;
|
|
|
|
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,
|
|
|
|
FieldDoc after,
|
|
|
|
boolean doDocScores,
|
|
|
|
int totalHitsThreshold,
|
|
|
|
|
|
|
|
int topDocsStartOffset,
|
|
|
|
int topDocsHowMany) throws IOException {
|
|
|
|
TopDocsCollector<?> collector;
|
|
|
|
if (luceneSort == null) {
|
|
|
|
collector = TopScoreDocCollector.create(limit, after, totalHitsThreshold);
|
|
|
|
} else {
|
|
|
|
collector = TopFieldCollector.create(luceneSort, limit, after, totalHitsThreshold);
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
FieldDoc after,
|
|
|
|
boolean doDocScores,
|
|
|
|
int totalHitsThreshold) throws IOException {
|
|
|
|
TopDocsCollector<?> collector;
|
|
|
|
if (luceneSort == null) {
|
|
|
|
collector = TopScoreDocCollector.create(limit, after, totalHitsThreshold);
|
|
|
|
} else {
|
|
|
|
collector = TopFieldCollector.create(luceneSort, limit, after, totalHitsThreshold);
|
|
|
|
}
|
|
|
|
TopDocs topDocs = collector.topDocs();
|
2021-04-01 19:48:25 +02:00
|
|
|
if (doDocScores) {
|
|
|
|
TopFieldCollector.populateScores(topDocs.scoreDocs, indexSearcher, query);
|
|
|
|
}
|
|
|
|
return topDocs;
|
|
|
|
}
|
|
|
|
}
|