diff --git a/core/src/main/java/org/elasticsearch/index/engine/Engine.java b/core/src/main/java/org/elasticsearch/index/engine/Engine.java index 90218431d0d..69ac57a3b47 100644 --- a/core/src/main/java/org/elasticsearch/index/engine/Engine.java +++ b/core/src/main/java/org/elasticsearch/index/engine/Engine.java @@ -579,12 +579,10 @@ public abstract class Engine implements Closeable { private final String source; private final IndexSearcher searcher; - private final DirectoryReader reader; public Searcher(String source, IndexSearcher searcher) { this.source = source; this.searcher = searcher; - this.reader = (DirectoryReader) searcher.getIndexReader(); } /** @@ -594,8 +592,15 @@ public abstract class Engine implements Closeable { return source; } - public DirectoryReader reader() { - return reader; + public IndexReader reader() { + return searcher.getIndexReader(); + } + + public DirectoryReader getDirectoryReader() { + if (reader() instanceof DirectoryReader) { + return (DirectoryReader) reader(); + } + throw new IllegalStateException("Can't use " + reader().getClass() + " as an directory reader"); } public IndexSearcher searcher() { diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexSearcherWrapper.java b/core/src/main/java/org/elasticsearch/index/shard/IndexSearcherWrapper.java index a0f98b370e0..41352b5c9fd 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexSearcherWrapper.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexSearcherWrapper.java @@ -63,11 +63,11 @@ public class IndexSearcherWrapper { * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search) */ public final Engine.Searcher wrap(EngineConfig engineConfig, Engine.Searcher engineSearcher) throws IOException { - final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.reader()); + final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader()); if (elasticsearchDirectoryReader == null) { throw new IllegalStateException("Can't wrap non elasticsearch directory reader"); } - DirectoryReader reader = wrap(engineSearcher.reader()); + DirectoryReader reader = wrap(engineSearcher.getDirectoryReader()); IndexSearcher innerIndexSearcher = new IndexSearcher(new CacheFriendlyReaderWrapper(reader, elasticsearchDirectoryReader)); innerIndexSearcher.setQueryCache(engineConfig.getQueryCache()); innerIndexSearcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy()); diff --git a/core/src/main/java/org/elasticsearch/indices/IndicesWarmer.java b/core/src/main/java/org/elasticsearch/indices/IndicesWarmer.java index 6d8d407e9bc..4f6f238ef6d 100644 --- a/core/src/main/java/org/elasticsearch/indices/IndicesWarmer.java +++ b/core/src/main/java/org/elasticsearch/indices/IndicesWarmer.java @@ -177,10 +177,14 @@ public final class IndicesWarmer extends AbstractComponent { return searcher; } - public DirectoryReader reader() { + public IndexReader reader() { return searcher.reader(); } + public DirectoryReader getDirectoryReader() { + return searcher.getDirectoryReader(); + } + @Override public String toString() { return "WarmerContext: " + searcher.reader(); diff --git a/core/src/main/java/org/elasticsearch/search/SearchService.java b/core/src/main/java/org/elasticsearch/search/SearchService.java index 57cecfceb8f..e7973494666 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchService.java +++ b/core/src/main/java/org/elasticsearch/search/SearchService.java @@ -1016,7 +1016,7 @@ public class SearchService extends AbstractLifecycleComponent { try { final long start = System.nanoTime(); IndexFieldData.Global ifd = indexFieldDataService.getForField(fieldType); - ifd.loadGlobal(context.reader()); + ifd.loadGlobal(context.getDirectoryReader()); if (indexShard.warmerService().logger().isTraceEnabled()) { indexShard.warmerService().logger().trace("warmed global ordinals for [{}], took [{}]", fieldType.names().fullName(), TimeValue.timeValueNanos(System.nanoTime() - start)); } diff --git a/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java b/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java index 7a3944ffbfd..0a9b860edb7 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java +++ b/core/src/main/java/org/elasticsearch/search/internal/ContextIndexSearcher.java @@ -41,12 +41,12 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable { private AggregatedDfs aggregatedDfs; - private final DirectoryReader directoryReader; + private final Engine.Searcher engineSearcher; public ContextIndexSearcher(SearchContext searchContext, Engine.Searcher searcher) { super(searcher.reader()); in = searcher.searcher(); - directoryReader = searcher.reader(); + engineSearcher = searcher; setSimilarity(searcher.searcher().getSimilarity(true)); setQueryCache(searchContext.getQueryCache()); setQueryCachingPolicy(searchContext.indexShard().getQueryCachingPolicy()); @@ -110,6 +110,6 @@ public class ContextIndexSearcher extends IndexSearcher implements Releasable { } public DirectoryReader getDirectoryReader() { - return directoryReader; + return engineSearcher.getDirectoryReader(); } } diff --git a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index 5f6e1db42b9..2a6150267a5 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -1499,7 +1499,7 @@ public class InternalEngineTests extends ESTestCase { @Test public void testExtractShardId() { try (Engine.Searcher test = this.engine.acquireSearcher("test")) { - ShardId shardId = ShardUtils.extractShardId(test.reader()); + ShardId shardId = ShardUtils.extractShardId(test.getDirectoryReader()); assertNotNull(shardId); assertEquals(shardId, engine.config().getShardId()); } diff --git a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java index 2c6ee40b86e..7dadafb8a0b 100644 --- a/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java +++ b/core/src/test/java/org/elasticsearch/index/engine/ShadowEngineTests.java @@ -855,7 +855,7 @@ public class ShadowEngineTests extends ESTestCase { @Test public void testExtractShardId() { try (Engine.Searcher test = replicaEngine.acquireSearcher("test")) { - ShardId shardId = ShardUtils.extractShardId(test.reader()); + ShardId shardId = ShardUtils.extractShardId(test.getDirectoryReader()); assertNotNull(shardId); assertEquals(shardId, replicaEngine.config().getShardId()); } diff --git a/core/src/test/java/org/elasticsearch/index/shard/IndexSearcherWrapperTests.java b/core/src/test/java/org/elasticsearch/index/shard/IndexSearcherWrapperTests.java index cdc696fea4f..3411f15a359 100644 --- a/core/src/test/java/org/elasticsearch/index/shard/IndexSearcherWrapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/shard/IndexSearcherWrapperTests.java @@ -75,7 +75,7 @@ public class IndexSearcherWrapperTests extends ESTestCase { try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher); assertEquals(1, wrap.reader().getRefCount()); - ElasticsearchDirectoryReader.addReaderCloseListener(wrap.reader(), reader -> { + ElasticsearchDirectoryReader.addReaderCloseListener(wrap.getDirectoryReader(), reader -> { if (reader == open) { count.incrementAndGet(); } @@ -119,7 +119,7 @@ public class IndexSearcherWrapperTests extends ESTestCase { final ConcurrentHashMap cache = new ConcurrentHashMap<>(); try (Engine.Searcher engineSearcher = new Engine.Searcher("foo", searcher)) { try (final Engine.Searcher wrap = wrapper.wrap(ENGINE_CONFIG, engineSearcher)) { - ElasticsearchDirectoryReader.addReaderCloseListener(wrap.reader(), reader -> { + ElasticsearchDirectoryReader.addReaderCloseListener(wrap.getDirectoryReader(), reader -> { cache.remove(reader.getCoreCacheKey()); }); TopDocs search = wrap.searcher().search(new TermQuery(new Term("field", "doc")), 1);