Skip to content

Commit 7004d2c

Browse files
committed
Limits num hits when reading lucene changes
1 parent 51caefe commit 7004d2c

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

server/src/main/java/org/elasticsearch/index/engine/LuceneChangesSnapshot.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import org.apache.lucene.index.NumericDocValues;
2626
import org.apache.lucene.index.ReaderUtil;
2727
import org.apache.lucene.index.Term;
28+
import org.apache.lucene.search.BooleanClause;
29+
import org.apache.lucene.search.BooleanQuery;
2830
import org.apache.lucene.search.DocIdSetIterator;
31+
import org.apache.lucene.search.DocValuesFieldExistsQuery;
2932
import org.apache.lucene.search.IndexSearcher;
3033
import org.apache.lucene.search.Query;
3134
import org.apache.lucene.search.Sort;
@@ -154,12 +157,17 @@ private int nextDocId() {
154157
}
155158

156159
private TopDocs searchOperations(IndexSearcher searcher) throws IOException {
157-
final Query rangeQuery = LongPoint.newRangeQuery(SeqNoFieldMapper.NAME, fromSeqNo, toSeqNo);
160+
final Query rangeQuery = new BooleanQuery.Builder()
161+
.add(new DocValuesFieldExistsQuery(SeqNoFieldMapper.PRIMARY_TERM_NAME), BooleanClause.Occur.FILTER)
162+
.add(LongPoint.newRangeQuery(SeqNoFieldMapper.NAME, fromSeqNo, toSeqNo), BooleanClause.Occur.FILTER)
163+
.build();
158164
final Sort sortedBySeqNoThenByTerm = new Sort(
159165
new SortedNumericSortField(SeqNoFieldMapper.NAME, SortField.Type.LONG),
160166
new SortedNumericSortField(SeqNoFieldMapper.PRIMARY_TERM_NAME, SortField.Type.LONG, true)
161167
);
162-
return searcher.search(rangeQuery, Integer.MAX_VALUE, sortedBySeqNoThenByTerm);
168+
// nocommit - limits the number of hits
169+
final long numHits = Math.min((toSeqNo + 1 - fromSeqNo) * 2, Integer.MAX_VALUE - 1);
170+
return searcher.search(rangeQuery, Math.toIntExact(numHits), sortedBySeqNoThenByTerm);
163171
}
164172

165173
private Translog.Operation readDocAsOp(int docID) throws IOException {

0 commit comments

Comments
 (0)