Example usage for org.apache.lucene.search IndexSearcher setQueryCachingPolicy

List of usage examples for org.apache.lucene.search IndexSearcher setQueryCachingPolicy

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher setQueryCachingPolicy.

Prototype

public void setQueryCachingPolicy(QueryCachingPolicy queryCachingPolicy) 

Source Link

Document

Set the QueryCachingPolicy to use for query caching.

Usage

From source file:org.elasticsearch.common.lucene.IndexCacheableQueryTests.java

License:Apache License

public void testCache() throws IOException {
    Directory dir = newDirectory();/*from  w w  w .j  a v a2 s. c om*/
    LRUQueryCache cache = new LRUQueryCache(10000, Long.MAX_VALUE);
    QueryCachingPolicy policy = QueryCachingPolicy.ALWAYS_CACHE;
    RandomIndexWriter writer = new RandomIndexWriter(getRandom(), dir);
    for (int i = 0; i < 10; ++i) {
        writer.addDocument(new Document());
    }

    IndexReader reader = writer.getReader();
    IndexSearcher searcher = newSearcher(reader);
    reader = searcher.getIndexReader(); // reader might be wrapped
    searcher.setQueryCache(cache);
    searcher.setQueryCachingPolicy(policy);

    assertEquals(0, cache.getCacheSize());
    DummyIndexCacheableQuery query = new DummyIndexCacheableQuery();
    searcher.count(query);
    int expectedCacheSize = reader.leaves().size();
    assertEquals(expectedCacheSize, cache.getCacheSize());
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());

    writer.addDocument(new Document());

    IndexReader reader2 = writer.getReader();
    searcher = newSearcher(reader2);
    reader2 = searcher.getIndexReader(); // reader might be wrapped
    searcher.setQueryCache(cache);
    searcher.setQueryCachingPolicy(policy);

    // since the query is only cacheable at the index level, it has to be recomputed on all leaves
    expectedCacheSize += reader2.leaves().size();
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());
    searcher.count(query);
    assertEquals(expectedCacheSize, cache.getCacheSize());

    reader.close();
    reader2.close();
    writer.close();
    assertEquals(0, cache.getCacheSize());
    dir.close();
}

From source file:org.elasticsearch.index.engine.EngineSearcherFactory.java

License:Apache License

@Override
public IndexSearcher newSearcher(IndexReader reader, IndexReader previousReader) throws IOException {
    IndexSearcher searcher = super.newSearcher(reader, previousReader);
    searcher.setQueryCache(engineConfig.getQueryCache());
    searcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy());
    searcher.setSimilarity(engineConfig.getSimilarity());
    return searcher;
}

From source file:org.elasticsearch.index.engine.IndexSearcherWrappingService.java

License:Apache License

/**
 * If there are configured {@link IndexSearcherWrapper} instances, the {@link IndexSearcher} of the provided engine searcher
 * gets wrapped and a new {@link Engine.Searcher} instances is returned, otherwise the provided {@link Engine.Searcher} is returned.
 *
 * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
 *//*  ww w.j ava2 s.  c om*/
public final Engine.Searcher wrap(EngineConfig engineConfig, final Engine.Searcher engineSearcher)
        throws IOException {
    final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader
            .getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
    if (elasticsearchDirectoryReader == null) {
        throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
    }
    if (wrapper == null) {
        return engineSearcher;
    }
    NonClosingReaderWrapper nonClosingReaderWrapper = new NonClosingReaderWrapper(
            engineSearcher.getDirectoryReader());
    DirectoryReader reader = wrapper.wrap(nonClosingReaderWrapper);
    if (reader != nonClosingReaderWrapper) {
        if (reader.getCoreCacheKey() != elasticsearchDirectoryReader.getCoreCacheKey()) {
            throw new IllegalStateException(
                    "wrapped directory reader doesn't delegate IndexReader#getCoreCacheKey, wrappers must override this method and delegate"
                            + " to the original readers core cache key. Wrapped readers can't be used as cache keys since their are used only per request which would lead to subtle bugs");
        }
        if (ElasticsearchDirectoryReader
                .getElasticsearchDirectoryReader(reader) != elasticsearchDirectoryReader) {
            // prevent that somebody wraps with a non-filter reader
            throw new IllegalStateException(
                    "wrapped directory reader hides actual ElasticsearchDirectoryReader but shouldn't");
        }
    }

    final IndexSearcher innerIndexSearcher = new IndexSearcher(reader);
    innerIndexSearcher.setQueryCache(engineConfig.getQueryCache());
    innerIndexSearcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy());
    innerIndexSearcher.setSimilarity(engineConfig.getSimilarity());
    // TODO: Right now IndexSearcher isn't wrapper friendly, when it becomes wrapper friendly we should revise this extension point
    // For example if IndexSearcher#rewrite() is overwritten than also IndexSearcher#createNormalizedWeight needs to be overwritten
    // This needs to be fixed before we can allow the IndexSearcher from Engine to be wrapped multiple times
    final IndexSearcher indexSearcher = wrapper.wrap(engineConfig, innerIndexSearcher);
    if (reader == nonClosingReaderWrapper && indexSearcher == innerIndexSearcher) {
        return engineSearcher;
    } else {
        return new Engine.Searcher(engineSearcher.source(), indexSearcher) {
            @Override
            public void close() throws ElasticsearchException {
                try {
                    reader().close();
                    // we close the reader to make sure wrappers can release resources if needed....
                    // our NonClosingReaderWrapper makes sure that our reader is not closed
                } catch (IOException e) {
                    throw new ElasticsearchException("failed to close reader", e);
                } finally {
                    engineSearcher.close();
                }

            }
        };
    }
}

From source file:org.elasticsearch.index.shard.IndexSearcherWrapper.java

License:Apache License

/**
 * If there are configured {@link IndexSearcherWrapper} instances, the {@link IndexSearcher} of the provided engine searcher
 * gets wrapped and a new {@link Engine.Searcher} instances is returned, otherwise the provided {@link Engine.Searcher} is returned.
 *
 * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
 *//*from  ww  w  . j  a v a  2  s.c om*/
public final Engine.Searcher wrap(Engine.Searcher engineSearcher) throws IOException {
    final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader
            .getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
    if (elasticsearchDirectoryReader == null) {
        throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
    }
    NonClosingReaderWrapper nonClosingReaderWrapper = new NonClosingReaderWrapper(
            engineSearcher.getDirectoryReader());
    DirectoryReader reader = wrap(nonClosingReaderWrapper);
    if (reader != nonClosingReaderWrapper) {
        if (reader.getCoreCacheKey() != elasticsearchDirectoryReader.getCoreCacheKey()) {
            throw new IllegalStateException(
                    "wrapped directory reader doesn't delegate IndexReader#getCoreCacheKey, wrappers must override this method and delegate"
                            + " to the original readers core cache key. Wrapped readers can't be used as cache keys since their are used only per request which would lead to subtle bugs");
        }
        if (ElasticsearchDirectoryReader
                .getElasticsearchDirectoryReader(reader) != elasticsearchDirectoryReader) {
            // prevent that somebody wraps with a non-filter reader
            throw new IllegalStateException(
                    "wrapped directory reader hides actual ElasticsearchDirectoryReader but shouldn't");
        }
    }

    final IndexSearcher origIndexSearcher = engineSearcher.searcher();
    final IndexSearcher innerIndexSearcher = new IndexSearcher(reader);
    innerIndexSearcher.setQueryCache(origIndexSearcher.getQueryCache());
    innerIndexSearcher.setQueryCachingPolicy(origIndexSearcher.getQueryCachingPolicy());
    innerIndexSearcher.setSimilarity(origIndexSearcher.getSimilarity(true));
    // TODO: Right now IndexSearcher isn't wrapper friendly, when it becomes wrapper friendly we should revise this extension point
    // For example if IndexSearcher#rewrite() is overwritten than also IndexSearcher#createNormalizedWeight needs to be overwritten
    // This needs to be fixed before we can allow the IndexSearcher from Engine to be wrapped multiple times
    final IndexSearcher indexSearcher = wrap(innerIndexSearcher);
    if (reader == nonClosingReaderWrapper && indexSearcher == innerIndexSearcher) {
        return engineSearcher;
    } else {
        return new Engine.Searcher(engineSearcher.source(), indexSearcher) {
            @Override
            public void close() throws ElasticsearchException {
                try {
                    reader().close();
                    // we close the reader to make sure wrappers can release resources if needed....
                    // our NonClosingReaderWrapper makes sure that our reader is not closed
                } catch (IOException e) {
                    throw new ElasticsearchException("failed to close reader", e);
                } finally {
                    engineSearcher.close();
                }

            }
        };
    }
}

From source file:org.elasticsearch.indices.cache.query.IndicesQueryCacheTests.java

License:Apache License

public void testBasics() throws IOException {
    Directory dir = newDirectory();/*  w  ww .  ja  va  2  s. c o  m*/
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
    w.addDocument(new Document());
    DirectoryReader r = DirectoryReader.open(w, false);
    w.close();
    ShardId shard = new ShardId(new Index("index"), 0);
    r = ElasticsearchDirectoryReader.wrap(r, shard);
    IndexSearcher s = new IndexSearcher(r);
    s.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT, 10).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s.setQueryCache(cache);

    QueryCacheStats stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(0L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(0L, stats.getMissCount());

    assertEquals(1, s.count(new DummyQuery(0)));

    stats = cache.getStats(shard);
    assertEquals(1L, stats.getCacheSize());
    assertEquals(1L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(1L, stats.getMissCount());

    for (int i = 1; i < 20; ++i) {
        assertEquals(1, s.count(new DummyQuery(i)));
    }

    stats = cache.getStats(shard);
    assertEquals(10L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    s.count(new DummyQuery(10));

    stats = cache.getStats(shard);
    assertEquals(10L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(1L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    IOUtils.close(r, dir);

    // got emptied, but no changes to other metrics
    stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(1L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    cache.onClose(shard);

    // forgot everything
    stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(0L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(0L, stats.getMissCount());

    cache.close(); // this triggers some assertions
}

From source file:org.elasticsearch.indices.cache.query.IndicesQueryCacheTests.java

License:Apache License

public void testTwoShards() throws IOException {
    Directory dir1 = newDirectory();//  w  w w.ja  v a  2 s . c om
    IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig());
    w1.addDocument(new Document());
    DirectoryReader r1 = DirectoryReader.open(w1, false);
    w1.close();
    ShardId shard1 = new ShardId(new Index("index"), 0);
    r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
    IndexSearcher s1 = new IndexSearcher(r1);
    s1.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Directory dir2 = newDirectory();
    IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
    w2.addDocument(new Document());
    DirectoryReader r2 = DirectoryReader.open(w2, false);
    w2.close();
    ShardId shard2 = new ShardId(new Index("index"), 1);
    r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
    IndexSearcher s2 = new IndexSearcher(r2);
    s2.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT, 10).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s1.setQueryCache(cache);
    s2.setQueryCache(cache);

    assertEquals(1, s1.count(new DummyQuery(0)));

    QueryCacheStats stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    QueryCacheStats stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());

    assertEquals(1, s2.count(new DummyQuery(0)));

    stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(1L, stats2.getCacheSize());
    assertEquals(1L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(1L, stats2.getMissCount());

    for (int i = 0; i < 20; ++i) {
        assertEquals(1, s2.count(new DummyQuery(i)));
    }

    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize()); // evicted
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    IOUtils.close(r1, dir1);

    // no changes
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    cache.onClose(shard1);

    // forgot everything about shard1
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    IOUtils.close(r2, dir2);
    cache.onClose(shard2);

    // forgot everything about shard2
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());

    cache.close(); // this triggers some assertions
}

From source file:org.elasticsearch.indices.cache.query.IndicesQueryCacheTests.java

License:Apache License

public void testStatsOnEviction() throws IOException {
    Directory dir1 = newDirectory();/*from   w  ww  .  ja v a 2s . co  m*/
    IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig());
    w1.addDocument(new Document());
    DirectoryReader r1 = DirectoryReader.open(w1, false);
    w1.close();
    ShardId shard1 = new ShardId(new Index("index"), 0);
    r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
    IndexSearcher s1 = new IndexSearcher(r1);
    s1.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Directory dir2 = newDirectory();
    IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
    w2.addDocument(new Document());
    DirectoryReader r2 = DirectoryReader.open(w2, false);
    w2.close();
    ShardId shard2 = new ShardId(new Index("index"), 1);
    r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
    IndexSearcher s2 = new IndexSearcher(r2);
    s2.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT, 10).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s1.setQueryCache(cache);
    s2.setQueryCache(cache);

    assertEquals(1, s1.count(new DummyQuery(0)));

    for (int i = 1; i <= 20; ++i) {
        assertEquals(1, s2.count(new DummyQuery(i)));
    }

    QueryCacheStats stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());

    // this used to fail because we were evicting an empty cache on
    // the segment from r1
    IOUtils.close(r1, dir1);
    cache.onClose(shard1);

    IOUtils.close(r2, dir2);
    cache.onClose(shard2);

    cache.close(); // this triggers some assertions
}

From source file:org.elasticsearch.indices.IndicesQueryCacheTests.java

License:Apache License

public void testBasics() throws IOException {
    Directory dir = newDirectory();//from  w  w w .  jav  a2s.  c om
    IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
    w.addDocument(new Document());
    DirectoryReader r = DirectoryReader.open(w);
    w.close();
    ShardId shard = new ShardId("index", "_na_", 0);
    r = ElasticsearchDirectoryReader.wrap(r, shard);
    IndexSearcher s = new IndexSearcher(r);
    s.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
            .put(IndicesQueryCache.INDICES_QUERIES_CACHE_ALL_SEGMENTS_SETTING.getKey(), true).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s.setQueryCache(cache);

    QueryCacheStats stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(0L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(0L, stats.getMissCount());

    assertEquals(1, s.count(new DummyQuery(0)));

    stats = cache.getStats(shard);
    assertEquals(1L, stats.getCacheSize());
    assertEquals(1L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(1L, stats.getMissCount());

    for (int i = 1; i < 20; ++i) {
        assertEquals(1, s.count(new DummyQuery(i)));
    }

    stats = cache.getStats(shard);
    assertEquals(10L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    s.count(new DummyQuery(10));

    stats = cache.getStats(shard);
    assertEquals(10L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(1L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    IOUtils.close(r, dir);

    // got emptied, but no changes to other metrics
    stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(20L, stats.getCacheCount());
    assertEquals(1L, stats.getHitCount());
    assertEquals(20L, stats.getMissCount());

    cache.onClose(shard);

    // forgot everything
    stats = cache.getStats(shard);
    assertEquals(0L, stats.getCacheSize());
    assertEquals(0L, stats.getCacheCount());
    assertEquals(0L, stats.getHitCount());
    assertEquals(0L, stats.getMissCount());

    cache.close(); // this triggers some assertions
}

From source file:org.elasticsearch.indices.IndicesQueryCacheTests.java

License:Apache License

public void testTwoShards() throws IOException {
    Directory dir1 = newDirectory();/*from  www.  j  av a 2s. c  o m*/
    IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig());
    w1.addDocument(new Document());
    DirectoryReader r1 = DirectoryReader.open(w1);
    w1.close();
    ShardId shard1 = new ShardId("index", "_na_", 0);
    r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
    IndexSearcher s1 = new IndexSearcher(r1);
    s1.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Directory dir2 = newDirectory();
    IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
    w2.addDocument(new Document());
    DirectoryReader r2 = DirectoryReader.open(w2);
    w2.close();
    ShardId shard2 = new ShardId("index", "_na_", 1);
    r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
    IndexSearcher s2 = new IndexSearcher(r2);
    s2.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
            .put(IndicesQueryCache.INDICES_QUERIES_CACHE_ALL_SEGMENTS_SETTING.getKey(), true).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s1.setQueryCache(cache);
    s2.setQueryCache(cache);

    assertEquals(1, s1.count(new DummyQuery(0)));

    QueryCacheStats stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    QueryCacheStats stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());

    assertEquals(1, s2.count(new DummyQuery(0)));

    stats1 = cache.getStats(shard1);
    assertEquals(1L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(1L, stats2.getCacheSize());
    assertEquals(1L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(1L, stats2.getMissCount());

    for (int i = 0; i < 20; ++i) {
        assertEquals(1, s2.count(new DummyQuery(i)));
    }

    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize()); // evicted
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    IOUtils.close(r1, dir1);

    // no changes
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(1L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    cache.onClose(shard1);

    // forgot everything about shard1
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(10L, stats2.getCacheSize());
    assertEquals(20L, stats2.getCacheCount());
    assertEquals(1L, stats2.getHitCount());
    assertEquals(20L, stats2.getMissCount());

    IOUtils.close(r2, dir2);
    cache.onClose(shard2);

    // forgot everything about shard2
    stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(0L, stats1.getCacheCount());
    assertEquals(0L, stats1.getHitCount());
    assertEquals(0L, stats1.getMissCount());

    stats2 = cache.getStats(shard2);
    assertEquals(0L, stats2.getCacheSize());
    assertEquals(0L, stats2.getCacheCount());
    assertEquals(0L, stats2.getHitCount());
    assertEquals(0L, stats2.getMissCount());

    cache.close(); // this triggers some assertions
}

From source file:org.elasticsearch.indices.IndicesQueryCacheTests.java

License:Apache License

public void testStatsOnEviction() throws IOException {
    Directory dir1 = newDirectory();/*from   w w  w . j  a v  a2  s .co  m*/
    IndexWriter w1 = new IndexWriter(dir1, newIndexWriterConfig());
    w1.addDocument(new Document());
    DirectoryReader r1 = DirectoryReader.open(w1);
    w1.close();
    ShardId shard1 = new ShardId("index", "_na_", 0);
    r1 = ElasticsearchDirectoryReader.wrap(r1, shard1);
    IndexSearcher s1 = new IndexSearcher(r1);
    s1.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Directory dir2 = newDirectory();
    IndexWriter w2 = new IndexWriter(dir2, newIndexWriterConfig());
    w2.addDocument(new Document());
    DirectoryReader r2 = DirectoryReader.open(w2);
    w2.close();
    ShardId shard2 = new ShardId("index", "_na_", 1);
    r2 = ElasticsearchDirectoryReader.wrap(r2, shard2);
    IndexSearcher s2 = new IndexSearcher(r2);
    s2.setQueryCachingPolicy(QueryCachingPolicy.ALWAYS_CACHE);

    Settings settings = Settings.builder().put(IndicesQueryCache.INDICES_CACHE_QUERY_COUNT_SETTING.getKey(), 10)
            .put(IndicesQueryCache.INDICES_QUERIES_CACHE_ALL_SEGMENTS_SETTING.getKey(), true).build();
    IndicesQueryCache cache = new IndicesQueryCache(settings);
    s1.setQueryCache(cache);
    s2.setQueryCache(cache);

    assertEquals(1, s1.count(new DummyQuery(0)));

    for (int i = 1; i <= 20; ++i) {
        assertEquals(1, s2.count(new DummyQuery(i)));
    }

    QueryCacheStats stats1 = cache.getStats(shard1);
    assertEquals(0L, stats1.getCacheSize());
    assertEquals(1L, stats1.getCacheCount());

    // this used to fail because we were evicting an empty cache on
    // the segment from r1
    IOUtils.close(r1, dir1);
    cache.onClose(shard1);

    IOUtils.close(r2, dir2);
    cache.onClose(shard2);

    cache.close(); // this triggers some assertions
}