Skip to content

Commit 806abee

Browse files
authored
Optimize DLS bitset building for matchAll query (#81030)
The PR avoids creating Weight and Scorer and stepping through docIterator when building DLS bitSet for an effective matchAll query. Instead it returns a MatchAllRoleBitSet directly after query rewritten for this scenario. Resolves: #80904
1 parent 537f371 commit 806abee

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCache.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import org.apache.lucene.index.IndexReaderContext;
1414
import org.apache.lucene.index.LeafReaderContext;
1515
import org.apache.lucene.index.ReaderUtil;
16+
import org.apache.lucene.search.ConstantScoreQuery;
1617
import org.apache.lucene.search.DocIdSetIterator;
1718
import org.apache.lucene.search.IndexSearcher;
19+
import org.apache.lucene.search.MatchAllDocsQuery;
1820
import org.apache.lucene.search.Query;
1921
import org.apache.lucene.search.ScoreMode;
2022
import org.apache.lucene.search.Scorer;
@@ -273,7 +275,11 @@ private BitSet computeBitSet(Query query, LeafReaderContext context) throws IOEx
273275
final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(context);
274276
final IndexSearcher searcher = new IndexSearcher(topLevelContext);
275277
searcher.setQueryCache(null);
276-
final Weight weight = searcher.createWeight(searcher.rewrite(query), ScoreMode.COMPLETE_NO_SCORES, 1f);
278+
final Query rewrittenQuery = searcher.rewrite(query);
279+
if (isEffectiveMatchAllDocsQuery(rewrittenQuery)) {
280+
return new MatchAllRoleBitSet(context.reader().maxDoc());
281+
}
282+
final Weight weight = searcher.createWeight(rewrittenQuery, ScoreMode.COMPLETE_NO_SCORES, 1f);
277283
final Scorer s = weight.scorer(context);
278284
if (s == null) {
279285
return null;
@@ -282,6 +288,17 @@ private BitSet computeBitSet(Query query, LeafReaderContext context) throws IOEx
282288
}
283289
}
284290

291+
// Package private for testing
292+
static boolean isEffectiveMatchAllDocsQuery(Query rewrittenQuery) {
293+
if (rewrittenQuery instanceof ConstantScoreQuery && ((ConstantScoreQuery) rewrittenQuery).getQuery() instanceof MatchAllDocsQuery) {
294+
return true;
295+
}
296+
if (rewrittenQuery instanceof MatchAllDocsQuery) {
297+
return true;
298+
}
299+
return false;
300+
}
301+
285302
private void maybeLogCacheFullWarning() {
286303
final long nextLogTime = cacheFullWarningTime.get();
287304
final long now = System.currentTimeMillis();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/accesscontrol/DocumentSubsetBitsetCacheTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
import org.apache.lucene.index.IndexWriterConfig;
2020
import org.apache.lucene.index.LeafReaderContext;
2121
import org.apache.lucene.index.NoMergePolicy;
22+
import org.apache.lucene.index.Term;
23+
import org.apache.lucene.search.ConstantScoreQuery;
2224
import org.apache.lucene.search.DocIdSetIterator;
2325
import org.apache.lucene.search.IndexSearcher;
26+
import org.apache.lucene.search.MatchAllDocsQuery;
2427
import org.apache.lucene.search.Query;
28+
import org.apache.lucene.search.TermQuery;
2529
import org.apache.lucene.store.Directory;
2630
import org.apache.lucene.util.BitSet;
2731
import org.apache.lucene.util.BitSetIterator;
@@ -515,6 +519,12 @@ public void testMatchAllRoleBitSet() throws Exception {
515519
}
516520
}
517521

522+
public void testEquivalentMatchAllDocsQuery() {
523+
assertTrue(DocumentSubsetBitsetCache.isEffectiveMatchAllDocsQuery(new MatchAllDocsQuery()));
524+
assertTrue(DocumentSubsetBitsetCache.isEffectiveMatchAllDocsQuery(new ConstantScoreQuery(new MatchAllDocsQuery())));
525+
assertFalse(DocumentSubsetBitsetCache.isEffectiveMatchAllDocsQuery(new TermQuery(new Term("term"))));
526+
}
527+
518528
private void runTestOnIndex(CheckedBiConsumer<SearchExecutionContext, LeafReaderContext, Exception> body) throws Exception {
519529
runTestOnIndices(1, ctx -> {
520530
final TestIndexContext indexContext = ctx.get(0);

0 commit comments

Comments
 (0)