Search in sources :

Example 81 with IndexSearcher

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

the class TestBufferedIndexInput method testSetBufferSize.

public void testSetBufferSize() throws IOException {
    Path indexDir = createTempDir("testSetBufferSize");
    MockFSDirectory dir = new MockFSDirectory(indexDir, random());
    IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig(new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE).setMergePolicy(newLogMergePolicy(false)));
    for (int i = 0; i < 37; i++) {
        Document doc = new Document();
        doc.add(newTextField("content", "aaa bbb ccc ddd" + i, Field.Store.YES));
        doc.add(newTextField("id", "" + i, Field.Store.YES));
        writer.addDocument(doc);
    }
    dir.allIndexInputs.clear();
    IndexReader reader = DirectoryReader.open(writer);
    Term aaa = new Term("content", "aaa");
    Term bbb = new Term("content", "bbb");
    reader.close();
    dir.tweakBufferSizes();
    writer.deleteDocuments(new Term("id", "0"));
    reader = DirectoryReader.open(writer);
    IndexSearcher searcher = newSearcher(reader);
    ScoreDoc[] hits = searcher.search(new TermQuery(bbb), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(36, hits.length);
    reader.close();
    dir.tweakBufferSizes();
    writer.deleteDocuments(new Term("id", "4"));
    reader = DirectoryReader.open(writer);
    searcher = newSearcher(reader);
    hits = searcher.search(new TermQuery(bbb), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(35, hits.length);
    dir.tweakBufferSizes();
    hits = searcher.search(new TermQuery(new Term("id", "33")), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(1, hits.length);
    hits = searcher.search(new TermQuery(aaa), 1000).scoreDocs;
    dir.tweakBufferSizes();
    assertEquals(35, hits.length);
    writer.close();
    reader.close();
}
Also used : Path(java.nio.file.Path) IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 82 with IndexSearcher

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

the class TestNRTCachingDirectory method testNRTAndCommit.

public void testNRTAndCommit() throws Exception {
    Directory dir = newDirectory();
    NRTCachingDirectory cachedDir = new NRTCachingDirectory(dir, 2.0, 25.0);
    MockAnalyzer analyzer = new MockAnalyzer(random());
    analyzer.setMaxTokenLength(TestUtil.nextInt(random(), 1, IndexWriter.MAX_TERM_LENGTH));
    IndexWriterConfig conf = newIndexWriterConfig(analyzer);
    RandomIndexWriter w = new RandomIndexWriter(random(), cachedDir, conf);
    final LineFileDocs docs = new LineFileDocs(random());
    final int numDocs = TestUtil.nextInt(random(), 100, 400);
    if (VERBOSE) {
        System.out.println("TEST: numDocs=" + numDocs);
    }
    final List<BytesRef> ids = new ArrayList<>();
    DirectoryReader r = null;
    for (int docCount = 0; docCount < numDocs; docCount++) {
        final Document doc = docs.nextDoc();
        ids.add(new BytesRef(doc.get("docid")));
        w.addDocument(doc);
        if (random().nextInt(20) == 17) {
            if (r == null) {
                r = DirectoryReader.open(w.w);
            } else {
                final DirectoryReader r2 = DirectoryReader.openIfChanged(r);
                if (r2 != null) {
                    r.close();
                    r = r2;
                }
            }
            assertEquals(1 + docCount, r.numDocs());
            final IndexSearcher s = newSearcher(r);
            // Just make sure search can run; we can't assert
            // totHits since it could be 0
            TopDocs hits = s.search(new TermQuery(new Term("body", "the")), 10);
        // System.out.println("tot hits " + hits.totalHits);
        }
    }
    if (r != null) {
        r.close();
    }
    // Close should force cache to clear since all files are sync'd
    w.close();
    final String[] cachedFiles = cachedDir.listCachedFiles();
    for (String file : cachedFiles) {
        System.out.println("FAIL: cached file " + file + " remains after sync");
    }
    assertEquals(0, cachedFiles.length);
    r = DirectoryReader.open(dir);
    for (BytesRef id : ids) {
        assertEquals(1, r.docFreq(new Term("docid", id)));
    }
    r.close();
    cachedDir.close();
    docs.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) BytesRef(org.apache.lucene.util.BytesRef) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) LineFileDocs(org.apache.lucene.util.LineFileDocs)

Example 83 with IndexSearcher

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

the class TestRAMDirectory method testRAMDirectory.

public void testRAMDirectory() throws IOException {
    Path indexDir = buildIndex();
    FSDirectory dir = new SimpleFSDirectory(indexDir);
    MockDirectoryWrapper ramDir = new MockDirectoryWrapper(random(), new RAMDirectory(dir, newIOContext(random())));
    // close the underlaying directory
    dir.close();
    // Check size
    assertEquals(ramDir.sizeInBytes(), ramDir.getRecomputedSizeInBytes());
    // open reader to test document count
    IndexReader reader = DirectoryReader.open(ramDir);
    assertEquals(DOCS_TO_ADD, reader.numDocs());
    // open search zo check if all doc's are there
    IndexSearcher searcher = newSearcher(reader);
    // search for all documents
    for (int i = 0; i < DOCS_TO_ADD; i++) {
        Document doc = searcher.doc(i);
        assertTrue(doc.getField("content") != null);
    }
    // cleanup
    reader.close();
}
Also used : Path(java.nio.file.Path) IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) Document(org.apache.lucene.document.Document)

Example 84 with IndexSearcher

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

the class TestSimilarity2 method testEmptyField.

/** similar to the above, but ORs the query with a real field */
public void testEmptyField() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(newTextField("foo", "bar", Field.Store.NO));
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher is = newSearcher(ir);
    for (Similarity sim : sims) {
        is.setSimilarity(sim);
        BooleanQuery.Builder query = new BooleanQuery.Builder();
        query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
        query.add(new TermQuery(new Term("bar", "baz")), BooleanClause.Occur.SHOULD);
        assertEquals(1, is.search(query.build(), 10).totalHits);
    }
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 85 with IndexSearcher

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

the class TestSimilarity2 method testEmptyIndex.

/** because of stupid things like querynorm, it's possible we computeStats on a field that doesnt exist at all
   *  test this against a totally empty index, to make sure sims handle it
   */
public void testEmptyIndex() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher is = newSearcher(ir);
    for (Similarity sim : sims) {
        is.setSimilarity(sim);
        assertEquals(0, is.search(new TermQuery(new Term("foo", "bar")), 10).totalHits);
    }
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

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