Search in sources :

Example 1 with AssertingIndexSearcher

use of org.apache.lucene.search.AssertingIndexSearcher in project elasticsearch by elastic.

the class MockEngineSupport method newSearcher.

public AssertingIndexSearcher newSearcher(String source, IndexSearcher searcher, SearcherManager manager) throws EngineException {
    IndexReader reader = searcher.getIndexReader();
    IndexReader wrappedReader = reader;
    assert reader != null;
    if (reader instanceof DirectoryReader && mockContext.wrapReader) {
        wrappedReader = wrapReader((DirectoryReader) reader);
    }
    // this executes basic query checks and asserts that weights are normalized only once etc.
    final AssertingIndexSearcher assertingIndexSearcher = new AssertingIndexSearcher(mockContext.random, wrappedReader);
    assertingIndexSearcher.setSimilarity(searcher.getSimilarity(true));
    assertingIndexSearcher.setQueryCache(filterCache);
    assertingIndexSearcher.setQueryCachingPolicy(filterCachingPolicy);
    return assertingIndexSearcher;
}
Also used : FilterDirectoryReader(org.apache.lucene.index.FilterDirectoryReader) DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexReader(org.apache.lucene.index.IndexReader) AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher)

Example 2 with AssertingIndexSearcher

use of org.apache.lucene.search.AssertingIndexSearcher in project elasticsearch by elastic.

the class MockEngineSupport method wrapSearcher.

public Engine.Searcher wrapSearcher(String source, Engine.Searcher engineSearcher, IndexSearcher searcher, SearcherManager manager) {
    final AssertingIndexSearcher assertingIndexSearcher = newSearcher(source, searcher, manager);
    assertingIndexSearcher.setSimilarity(searcher.getSimilarity(true));
    // pass the original searcher to the super.newSearcher() method to make sure this is the searcher that will
    // be released later on. If we wrap an index reader here must not pass the wrapped version to the manager
    // on release otherwise the reader will be closed too early. - good news, stuff will fail all over the place if we don't get this right here
    AssertingSearcher assertingSearcher = new AssertingSearcher(assertingIndexSearcher, engineSearcher, shardId, logger) {

        @Override
        public void close() {
            try {
                searcherCloseable.remove(this);
            } finally {
                super.close();
            }
        }
    };
    searcherCloseable.add(assertingSearcher, engineSearcher.source());
    return assertingSearcher;
}
Also used : AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher)

Example 3 with AssertingIndexSearcher

use of org.apache.lucene.search.AssertingIndexSearcher in project lucene-solr by apache.

the class LuceneTestCase method newSearcher.

/**
   * Create a new searcher over the reader. This searcher might randomly use
   * threads. if <code>maybeWrap</code> is true, this searcher might wrap the
   * reader with one that returns null for getSequentialSubReaders. If
   * <code>wrapWithAssertions</code> is true, this searcher might be an
   * {@link AssertingIndexSearcher} instance.
   */
public static IndexSearcher newSearcher(IndexReader r, boolean maybeWrap, boolean wrapWithAssertions) {
    Random random = random();
    if (usually()) {
        if (maybeWrap) {
            try {
                r = maybeWrapReader(r);
            } catch (IOException e) {
                Rethrow.rethrow(e);
            }
        }
        // ultimately whatever you do will be checkIndex'd at the end anyway. 
        if (random.nextInt(500) == 0 && r instanceof LeafReader) {
            // but maybe sometimes run this on the other crazy readers maybeWrapReader creates?
            try {
                TestUtil.checkReader(r);
            } catch (IOException e) {
                Rethrow.rethrow(e);
            }
        }
        final IndexSearcher ret;
        if (wrapWithAssertions) {
            ret = random.nextBoolean() ? new AssertingIndexSearcher(random, r) : new AssertingIndexSearcher(random, r.getContext());
        } else {
            ret = random.nextBoolean() ? new IndexSearcher(r) : new IndexSearcher(r.getContext());
        }
        ret.setSimilarity(classEnvRule.similarity);
        return ret;
    } else {
        int threads = 0;
        final ThreadPoolExecutor ex;
        if (r.getReaderCacheHelper() == null || random.nextBoolean()) {
            ex = null;
        } else {
            threads = TestUtil.nextInt(random, 1, 8);
            ex = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new NamedThreadFactory("LuceneTestCase"));
        // uncomment to intensify LUCENE-3840
        // ex.prestartAllCoreThreads();
        }
        if (ex != null) {
            if (VERBOSE) {
                System.out.println("NOTE: newSearcher using ExecutorService with " + threads + " threads");
            }
            r.getReaderCacheHelper().addClosedListener(cacheKey -> TestUtil.shutdownExecutorService(ex));
        }
        IndexSearcher ret;
        if (wrapWithAssertions) {
            ret = random.nextBoolean() ? new AssertingIndexSearcher(random, r, ex) : new AssertingIndexSearcher(random, r.getContext(), ex);
        } else {
            ret = random.nextBoolean() ? new IndexSearcher(r, ex) : new IndexSearcher(r.getContext(), ex);
        }
        ret.setSimilarity(classEnvRule.similarity);
        ret.setQueryCachingPolicy(MAYBE_CACHE_POLICY);
        return ret;
    }
}
Also used : AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Random(java.util.Random) IOException(java.io.IOException) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) AssertingIndexSearcher(org.apache.lucene.search.AssertingIndexSearcher)

Aggregations

AssertingIndexSearcher (org.apache.lucene.search.AssertingIndexSearcher)3 IOException (java.io.IOException)1 Random (java.util.Random)1 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 FilterDirectoryReader (org.apache.lucene.index.FilterDirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1