Search in sources :

Example 76 with IndexSearcher

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

the class QueryProfilerTests method setup.

@BeforeClass
public static void setup() throws IOException {
    dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir);
    final int numDocs = TestUtil.nextInt(random(), 1, 20);
    for (int i = 0; i < numDocs; ++i) {
        final int numHoles = random().nextInt(5);
        for (int j = 0; j < numHoles; ++j) {
            w.addDocument(new Document());
        }
        Document doc = new Document();
        doc.add(new StringField("foo", "bar", Store.NO));
        w.addDocument(doc);
    }
    reader = w.getReader();
    w.close();
    Engine.Searcher engineSearcher = new Engine.Searcher("test", new IndexSearcher(reader));
    searcher = new ContextIndexSearcher(engineSearcher, IndexSearcher.getDefaultQueryCache(), MAYBE_CACHE_POLICY);
}
Also used : ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) ContextIndexSearcher(org.elasticsearch.search.internal.ContextIndexSearcher) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Engine(org.elasticsearch.index.engine.Engine) BeforeClass(org.junit.BeforeClass)

Example 77 with IndexSearcher

use of org.apache.lucene.search.IndexSearcher in project neo4j by neo4j.

the class FullTxData method internalQuery.

private Collection<EntityId> internalQuery(Query query, QueryContext contextOrNull) {
    if (this.directory == null) {
        return Collections.emptySet();
    }
    try {
        Sort sorting = contextOrNull != null ? contextOrNull.getSorting() : null;
        boolean prioritizeCorrectness = contextOrNull == null || !contextOrNull.getTradeCorrectnessForSpeed();
        IndexSearcher theSearcher = searcher(prioritizeCorrectness);
        query = includeOrphans(query);
        DocValuesCollector docValuesCollector = new DocValuesCollector(prioritizeCorrectness);
        theSearcher.search(query, docValuesCollector);
        Collection<EntityId> result = new ArrayList<>();
        PrimitiveLongIterator valuesIterator = docValuesCollector.getSortedValuesIterator(KEY_DOC_ID, sorting);
        while (valuesIterator.hasNext()) {
            result.add(new EntityId.IdData(valuesIterator.next()));
        }
        return result;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) PrimitiveLongIterator(org.neo4j.collection.primitive.PrimitiveLongIterator) DocValuesCollector(org.neo4j.kernel.api.impl.index.collector.DocValuesCollector) ArrayList(java.util.ArrayList) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException)

Example 78 with IndexSearcher

use of org.apache.lucene.search.IndexSearcher in project neo4j by neo4j.

the class FullTxData method searcher.

private IndexSearcher searcher(boolean allowRefreshSearcher) {
    if (this.searcher != null && (!modified || !allowRefreshSearcher)) {
        return this.searcher;
    }
    try {
        IndexReader newReader = this.reader == null ? DirectoryReader.open(this.writer) : DirectoryReader.openIfChanged((DirectoryReader) this.reader);
        if (newReader == null) {
            return this.searcher;
        }
        LuceneUtil.close(reader);
        this.reader = newReader;
        LuceneUtil.close(searcher);
        searcher = new IndexSearcher(reader);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (allowRefreshSearcher) {
            this.modified = false;
        }
    }
    return this.searcher;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Example 79 with IndexSearcher

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

the class CollationTestBase method testFarsiTermRangeQuery.

public void testFarsiTermRangeQuery(Analyzer analyzer, BytesRef firstBeg, BytesRef firstEnd, BytesRef secondBeg, BytesRef secondEnd) throws Exception {
    Directory farsiIndex = newDirectory();
    IndexWriter writer = new IndexWriter(farsiIndex, new IndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(new TextField("content", "ساب", Field.Store.YES));
    doc.add(new StringField("body", "body", Field.Store.YES));
    writer.addDocument(doc);
    writer.close();
    IndexReader reader = DirectoryReader.open(farsiIndex);
    IndexSearcher search = newSearcher(reader);
    // Unicode order would include U+0633 in [ U+062F - U+0698 ], but Farsi
    // orders the U+0698 character before the U+0633 character, so the single
    // index Term below should NOT be returned by a TermRangeQuery
    // with a Farsi Collator (or an Arabic one for the case when Farsi is 
    // not supported).
    Query csrq = new TermRangeQuery("content", firstBeg, firstEnd, true, true);
    ScoreDoc[] result = search.search(csrq, 1000).scoreDocs;
    assertEquals("The index Term should not be included.", 0, result.length);
    csrq = new TermRangeQuery("content", secondBeg, secondEnd, true, true);
    result = search.search(csrq, 1000).scoreDocs;
    assertEquals("The index Term should be included.", 1, result.length);
    reader.close();
    farsiIndex.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) IndexReader(org.apache.lucene.index.IndexReader) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 80 with IndexSearcher

use of org.apache.lucene.search.IndexSearcher 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

IndexSearcher (org.apache.lucene.search.IndexSearcher)927 Document (org.apache.lucene.document.Document)528 IndexReader (org.apache.lucene.index.IndexReader)430 Directory (org.apache.lucene.store.Directory)407 TopDocs (org.apache.lucene.search.TopDocs)382 TermQuery (org.apache.lucene.search.TermQuery)332 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)311 Query (org.apache.lucene.search.Query)299 Term (org.apache.lucene.index.Term)287 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)229 BooleanQuery (org.apache.lucene.search.BooleanQuery)176 IOException (java.io.IOException)155 Field (org.apache.lucene.document.Field)140 IndexWriter (org.apache.lucene.index.IndexWriter)139 ScoreDoc (org.apache.lucene.search.ScoreDoc)135 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)122 Sort (org.apache.lucene.search.Sort)114 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)111 DirectoryReader (org.apache.lucene.index.DirectoryReader)110 ArrayList (java.util.ArrayList)109