diff --git a/src/main/java/org/elasticsearch/index/cache/bloom/simple/SimpleBloomCache.java b/src/main/java/org/elasticsearch/index/cache/bloom/simple/SimpleBloomCache.java index b2f228167ec..791dc28a961 100644 --- a/src/main/java/org/elasticsearch/index/cache/bloom/simple/SimpleBloomCache.java +++ b/src/main/java/org/elasticsearch/index/cache/bloom/simple/SimpleBloomCache.java @@ -123,7 +123,9 @@ public class SimpleBloomCache extends AbstractIndexComponent implements BloomCac synchronized (creationMutex) { fieldCache = cache.get(reader.getCoreCacheKey()); if (fieldCache == null) { - ((SegmentReader) reader).addCoreClosedListener(this); + if (reader instanceof SegmentReader) { + ((SegmentReader) reader).addCoreClosedListener(this); + } fieldCache = ConcurrentCollections.newConcurrentMap(); cache.put(reader.getCoreCacheKey(), fieldCache); } diff --git a/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java b/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java index 8382463d78f..1995a5a8e57 100644 --- a/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java +++ b/src/main/java/org/elasticsearch/index/cache/field/data/support/AbstractConcurrentMapFieldDataCache.java @@ -112,7 +112,9 @@ public abstract class AbstractConcurrentMapFieldDataCache extends AbstractIndexC fieldDataCache = cache.get(reader.getCoreCacheKey()); if (fieldDataCache == null) { fieldDataCache = buildFieldDataMap(); - ((SegmentReader) reader).addCoreClosedListener(this); + if (reader instanceof SegmentReader) { + ((SegmentReader) reader).addCoreClosedListener(this); + } cache.put(reader.getCoreCacheKey(), fieldDataCache); } } diff --git a/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java b/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java index 8c14b8cd6c9..5f68be5a840 100644 --- a/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java +++ b/src/main/java/org/elasticsearch/index/cache/filter/weighted/WeightedFilterCache.java @@ -166,7 +166,7 @@ public class WeightedFilterCache extends AbstractIndexComponent implements Filte if (cacheValue == null) { if (!cache.seenReaders.containsKey(reader.getCoreCacheKey())) { Boolean previous = cache.seenReaders.putIfAbsent(reader.getCoreCacheKey(), Boolean.TRUE); - if (previous == null) { + if (previous == null && (reader instanceof SegmentReader)) { ((SegmentReader) reader).addCoreClosedListener(cache); cache.seenReadersCount.inc(); } diff --git a/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java b/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java index 1fc669330ad..b8d63799460 100644 --- a/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java +++ b/src/main/java/org/elasticsearch/index/cache/id/simple/SimpleIdCache.java @@ -109,7 +109,9 @@ public class SimpleIdCache extends AbstractIndexComponent implements IdCache, Se continue; } - ((SegmentReader) reader).addCoreClosedListener(this); + if (reader instanceof SegmentReader) { + ((SegmentReader) reader).addCoreClosedListener(this); + } HashMap readerBuilder = new HashMap(); builders.put(reader.getCoreCacheKey(), readerBuilder); diff --git a/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java b/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java index ef3d18ed69f..6ec07fd742b 100644 --- a/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java +++ b/src/main/java/org/elasticsearch/index/percolator/PercolatorExecutor.java @@ -249,7 +249,7 @@ public class PercolatorExecutor extends AbstractIndexComponent { } } - public synchronized void addQuery(String name, Query query) { + private synchronized void addQuery(String name, Query query) { Preconditions.checkArgument(query != null, "query must be provided for percolate request"); this.queries = MapBuilder.newMapBuilder(queries).put(name, query).immutableMap(); } @@ -349,43 +349,46 @@ public class PercolatorExecutor extends AbstractIndexComponent { } final IndexSearcher searcher = memoryIndex.createSearcher(); - List matches = new ArrayList(); - if (request.query() == null) { - Lucene.ExistsCollector collector = new Lucene.ExistsCollector(); - for (Map.Entry entry : queries.entrySet()) { - collector.reset(); + + try { + if (request.query() == null) { + Lucene.ExistsCollector collector = new Lucene.ExistsCollector(); + for (Map.Entry entry : queries.entrySet()) { + collector.reset(); + try { + searcher.search(entry.getValue(), collector); + } catch (IOException e) { + logger.warn("[" + entry.getKey() + "] failed to execute query", e); + } + + if (collector.exists()) { + matches.add(entry.getKey()); + } + } + } else { + IndexService percolatorIndex = indicesService.indexService(PercolatorService.INDEX_NAME); + if (percolatorIndex == null) { + throw new PercolateIndexUnavailable(new Index(PercolatorService.INDEX_NAME)); + } + if (percolatorIndex.numberOfShards() == 0) { + throw new PercolateIndexUnavailable(new Index(PercolatorService.INDEX_NAME)); + } + IndexShard percolatorShard = percolatorIndex.shard(0); + Engine.Searcher percolatorSearcher = percolatorShard.searcher(); try { - searcher.search(entry.getValue(), collector); + percolatorSearcher.searcher().search(request.query(), new QueryCollector(logger, queries, searcher, percolatorIndex, matches)); } catch (IOException e) { - logger.warn("[" + entry.getKey() + "] failed to execute query", e); - } - - if (collector.exists()) { - matches.add(entry.getKey()); + logger.warn("failed to execute", e); + } finally { + percolatorSearcher.release(); } } - } else { - IndexService percolatorIndex = indicesService.indexService(PercolatorService.INDEX_NAME); - if (percolatorIndex == null) { - throw new PercolateIndexUnavailable(new Index(PercolatorService.INDEX_NAME)); - } - if (percolatorIndex.numberOfShards() == 0) { - throw new PercolateIndexUnavailable(new Index(PercolatorService.INDEX_NAME)); - } - IndexShard percolatorShard = percolatorIndex.shard(0); - Engine.Searcher percolatorSearcher = percolatorShard.searcher(); - try { - percolatorSearcher.searcher().search(request.query(), new QueryCollector(logger, queries, searcher, percolatorIndex, matches)); - } catch (IOException e) { - logger.warn("failed to execute", e); - } finally { - percolatorSearcher.release(); - } + } finally { + // explicitly clear the reader, since we can only register on callback on SegmentReader + indexCache.clear(searcher.getIndexReader()); } - indexCache.clear(searcher.getIndexReader()); - return new Response(matches, request.doc().mappersAdded()); } diff --git a/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java b/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java index d132ed556ce..2889d949aa0 100644 --- a/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java +++ b/src/test/java/org/elasticsearch/test/unit/index/percolator/PercolatorExecutorTests.java @@ -36,6 +36,7 @@ import org.elasticsearch.index.cache.IndexCacheModule; import org.elasticsearch.index.engine.IndexEngineModule; import org.elasticsearch.index.mapper.MapperServiceModule; import org.elasticsearch.index.percolator.PercolatorExecutor; +import org.elasticsearch.index.query.FilterBuilders; import org.elasticsearch.index.query.IndexQueryParserModule; import org.elasticsearch.index.settings.IndexSettingsModule; import org.elasticsearch.index.similarity.SimilarityModule; @@ -45,6 +46,7 @@ import org.elasticsearch.threadpool.ThreadPoolModule; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import static org.elasticsearch.index.query.QueryBuilders.constantScoreQuery; import static org.elasticsearch.index.query.QueryBuilders.termQuery; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -60,7 +62,7 @@ public class PercolatorExecutorTests { @BeforeClass public void buildPercolatorService() { Settings settings = ImmutableSettings.settingsBuilder() - .put("index.cache.filter.type", "none") + //.put("index.cache.filter.type", "none") .build(); Index index = new Index("test"); Injector injector = new ModulesBuilder().add( @@ -128,5 +130,13 @@ public class PercolatorExecutorTests { percolate = percolatorExecutor.percolate(new PercolatorExecutor.SourceRequest("type1", source)); assertThat(percolate.matches(), hasSize(1)); assertThat(percolate.matches(), hasItems("test1")); + + // add a range query (cached) + // add a query + percolatorExecutor.addQuery("test1", constantScoreQuery(FilterBuilders.rangeFilter("field2").from("value").includeLower(true))); + + percolate = percolatorExecutor.percolate(new PercolatorExecutor.SourceRequest("type1", source)); + assertThat(percolate.matches(), hasSize(1)); + assertThat(percolate.matches(), hasItem("test1")); } }