2021-10-14 23:04:34 +02:00
|
|
|
/*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership.
|
|
|
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
* (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2022-04-09 02:45:42 +02:00
|
|
|
package it.cavallium.dbengine.lucene.hugepq.search;
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-12-18 21:01:14 +01:00
|
|
|
import it.cavallium.dbengine.database.SafeCloseable;
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.cavallium.dbengine.database.disk.LLTempHugePqEnv;
|
2021-10-14 23:04:34 +02:00
|
|
|
import it.cavallium.dbengine.lucene.FieldValueHitQueue;
|
|
|
|
import it.cavallium.dbengine.lucene.FullDocs;
|
2021-10-15 00:03:41 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LLFieldDoc;
|
2021-10-14 23:04:34 +02:00
|
|
|
import it.cavallium.dbengine.lucene.LLSlotDoc;
|
|
|
|
import it.cavallium.dbengine.lucene.LLSlotDocCodec;
|
2022-04-06 02:41:32 +02:00
|
|
|
import it.cavallium.dbengine.lucene.HugePqPriorityQueue;
|
2021-10-14 23:04:34 +02:00
|
|
|
import it.cavallium.dbengine.lucene.MaxScoreAccumulator;
|
2021-10-15 00:03:41 +02:00
|
|
|
import it.cavallium.dbengine.lucene.PriorityQueue;
|
|
|
|
import it.cavallium.dbengine.lucene.ResourceIterable;
|
2022-02-11 13:32:50 +01:00
|
|
|
import it.cavallium.dbengine.lucene.collector.FullDocsCollector;
|
|
|
|
import it.cavallium.dbengine.lucene.collector.FullFieldDocs;
|
2022-04-09 02:45:42 +02:00
|
|
|
import it.cavallium.dbengine.lucene.hugepq.mirrored.HitsThresholdChecker;
|
|
|
|
import it.cavallium.dbengine.lucene.hugepq.mirrored.MultiLeafFieldComparator;
|
2021-10-14 23:04:34 +02:00
|
|
|
import java.io.IOException;
|
2021-10-15 00:03:41 +02:00
|
|
|
import java.util.Arrays;
|
2021-10-14 23:04:34 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import org.apache.lucene.index.LeafReaderContext;
|
2022-04-09 02:45:42 +02:00
|
|
|
import org.apache.lucene.search.CollectionTerminatedException;
|
|
|
|
import org.apache.lucene.search.CollectorManager;
|
|
|
|
import org.apache.lucene.search.DocIdSetIterator;
|
|
|
|
import org.apache.lucene.search.FieldComparator;
|
|
|
|
import org.apache.lucene.search.LeafCollector;
|
|
|
|
import org.apache.lucene.search.LeafFieldComparator;
|
|
|
|
import org.apache.lucene.search.Scorable;
|
|
|
|
import org.apache.lucene.search.ScoreMode;
|
|
|
|
import org.apache.lucene.search.Sort;
|
|
|
|
import org.apache.lucene.search.SortField;
|
|
|
|
import org.apache.lucene.search.TotalHits;
|
2021-10-15 00:03:41 +02:00
|
|
|
import org.apache.lucene.search.TotalHits.Relation;
|
|
|
|
import reactor.core.publisher.Flux;
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
/**
|
|
|
|
* A {@link org.apache.lucene.search.Collector} that sorts by {@link SortField} using {@link FieldComparator}s.
|
|
|
|
*
|
2022-04-06 02:41:32 +02:00
|
|
|
* <p>See the {@link #create(LLTempHugePqEnv, Sort, int, int)} (org.apache.lucene.search.Sort, int, int)} method for instantiating a
|
2021-10-15 00:03:41 +02:00
|
|
|
* TopFieldCollector.
|
|
|
|
*
|
2021-11-16 23:19:13 +01:00
|
|
|
* This class must mirror this changes:
|
|
|
|
* <a href="https://github.com/apache/lucene/commits/main/lucene/core/src/java/org/apache/lucene/search/TopFieldCollector.java">
|
|
|
|
* Lucene TopFieldCollector changes on GitHub</a>
|
2021-10-15 00:03:41 +02:00
|
|
|
*/
|
2022-04-06 02:41:32 +02:00
|
|
|
public abstract class HugePqFullFieldDocCollector extends
|
|
|
|
FullDocsCollector<HugePqPriorityQueue<LLSlotDoc>, LLSlotDoc, LLFieldDoc> {
|
2021-10-15 00:03:41 +02:00
|
|
|
|
|
|
|
// TODO: one optimization we could do is to pre-fill
|
|
|
|
// the queue with sentinel value that guaranteed to
|
|
|
|
// always compare lower than a real hit; this would
|
|
|
|
// save having to check queueFull on each insert
|
|
|
|
|
|
|
|
private abstract class TopFieldLeafCollector implements LeafCollector {
|
|
|
|
|
|
|
|
final LeafFieldComparator comparator;
|
|
|
|
final int reverseMul;
|
|
|
|
Scorable scorer;
|
|
|
|
boolean collectedAllCompetitiveHits = false;
|
|
|
|
|
2022-02-11 13:32:50 +01:00
|
|
|
TopFieldLeafCollector(FieldValueHitQueue fieldValueHitQueue, Sort sort, LeafReaderContext context)
|
2021-10-15 00:03:41 +02:00
|
|
|
throws IOException {
|
|
|
|
// as all segments are sorted in the same way, enough to check only the 1st segment for
|
|
|
|
// indexSort
|
|
|
|
if (searchSortPartOfIndexSort == null) {
|
|
|
|
final Sort indexSort = context.reader().getMetaData().getSort();
|
|
|
|
searchSortPartOfIndexSort = canEarlyTerminate(sort, indexSort);
|
|
|
|
if (searchSortPartOfIndexSort) {
|
|
|
|
firstComparator.disableSkipping();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LeafFieldComparator[] comparators = fieldValueHitQueue.getComparators(context);
|
|
|
|
int[] reverseMuls = fieldValueHitQueue.getReverseMul();
|
|
|
|
if (comparators.length == 1) {
|
|
|
|
this.reverseMul = reverseMuls[0];
|
|
|
|
this.comparator = comparators[0];
|
|
|
|
} else {
|
|
|
|
this.reverseMul = 1;
|
|
|
|
this.comparator = new MultiLeafFieldComparator(comparators, reverseMuls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void countHit(int doc) throws IOException {
|
|
|
|
++totalHits;
|
|
|
|
hitsThresholdChecker.incrementHitCount();
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
if (minScoreAcc != null && (totalHits & minScoreAcc.modInterval) == 0) {
|
|
|
|
updateGlobalMinCompetitiveScore(scorer);
|
|
|
|
}
|
2022-02-11 13:32:50 +01:00
|
|
|
if (!scoreMode.isExhaustive()
|
2021-10-15 00:03:41 +02:00
|
|
|
&& totalHitsRelation == TotalHits.Relation.EQUAL_TO
|
2022-02-11 13:32:50 +01:00
|
|
|
&& hitsThresholdChecker.isThresholdReached()) {
|
2021-10-15 00:03:41 +02:00
|
|
|
// for the first time hitsThreshold is reached, notify comparator about this
|
|
|
|
comparator.setHitsThresholdReached();
|
|
|
|
totalHitsRelation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean thresholdCheck(int doc) throws IOException {
|
|
|
|
if (collectedAllCompetitiveHits || reverseMul * comparator.compareBottom(doc) <= 0) {
|
|
|
|
// since docs are visited in doc Id order, if compare is 0, it means
|
|
|
|
// this document is largest than anything else in the queue, and
|
|
|
|
// therefore not competitive.
|
|
|
|
if (searchSortPartOfIndexSort) {
|
2022-02-11 13:32:50 +01:00
|
|
|
if (hitsThresholdChecker.isThresholdReached()) {
|
2021-10-15 00:03:41 +02:00
|
|
|
totalHitsRelation = Relation.GREATER_THAN_OR_EQUAL_TO;
|
|
|
|
throw new CollectionTerminatedException();
|
|
|
|
} else {
|
|
|
|
collectedAllCompetitiveHits = true;
|
|
|
|
}
|
|
|
|
} else if (totalHitsRelation == TotalHits.Relation.EQUAL_TO) {
|
|
|
|
// we can start setting the min competitive score if the
|
|
|
|
// threshold is reached for the first time here.
|
|
|
|
updateMinCompetitiveScore(scorer);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
void collectCompetitiveHit(int doc) throws IOException {
|
|
|
|
// This hit is competitive - replace bottom element in queue & adjustTop
|
|
|
|
comparator.copy(pq.top().slot(), doc);
|
|
|
|
updateBottom(doc);
|
|
|
|
comparator.setBottom(pq.top().slot());
|
|
|
|
updateMinCompetitiveScore(scorer);
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
void collectAnyHit(int doc, int hitsCollected) throws IOException {
|
|
|
|
// Startup transient: queue hasn't gathered numHits yet
|
|
|
|
int slot = hitsCollected - 1;
|
|
|
|
// Copy hit into queue
|
|
|
|
comparator.copy(slot, doc);
|
|
|
|
add(slot, doc);
|
|
|
|
if (queueFull) {
|
|
|
|
comparator.setBottom(pq.top().slot());
|
|
|
|
updateMinCompetitiveScore(scorer);
|
|
|
|
}
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setScorer(Scorable scorer) throws IOException {
|
|
|
|
this.scorer = scorer;
|
2021-10-15 00:03:41 +02:00
|
|
|
comparator.setScorer(scorer);
|
|
|
|
minCompetitiveScore = 0f;
|
|
|
|
updateMinCompetitiveScore(scorer);
|
|
|
|
if (minScoreAcc != null) {
|
|
|
|
updateGlobalMinCompetitiveScore(scorer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DocIdSetIterator competitiveIterator() throws IOException {
|
|
|
|
return comparator.competitiveIterator();
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
static boolean canEarlyTerminate(Sort searchSort, Sort indexSort) {
|
|
|
|
return canEarlyTerminateOnDocId(searchSort) || canEarlyTerminateOnPrefix(searchSort, indexSort);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean canEarlyTerminateOnDocId(Sort searchSort) {
|
|
|
|
final SortField[] fields1 = searchSort.getSort();
|
|
|
|
return SortField.FIELD_DOC.equals(fields1[0]);
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
private static boolean canEarlyTerminateOnPrefix(Sort searchSort, Sort indexSort) {
|
|
|
|
if (indexSort != null) {
|
|
|
|
final SortField[] fields1 = searchSort.getSort();
|
|
|
|
final SortField[] fields2 = indexSort.getSort();
|
|
|
|
// early termination is possible if fields1 is a prefix of fields2
|
|
|
|
if (fields1.length > fields2.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Arrays.asList(fields1).equals(Arrays.asList(fields2).subList(0, fields1.length));
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implements a TopFieldCollector over one SortField criteria, with tracking
|
|
|
|
* document scores and maxScore.
|
|
|
|
*/
|
2022-04-06 02:41:32 +02:00
|
|
|
private static class SimpleFieldCollector extends HugePqFullFieldDocCollector {
|
2021-10-15 00:03:41 +02:00
|
|
|
final Sort sort;
|
|
|
|
final PriorityQueue<LLSlotDoc> queue;
|
|
|
|
private final FieldValueHitQueue fieldValueHitQueue;
|
|
|
|
|
|
|
|
public SimpleFieldCollector(
|
|
|
|
Sort sort,
|
2022-04-06 02:41:32 +02:00
|
|
|
HugePqPriorityQueue<LLSlotDoc> queue,
|
2021-10-15 00:03:41 +02:00
|
|
|
FieldValueHitQueue fieldValueHitQueue,
|
2021-10-15 22:03:53 +02:00
|
|
|
long numHits,
|
2021-10-15 00:03:41 +02:00
|
|
|
HitsThresholdChecker hitsThresholdChecker,
|
|
|
|
MaxScoreAccumulator minScoreAcc) {
|
|
|
|
super(queue, fieldValueHitQueue, numHits, hitsThresholdChecker, sort.needsScores(), minScoreAcc);
|
|
|
|
this.sort = sort;
|
|
|
|
this.queue = queue;
|
|
|
|
this.fieldValueHitQueue = fieldValueHitQueue;
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-10-15 00:03:41 +02:00
|
|
|
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
|
2021-10-14 23:04:34 +02:00
|
|
|
docBase = context.docBase;
|
|
|
|
|
2022-02-11 13:32:50 +01:00
|
|
|
return new TopFieldLeafCollector(fieldValueHitQueue, sort, context) {
|
2021-10-14 23:04:34 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void collect(int doc) throws IOException {
|
2021-10-15 00:03:41 +02:00
|
|
|
countHit(doc);
|
|
|
|
if (queueFull) {
|
|
|
|
if (thresholdCheck(doc)) {
|
2021-10-14 23:04:34 +02:00
|
|
|
return;
|
|
|
|
}
|
2021-10-15 00:03:41 +02:00
|
|
|
collectCompetitiveHit(doc);
|
2021-10-14 23:04:34 +02:00
|
|
|
} else {
|
2021-10-15 00:03:41 +02:00
|
|
|
collectAnyHit(doc, totalHits);
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
@Override
|
|
|
|
public ResourceIterable<LLFieldDoc> mapResults(ResourceIterable<LLSlotDoc> it) {
|
|
|
|
return new ResourceIterable<>() {
|
2021-12-18 21:01:14 +01:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
it.close();
|
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
@Override
|
|
|
|
public Flux<LLFieldDoc> iterate() {
|
|
|
|
return it.iterate().map(fieldValueHitQueue::fillFields);
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
@Override
|
|
|
|
public Flux<LLFieldDoc> iterate(long skips) {
|
|
|
|
return it.iterate(skips).map(fieldValueHitQueue::fillFields);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:03:53 +02:00
|
|
|
final long numHits;
|
2021-10-15 00:03:41 +02:00
|
|
|
final HitsThresholdChecker hitsThresholdChecker;
|
|
|
|
final FieldComparator<?> firstComparator;
|
|
|
|
final boolean canSetMinScore;
|
|
|
|
|
|
|
|
Boolean searchSortPartOfIndexSort = null; // shows if Search Sort if a part of the Index Sort
|
|
|
|
|
|
|
|
// an accumulator that maintains the maximum of the segment's minimum competitive scores
|
|
|
|
final MaxScoreAccumulator minScoreAcc;
|
|
|
|
// the current local minimum competitive score already propagated to the underlying scorer
|
|
|
|
float minCompetitiveScore;
|
|
|
|
|
|
|
|
final int numComparators;
|
|
|
|
boolean queueFull;
|
|
|
|
int docBase;
|
|
|
|
final boolean needsScores;
|
|
|
|
final ScoreMode scoreMode;
|
|
|
|
|
|
|
|
// Declaring the constructor private prevents extending this class by anyone
|
|
|
|
// else. Note that the class cannot be final since it's extended by the
|
|
|
|
// internal versions. If someone will define a constructor with any other
|
|
|
|
// visibility, then anyone will be able to extend the class, which is not what
|
|
|
|
// we want.
|
2022-04-06 02:41:32 +02:00
|
|
|
private HugePqFullFieldDocCollector(
|
|
|
|
HugePqPriorityQueue<LLSlotDoc> pq,
|
2021-10-15 00:03:41 +02:00
|
|
|
FieldValueHitQueue fieldValueHitQueue,
|
2021-10-15 22:03:53 +02:00
|
|
|
long numHits,
|
2021-10-14 23:04:34 +02:00
|
|
|
HitsThresholdChecker hitsThresholdChecker,
|
2021-10-15 00:03:41 +02:00
|
|
|
boolean needsScores,
|
2021-10-14 23:04:34 +02:00
|
|
|
MaxScoreAccumulator minScoreAcc) {
|
2021-10-15 00:03:41 +02:00
|
|
|
super(pq);
|
|
|
|
this.needsScores = needsScores;
|
|
|
|
this.numHits = numHits;
|
|
|
|
this.hitsThresholdChecker = hitsThresholdChecker;
|
|
|
|
this.numComparators = fieldValueHitQueue.getComparators().length;
|
|
|
|
this.firstComparator = fieldValueHitQueue.getComparators()[0];
|
|
|
|
int reverseMul = fieldValueHitQueue.getReverseMul()[0];
|
|
|
|
|
|
|
|
if (firstComparator.getClass().equals(FieldComparator.RelevanceComparator.class)
|
|
|
|
&& reverseMul == 1 // if the natural sort is preserved (sort by descending relevance)
|
2022-02-11 13:32:50 +01:00
|
|
|
&& hitsThresholdChecker.getHitsThreshold() != Integer.MAX_VALUE) {
|
2021-10-15 00:03:41 +02:00
|
|
|
scoreMode = ScoreMode.TOP_SCORES;
|
|
|
|
canSetMinScore = true;
|
|
|
|
} else {
|
|
|
|
canSetMinScore = false;
|
2022-02-11 13:32:50 +01:00
|
|
|
if (hitsThresholdChecker.getHitsThreshold() != Integer.MAX_VALUE) {
|
2021-10-15 00:03:41 +02:00
|
|
|
scoreMode = needsScores ? ScoreMode.TOP_DOCS_WITH_SCORES : ScoreMode.TOP_DOCS;
|
|
|
|
} else {
|
|
|
|
scoreMode = needsScores ? ScoreMode.COMPLETE : ScoreMode.COMPLETE_NO_SCORES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.minScoreAcc = minScoreAcc;
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
@Override
|
|
|
|
public ScoreMode scoreMode() {
|
|
|
|
return scoreMode;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void updateGlobalMinCompetitiveScore(Scorable scorer) throws IOException {
|
|
|
|
assert minScoreAcc != null;
|
2022-02-11 13:32:50 +01:00
|
|
|
if (canSetMinScore && hitsThresholdChecker.isThresholdReached()) {
|
2021-10-15 00:03:41 +02:00
|
|
|
// we can start checking the global maximum score even
|
|
|
|
// if the local queue is not full because the threshold
|
|
|
|
// is reached.
|
|
|
|
MaxScoreAccumulator.DocAndScore maxMinScore = minScoreAcc.get();
|
|
|
|
if (maxMinScore != null && maxMinScore.score > minCompetitiveScore) {
|
|
|
|
scorer.setMinCompetitiveScore(maxMinScore.score);
|
|
|
|
minCompetitiveScore = maxMinScore.score;
|
|
|
|
totalHitsRelation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
2021-10-15 00:03:41 +02:00
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
protected void updateMinCompetitiveScore(Scorable scorer) throws IOException {
|
2022-02-11 13:32:50 +01:00
|
|
|
if (canSetMinScore && queueFull && hitsThresholdChecker.isThresholdReached()) {
|
2021-10-15 00:03:41 +02:00
|
|
|
assert pq.top() != null;
|
|
|
|
float minScore = (float) firstComparator.value(pq.top().slot());
|
|
|
|
if (minScore > minCompetitiveScore) {
|
|
|
|
scorer.setMinCompetitiveScore(minScore);
|
|
|
|
minCompetitiveScore = minScore;
|
|
|
|
totalHitsRelation = TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO;
|
|
|
|
if (minScoreAcc != null) {
|
|
|
|
minScoreAcc.accumulate(pq.top().doc(), minScore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-04-06 02:41:32 +02:00
|
|
|
* Creates a new {@link HugePqFullFieldDocCollector} from the given arguments.
|
2021-10-15 00:03:41 +02:00
|
|
|
*
|
|
|
|
* <p><b>NOTE</b>: The instances returned by this method pre-allocate a full array of length
|
|
|
|
* <code>numHits</code>.
|
|
|
|
*
|
|
|
|
* @param sort the sort criteria (SortFields).
|
|
|
|
* @param numHits the number of results to collect.
|
|
|
|
* @param totalHitsThreshold the number of docs to count accurately. If the query matches more
|
|
|
|
* than {@code totalHitsThreshold} hits then its hit count will be a lower bound. On the other
|
|
|
|
* hand if the query matches less than or exactly {@code totalHitsThreshold} hits then the hit
|
|
|
|
* count of the result will be accurate. {@link Integer#MAX_VALUE} may be used to make the hit
|
|
|
|
* count accurate, but this will also make query processing slower.
|
2022-04-06 02:41:32 +02:00
|
|
|
* @return a {@link HugePqFullFieldDocCollector} instance which will sort the results by the sort criteria.
|
2021-10-15 00:03:41 +02:00
|
|
|
*/
|
2022-04-06 02:41:32 +02:00
|
|
|
public static HugePqFullFieldDocCollector create(LLTempHugePqEnv env, Sort sort, int numHits, int totalHitsThreshold) {
|
2021-10-15 00:03:41 +02:00
|
|
|
if (totalHitsThreshold < 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"totalHitsThreshold must be >= 0, got " + totalHitsThreshold);
|
|
|
|
}
|
|
|
|
|
|
|
|
return create(
|
|
|
|
env,
|
|
|
|
sort,
|
|
|
|
numHits,
|
|
|
|
HitsThresholdChecker.create(Math.max(totalHitsThreshold, numHits)),
|
|
|
|
null /* bottomValueChecker */);
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
/**
|
|
|
|
* Same as above with additional parameters to allow passing in the threshold checker and the max
|
|
|
|
* score accumulator.
|
|
|
|
*/
|
2022-04-06 02:41:32 +02:00
|
|
|
static HugePqFullFieldDocCollector create(
|
|
|
|
LLTempHugePqEnv env,
|
2021-10-15 00:03:41 +02:00
|
|
|
Sort sort,
|
|
|
|
int numHits,
|
2021-10-14 23:04:34 +02:00
|
|
|
HitsThresholdChecker hitsThresholdChecker,
|
|
|
|
MaxScoreAccumulator minScoreAcc) {
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
if (sort.getSort().length == 0) {
|
|
|
|
throw new IllegalArgumentException("Sort must contain at least one field");
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
if (numHits <= 0) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
"numHits must be > 0; please use TotalHitCountCollector if you just need the total hit count");
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
if (hitsThresholdChecker == null) {
|
|
|
|
throw new IllegalArgumentException("hitsThresholdChecker should not be null");
|
|
|
|
}
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 22:03:53 +02:00
|
|
|
var fieldValueHitQueue = new LLSlotDocCodec(env, numHits, sort.getSort());
|
2022-04-06 02:41:32 +02:00
|
|
|
var queue = new HugePqPriorityQueue<>(env, fieldValueHitQueue);
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
// inform a comparator that sort is based on this single field
|
|
|
|
// to enable some optimizations for skipping over non-competitive documents
|
|
|
|
// We can't set single sort when the `after` parameter is non-null as it's
|
|
|
|
// an implicit sort over the document id.
|
|
|
|
if (fieldValueHitQueue.getComparators().length == 1) {
|
|
|
|
fieldValueHitQueue.getComparators()[0].setSingleSort();
|
|
|
|
}
|
|
|
|
return new SimpleFieldCollector(sort, queue, fieldValueHitQueue, numHits, hitsThresholdChecker, minScoreAcc);
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
/**
|
|
|
|
* Create a CollectorManager which uses a shared hit counter to maintain number of hits and a
|
|
|
|
* shared {@link MaxScoreAccumulator} to propagate the minimum score accross segments if the
|
|
|
|
* primary sort is by relevancy.
|
|
|
|
*/
|
2022-04-06 02:41:32 +02:00
|
|
|
public static CollectorManager<HugePqFullFieldDocCollector, FullFieldDocs<LLFieldDoc>> createSharedManager(
|
|
|
|
LLTempHugePqEnv env, Sort sort, int numHits, long totalHitsThreshold) {
|
2021-10-14 23:04:34 +02:00
|
|
|
return new CollectorManager<>() {
|
|
|
|
|
2022-02-11 13:32:50 +01:00
|
|
|
private final HitsThresholdChecker hitsThresholdChecker;
|
|
|
|
|
|
|
|
{
|
|
|
|
if (totalHitsThreshold < Integer.MAX_VALUE) {
|
|
|
|
hitsThresholdChecker = HitsThresholdChecker.createShared(Math.max((int) totalHitsThreshold, numHits));
|
|
|
|
} else {
|
|
|
|
hitsThresholdChecker = HitsThresholdChecker.createShared(Integer.MAX_VALUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 23:04:34 +02:00
|
|
|
private final MaxScoreAccumulator minScoreAcc = new MaxScoreAccumulator();
|
|
|
|
|
|
|
|
@Override
|
2022-04-06 02:41:32 +02:00
|
|
|
public HugePqFullFieldDocCollector newCollector() {
|
2021-10-15 00:03:41 +02:00
|
|
|
return create(env, sort, numHits, hitsThresholdChecker, minScoreAcc);
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2022-04-06 02:41:32 +02:00
|
|
|
public FullFieldDocs<LLFieldDoc> reduce(Collection<HugePqFullFieldDocCollector> collectors) {
|
2021-10-15 00:03:41 +02:00
|
|
|
return reduceShared(sort, collectors);
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-04-06 02:41:32 +02:00
|
|
|
private static FullFieldDocs<LLFieldDoc> reduceShared(Sort sort, Collection<HugePqFullFieldDocCollector> collectors) {
|
2021-10-14 23:04:34 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2021-10-15 00:03:41 +02:00
|
|
|
final FullDocs<LLFieldDoc>[] fullDocs = new FullDocs[collectors.size()];
|
2021-10-14 23:04:34 +02:00
|
|
|
int i = 0;
|
2021-10-15 00:03:41 +02:00
|
|
|
for (var collector : collectors) {
|
2021-10-14 23:04:34 +02:00
|
|
|
fullDocs[i++] = collector.fullDocs();
|
|
|
|
}
|
2021-10-15 00:03:41 +02:00
|
|
|
return (FullFieldDocs<LLFieldDoc>) FullDocs.merge(sort, fullDocs);
|
|
|
|
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
final void add(int slot, int doc) {
|
|
|
|
pq.add(new LLSlotDoc(docBase + doc, Float.NaN, -1, slot));
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
// The queue is full either when totalHits == numHits (in SimpleFieldCollector), in which case
|
|
|
|
// slot = totalHits - 1, or when hitsCollected == numHits (in PagingFieldCollector this is hits
|
|
|
|
// on the current page) and slot = hitsCollected - 1.
|
|
|
|
assert slot < numHits;
|
|
|
|
queueFull = slot == numHits - 1;
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
//todo: check if this part is efficient and not redundant
|
|
|
|
final void updateBottom(int doc) {
|
|
|
|
// bottom.score is already set to Float.NaN in add().
|
|
|
|
var bottom = pq.top();
|
|
|
|
pq.replaceTop(new LLSlotDoc(docBase + doc, bottom.score(), bottom.shardIndex(), bottom.slot()));
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
/*
|
|
|
|
* Only the following callback methods need to be overridden since
|
|
|
|
* topDocs(int, int) calls them to return the results.
|
|
|
|
*/
|
2021-10-14 23:04:34 +02:00
|
|
|
|
2021-10-15 00:03:41 +02:00
|
|
|
/** Return whether collection terminated early. */
|
|
|
|
public boolean isEarlyTerminated() {
|
|
|
|
return totalHitsRelation == Relation.GREATER_THAN_OR_EQUAL_TO;
|
2021-10-14 23:04:34 +02:00
|
|
|
}
|
2021-12-18 21:01:14 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
this.pq.close();
|
|
|
|
if (this.firstComparator instanceof SafeCloseable closeable) {
|
|
|
|
closeable.close();
|
|
|
|
}
|
|
|
|
super.close();
|
|
|
|
}
|
2021-10-15 00:03:41 +02:00
|
|
|
}
|