Example usage for org.apache.lucene.index DirectoryReader open

List of usage examples for org.apache.lucene.index DirectoryReader open

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader open.

Prototype

public static DirectoryReader open(final IndexCommit commit) throws IOException 

Source Link

Document

Expert: returns an IndexReader reading the index in the given IndexCommit .

Usage

From source file:com.sxc.lucene.analysis.synonym.WordNetSynonymEngine.java

License:Apache License

public WordNetSynonymEngine(File index) throws IOException {
    fsDir = FSDirectory.open(index);
    searcher = new IndexSearcher(DirectoryReader.open(fsDir));
}

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

protected int getHitCount(String fieldName, String searchString) throws IOException {
    IndexReader reader = DirectoryReader.open(directory);
    IndexSearcher searcher = new IndexSearcher(reader); // 4
    Term t = new Term(fieldName, searchString);
    Query query = new TermQuery(t); // 5
    int hitCount = TestUtil.hitCount(searcher, query); // 6
    reader.close();/*from   w ww  . j av  a  2  s . co  m*/
    return hitCount;
}

From source file:com.sxc.lucene.index.IndexingTest.java

License:Apache License

public void testIndexReader() throws IOException {
    IndexReader reader = DirectoryReader.open(directory);
    assertEquals(ids.length, reader.maxDoc()); // 8
    assertEquals(ids.length, reader.numDocs()); // 8
    reader.close();/*from www  .  j  a va2  s.co m*/
}

From source file:com.sxc.lucene.searching.BasicSearchingTest.java

License:Apache License

public void testTerm() throws Exception {
    IndexReader reader = DirectoryReader.open(directory); // A
    IndexSearcher searcher = new IndexSearcher(reader); // B

    Term t = new Term("country", "");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 1);
    System.out.println(docs.totalHits);

    reader.close();/*  www.  j a  v  a  2 s  .c  o  m*/
    directory.close();
}

From source file:com.sxc.lucene.searching.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    IndexReader reader = DirectoryReader.open(directory); // A
    IndexSearcher searcher = new IndexSearcher(reader); // B

    QueryParser parser = new QueryParser(Version.LUCENE_47, "contents",
            new SmartChineseAnalyzer(Version.LUCENE_47));

    Query query = parser.parse("* OR *");
    TopDocs docs = searcher.search(query, 10);
    assertEquals(2, docs.totalHits);//from  ww  w .j  a  va  2 s  . co  m
    Document d = searcher.doc(docs.scoreDocs[0].doc);
    assertEquals("", d.get("country"));

    directory.close();
}

From source file:com.sxc.lucene.searching.BooleanQueryTest.java

License:Apache License

public void testAnd() throws Exception {
    TermQuery searchingBooks = new TermQuery(new Term("subject", "search")); //#1

    Query books2010 = //#2
            NumericRangeQuery.newIntRange("pubmonth", 201001, //#2
                    201012, //#2
                    true, true); //#2

    BooleanQuery searchingBooks2010 = new BooleanQuery(); //#3
    searchingBooks2010.add(searchingBooks, BooleanClause.Occur.MUST); //#3
    searchingBooks2010.add(books2010, BooleanClause.Occur.MUST); //#3

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs matches = searcher.search(searchingBooks2010, 10);

    assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Lucene in Action, Second Edition"));
    reader.close();/*from   w  w  w.jav a2 s . c  o  m*/
    dir.close();
}

From source file:com.sxc.lucene.searching.BooleanQueryTest.java

License:Apache License

public void testOr() throws Exception {
    TermQuery methodologyBooks = new TermQuery( // #1
            new Term("category", // #1
                    "/technology/computers/programming/methodology")); // #1

    TermQuery easternPhilosophyBooks = new TermQuery( // #2
            new Term("category", // #2
                    "/philosophy/eastern")); // #2

    BooleanQuery enlightenmentBooks = new BooleanQuery(); // #3
    enlightenmentBooks.add(methodologyBooks, // #3
            BooleanClause.Occur.SHOULD); // #3
    enlightenmentBooks.add(easternPhilosophyBooks, // #3
            BooleanClause.Occur.SHOULD); // #3

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopDocs matches = searcher.search(enlightenmentBooks, 10);
    System.out.println("or = " + enlightenmentBooks);

    assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Extreme Programming Explained"));
    assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Tao Te Ching \u9053\u5FB7\u7D93"));
    reader.close();/*from w w  w . j a  v  a2s  .  c  o  m*/
    dir.close();
}

From source file:com.sxc.lucene.searching.PhraseQueryTest.java

License:Apache License

protected void setUp() throws IOException {
    dir = FSDirectory.open(new File("D:/programming/lucene/PhraseQueryTest"));
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47,
            new SmartChineseAnalyzer(Version.LUCENE_47));
    config.setOpenMode(OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(dir, config);

    Document doc = new Document();
    doc.add(new TextField("field", // 1
            "the quick brown fox jumped over the lazy dog", // 1
            Field.Store.YES)); // 1
    writer.addDocument(doc);//from w  w w.jav  a2 s.  c om
    writer.close();

    searcher = new IndexSearcher(DirectoryReader.open(dir));
}

From source file:com.sxc.lucene.searching.TermRangeQueryTest.java

License:Apache License

public void testTermRangeQuery() throws Exception {
    String indexDir = "D:/programming/lucene/indexingTest";
    Directory dir = FSDirectory.open(new File(indexDir));
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);

    TermRangeQuery query = TermRangeQuery.newStringRange("id", "1", "4", true, true);

    TopDocs matches = searcher.search(query, 100);

    assertEquals(2, matches.totalHits);// ww w. j  a va  2s  . co  m
    reader.close();
    dir.close();
}

From source file:com.tekstosense.stemmer.index.Indexer.java

License:Open Source License

/**
 * Searcher./*from w w  w .j  a va 2 s .c  om*/
 *
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 * @throws QueryNodeException
 *             the query node exception
 * @throws ParseException
 *             the parse exception
 */
private static void searcher() throws IOException, QueryNodeException, ParseException {
    Path indexDirectoryPath = new File(INDEX_PATH).toPath();
    FSDirectory indexDirectory = new SimpleFSDirectory(indexDirectoryPath);
    DirectoryReader ireader = DirectoryReader.open(indexDirectory);
    IndexSearcher isearcher = new IndexSearcher(ireader);
    QueryParser parser = new QueryParser("title", new StandardAnalyzer());
    Query query = parser.parse("\"Lucene in Action\"");

    TopScoreDocCollector collector = TopScoreDocCollector.create(10);
    isearcher.search(query, new PositiveScoresOnlyCollector(collector));
    TopDocs topDocs = collector.topDocs();
    Set<String> fields = new HashSet<String>();
    fields.add("title");
    fields.add("isbn");
    for (ScoreDoc result : topDocs.scoreDocs) {
        Document doc = isearcher.doc(result.doc, fields);

        if (LOGGER.isInfoEnabled()) {

            LOGGER.info("--- Title :  " + doc.getField("title").stringValue() + " ---");
            LOGGER.info("--- ISBN : " + doc.getField("isbn").stringValue() + " ---");
            LOGGER.info(isearcher.explain(query, result.doc));
        }

    }

}