-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Allow efficient can_match phases on frozen indices #35431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a62276c
Allow efficient can_match phases on frozen indices
s1monw fcf8dea
apply review comments
s1monw aaeb52a
fix checkstyle
s1monw f6cee87
Merge branch 'master' into issues/34352/can_match
s1monw 43f5806
apply more feedback
s1monw f724d27
Merge branch 'master' into issues/34352/can_match
s1monw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
268 changes: 268 additions & 0 deletions
268
...ugin/core/src/main/java/org/elasticsearch/index/engine/RewriteCachingDirectoryReader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
| package org.elasticsearch.index.engine; | ||
|
|
||
| import org.apache.lucene.index.BinaryDocValues; | ||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.FieldInfo; | ||
| import org.apache.lucene.index.FieldInfos; | ||
| import org.apache.lucene.index.Fields; | ||
| import org.apache.lucene.index.IndexCommit; | ||
| import org.apache.lucene.index.IndexWriter; | ||
| import org.apache.lucene.index.LeafMetaData; | ||
| import org.apache.lucene.index.LeafReader; | ||
| import org.apache.lucene.index.LeafReaderContext; | ||
| import org.apache.lucene.index.NumericDocValues; | ||
| import org.apache.lucene.index.PointValues; | ||
| import org.apache.lucene.index.SortedDocValues; | ||
| import org.apache.lucene.index.SortedNumericDocValues; | ||
| import org.apache.lucene.index.SortedSetDocValues; | ||
| import org.apache.lucene.index.StoredFieldVisitor; | ||
| import org.apache.lucene.index.Terms; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.apache.lucene.util.Bits; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * This special DirectoryReader is used to handle can_match requests against frozen indices. | ||
| * It' caches all relevant point value data for every point value field ie. min/max packed values etc. | ||
| * to hold enough information to rewrite a date range query and make a decisions if an index can match or not. | ||
| * This allows frozen indices to be searched with wildcards in a very efficient way without opening a reader on them. | ||
| */ | ||
| final class RewriteCachingDirectoryReader extends DirectoryReader { | ||
|
|
||
| RewriteCachingDirectoryReader(Directory directory, List<LeafReaderContext> segmentReaders) throws | ||
| IOException { | ||
| super(directory, wrap(segmentReaders)); | ||
| } | ||
|
|
||
| private static LeafReader[] wrap(List<LeafReaderContext> readers) throws IOException { | ||
| LeafReader[] wrapped = new LeafReader[readers.size()]; | ||
| int i = 0; | ||
| for (LeafReaderContext ctx : readers) { | ||
| LeafReader wrap = new RewriteCachingLeafReader(ctx.reader()); | ||
| assert wrap != null; | ||
| wrapped[i++] = wrap; | ||
| } | ||
| return wrapped; | ||
| } | ||
|
|
||
| @Override | ||
| protected DirectoryReader doOpenIfChanged() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected DirectoryReader doOpenIfChanged(IndexCommit commit) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected DirectoryReader doOpenIfChanged(IndexWriter writer, boolean applyAllDeletes) { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public long getVersion() { | ||
| return 0; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isCurrent() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public IndexCommit getIndexCommit() { | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| protected void doClose() { | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getReaderCacheHelper() { | ||
| return null; | ||
s1monw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // except of a couple of selected methods everything else will | ||
| // throw a UOE which causes a can_match phase to just move to the actual phase | ||
| // later such that we never false exclude a shard if something else is used to rewrite. | ||
| private static final class RewriteCachingLeafReader extends LeafReader { | ||
|
|
||
| private final int maxDoc; | ||
| private final int numDocs; | ||
| private final Map<String, PointValues> pointValuesMap; | ||
| private final FieldInfos fieldInfos; | ||
|
|
||
| private RewriteCachingLeafReader(LeafReader original) throws IOException { | ||
| this.maxDoc = original.maxDoc(); | ||
| this.numDocs = original.numDocs(); | ||
| fieldInfos = original.getFieldInfos(); | ||
| Map<String, PointValues> valuesMap = new HashMap<>(); | ||
| for (FieldInfo info : fieldInfos) { | ||
| if (info.getPointIndexDimensionCount() != 0) { | ||
| PointValues pointValues = original.getPointValues(info.name); | ||
| if (pointValues != null) { // might not be in this reader | ||
| byte[] minPackedValue = pointValues.getMinPackedValue(); | ||
| byte[] maxPackedValue = pointValues.getMaxPackedValue(); | ||
| int numDimensions = pointValues.getNumIndexDimensions(); | ||
| int bytesPerDimension = pointValues.getBytesPerDimension(); | ||
| int numDataDimensions = pointValues.getNumDataDimensions(); | ||
| long size = pointValues.size(); | ||
| int docCount = pointValues.getDocCount(); | ||
| valuesMap.put(info.name, new PointValues() { | ||
| @Override | ||
| public void intersect(IntersectVisitor visitor) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public long estimatePointCount(IntersectVisitor visitor) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] getMinPackedValue() { | ||
| return minPackedValue; | ||
| } | ||
|
|
||
| @Override | ||
| public byte[] getMaxPackedValue() { | ||
| return maxPackedValue; | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumDataDimensions() { | ||
| return numDataDimensions; | ||
| } | ||
|
|
||
| @Override | ||
| public int getNumIndexDimensions() { | ||
| return numDimensions; | ||
| } | ||
|
|
||
| @Override | ||
| public int getBytesPerDimension() { | ||
| return bytesPerDimension; | ||
| } | ||
|
|
||
| @Override | ||
| public long size() { | ||
| return size; | ||
| } | ||
|
|
||
| @Override | ||
| public int getDocCount() { | ||
| return docCount; | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| pointValuesMap = valuesMap; | ||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getCoreCacheHelper() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public Terms terms(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public NumericDocValues getNumericDocValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public BinaryDocValues getBinaryDocValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public SortedDocValues getSortedDocValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public SortedNumericDocValues getSortedNumericDocValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public SortedSetDocValues getSortedSetDocValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public NumericDocValues getNormValues(String field) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public FieldInfos getFieldInfos() { | ||
| return fieldInfos; | ||
| } | ||
|
|
||
| @Override | ||
| public Bits getLiveDocs() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public PointValues getPointValues(String field) { | ||
| return pointValuesMap.get(field); | ||
| } | ||
|
|
||
| @Override | ||
| public void checkIntegrity() { | ||
| } | ||
|
|
||
| @Override | ||
| public LeafMetaData getMetaData() { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public Fields getTermVectors(int docID) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| public int numDocs() { | ||
| return numDocs; | ||
| } | ||
|
|
||
| @Override | ||
| public int maxDoc() { | ||
| return maxDoc; | ||
| } | ||
|
|
||
| @Override | ||
| public void document(int docID, StoredFieldVisitor visitor) { | ||
| throw new UnsupportedOperationException(); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doClose() { | ||
| } | ||
|
|
||
| @Override | ||
| public CacheHelper getReaderCacheHelper() { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.