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

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

Introduction

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

Prototype

public QueryCachingPolicy getQueryCachingPolicy() 

Source Link

Document

Return the query cache of this IndexSearcher .

Usage

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 w  ww. j a  v a2  s.  c  o m*/
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.percolator.PercolatorServiceTests.java

License:Apache License

public void testCount() throws Exception {
    PercolateContext context = mock(PercolateContext.class);
    when(context.shardTarget()).thenReturn(new SearchShardTarget("_id", new Index("_index", "_na_"), 0));
    when(context.percolatorTypeFilter()).thenReturn(new MatchAllDocsQuery());
    when(context.isOnlyCount()).thenReturn(true);
    IndexShard shard = mock(IndexShard.class);
    when(shard.shardId()).thenReturn(new ShardId("_index", "_na_", 0));
    when(context.indexShard()).thenReturn(shard);

    PercolatorQueriesRegistry registry = createRegistry();
    addPercolatorQuery("1", new TermQuery(new Term("field", "brown")), indexWriter, registry);
    addPercolatorQuery("2", new TermQuery(new Term("field", "fox")), indexWriter, registry);
    addPercolatorQuery("3", new TermQuery(new Term("field", "monkey")), indexWriter, registry);

    indexWriter.close();/* w w w .ja v a2s .c  om*/
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    when(context.searcher()).thenReturn(new ContextIndexSearcher(new Engine.Searcher("test", shardSearcher),
            shardSearcher.getQueryCache(), shardSearcher.getQueryCachingPolicy()));

    MemoryIndex memoryIndex = new MemoryIndex();
    memoryIndex.addField("field", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer());
    IndexSearcher percolateSearcher = memoryIndex.createSearcher();
    when(context.docSearcher()).thenReturn(percolateSearcher);

    PercolateShardResponse response = PercolatorService.doPercolate(context, registry, null, null, null);
    assertThat(response.topDocs().totalHits, equalTo(2));
}

From source file:org.elasticsearch.percolator.PercolatorServiceTests.java

License:Apache License

public void testTopMatching() throws Exception {
    PercolateContext context = mock(PercolateContext.class);
    when(context.shardTarget()).thenReturn(new SearchShardTarget("_id", new Index("_index", "_na_"), 0));
    when(context.percolatorTypeFilter()).thenReturn(new MatchAllDocsQuery());
    when(context.size()).thenReturn(10);
    IndexShard shard = mock(IndexShard.class);
    when(shard.shardId()).thenReturn(new ShardId("_index", "_na_", 0));
    when(context.indexShard()).thenReturn(shard);

    PercolatorQueriesRegistry registry = createRegistry();
    addPercolatorQuery("1", new TermQuery(new Term("field", "brown")), indexWriter, registry);
    addPercolatorQuery("2", new TermQuery(new Term("field", "monkey")), indexWriter, registry);
    addPercolatorQuery("3", new TermQuery(new Term("field", "fox")), indexWriter, registry);

    indexWriter.close();//from  w  w w .j a  va2s . c o  m
    directoryReader = DirectoryReader.open(directory);
    IndexSearcher shardSearcher = newSearcher(directoryReader);
    when(context.searcher()).thenReturn(new ContextIndexSearcher(new Engine.Searcher("test", shardSearcher),
            shardSearcher.getQueryCache(), shardSearcher.getQueryCachingPolicy()));

    MemoryIndex memoryIndex = new MemoryIndex();
    memoryIndex.addField("field", "the quick brown fox jumps over the lazy dog", new WhitespaceAnalyzer());
    IndexSearcher percolateSearcher = memoryIndex.createSearcher();
    when(context.docSearcher()).thenReturn(percolateSearcher);

    PercolateShardResponse response = PercolatorService.doPercolate(context, registry, null, null, null);
    TopDocs topDocs = response.topDocs();
    assertThat(topDocs.totalHits, equalTo(2));
    assertThat(topDocs.scoreDocs.length, equalTo(2));
    assertThat(topDocs.scoreDocs[0].doc, equalTo(0));
    assertThat(topDocs.scoreDocs[1].doc, equalTo(2));
}

From source file:org.elasticsearch.xpack.core.security.authz.accesscontrol.SecurityIndexSearcherWrapper.java

License:Open Source License

@Override
protected IndexSearcher wrap(IndexSearcher searcher) throws EngineException {
    if (licenseState.isSecurityEnabled() == false
            || licenseState.isDocumentAndFieldLevelSecurityAllowed() == false) {
        return searcher;
    }//from w ww  . j av a  2  s .  c  o  m

    final DirectoryReader directoryReader = (DirectoryReader) searcher.getIndexReader();
    if (directoryReader instanceof DocumentSubsetDirectoryReader) {
        // The reasons why we return a custom searcher:
        // 1) in the case the role query is sparse then large part of the main query can be skipped
        // 2) If the role query doesn't match with any docs in a segment, that a segment can be skipped
        IndexSearcher indexSearcher = new IndexSearcherWrapper((DocumentSubsetDirectoryReader) directoryReader);
        indexSearcher.setQueryCache(indexSearcher.getQueryCache());
        indexSearcher.setQueryCachingPolicy(indexSearcher.getQueryCachingPolicy());
        indexSearcher.setSimilarity(indexSearcher.getSimilarity(true));
        return indexSearcher;
    }
    return searcher;
}