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.mathworks.xzheng.advsearching.SpanQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();

    analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("f", "the quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);/*from   w ww .  j a v  a 2s  . c  o m*/

    doc = new Document();
    doc.add(new Field("f", "the quick red fox jumps over the sleepy cat", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);

    writer.close();

    searcher = new IndexSearcher(DirectoryReader.open(directory));
    reader = searcher.getIndexReader();

    quick = new SpanTermQuery(new Term("f", "quick"));
    brown = new SpanTermQuery(new Term("f", "brown"));
    red = new SpanTermQuery(new Term("f", "red"));
    fox = new SpanTermQuery(new Term("f", "fox"));
    lazy = new SpanTermQuery(new Term("f", "lazy"));
    sleepy = new SpanTermQuery(new Term("f", "sleepy"));
    dog = new SpanTermQuery(new Term("f", "dog"));
    cat = new SpanTermQuery(new Term("f", "cat"));
}

From source file:com.mathworks.xzheng.advsearching.TimeLimitingCollectorTest.java

License:Apache License

public void testTimeLimitingCollector() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));
    Query q = new MatchAllDocsQuery();
    int numAllBooks = TestUtil.hitCount(searcher, q);

    TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false);
    Collector collector = new TimeLimitingCollector(topDocs, // #A
            null, 1000); // #A
    try {/*from ww w  .ja v a2s.c o  m*/
        searcher.search(q, collector);
        assertEquals(numAllBooks, topDocs.getTotalHits()); // #B
    } catch (TimeExceededException tee) { // #C
        System.out.println("Too much time taken."); // #C
    } // #C

    dir.close();
}

From source file:com.mathworks.xzheng.analysis.codec.MetaphoneAnalyzerTest.java

License:Apache License

public void testKoolKat() throws Exception {
    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new MetaphoneReplacementAnalyzer();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer);
    config.setOpenMode(IndexWriterConfig.OpenMode.CREATE);

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("contents", //#A
            "cool cat", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);//from   w  w w.j  a  v  a2s. c o  m
    writer.close();

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));

    Query query = new QueryParser(Version.LUCENE_46, //#B
            "contents", analyzer) //#B
                    .parse("kool kat"); //#B

    TopDocs hits = searcher.search(query, 1);
    assertEquals(1, hits.totalHits); //#C
    int docID = hits.scoreDocs[0].doc;
    doc = searcher.doc(docID);
    assertEquals("cool cat", doc.get("contents")); //#D

}

From source file:com.mathworks.xzheng.analysis.i18n.ChineseTest.java

License:Apache License

public void testChinese() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));
    Query query = new TermQuery(new Term("contents", "abc"));
    assertEquals("tao", 1, TestUtil.hitCount(searcher, query));
}

From source file:com.mathworks.xzheng.analysis.keyword.KeywordAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {
    Directory directory = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new SimpleAnalyzer(Version.LUCENE_46));

    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("partnum", "Q36", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); //A
    doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/*from   www. jav a  2s  .  c  o m*/

    writer.close();

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

From source file:com.mathworks.xzheng.analysis.positional.PositionalPorterStopAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {

    RAMDirectory directory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, porterAnalyzer);
    IndexWriter writer = new IndexWriter(directory, config);

    Document doc = new Document();
    doc.add(new Field("contents", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc);// w w  w  .j  a va  2  s .c  om
    writer.close();
    searcher = new IndexSearcher(DirectoryReader.open(directory));
    parser = new QueryParser(Version.LUCENE_46, "contents", porterAnalyzer);
}

From source file:com.mathworks.xzheng.analysis.synonym.SynonymAnalyzerTest.java

License:Apache License

public void setUp() throws Exception {
    RAMDirectory directory = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, synonymAnalyzer);
    IndexWriter writer = new IndexWriter(directory, config); //#1
    Document doc = new Document();
    doc.add(new Field("content", "The quick brown fox jumps over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED)); //#2
    writer.addDocument(doc);//from  ww  w  .j av a2s  .c om

    writer.close();

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

From source file:com.mathworks.xzheng.benchmark.PrecisionRecall.java

License:Apache License

public static void main(String[] args) throws Throwable {

    File topicsFile = new File("src/lia/benchmark/topics.txt");
    File qrelsFile = new File("src/lia/benchmark/qrels.txt");
    Directory dir = FSDirectory.open(new File("indexes/MeetLucene"));

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

    String docNameField = "filename";

    PrintWriter logger = new PrintWriter(System.out, true);

    TrecTopicsReader qReader = new TrecTopicsReader(); //#1
    QualityQuery qqs[] = qReader.readQueries( //#1
            new BufferedReader(new FileReader(topicsFile))); //#1

    Judge judge = new TrecJudge(new BufferedReader( //#2
            new FileReader(qrelsFile))); //#2

    judge.validateData(qqs, logger); //#3

    QualityQueryParser qqParser = new SimpleQQParser("title", "contents"); //#4

    QualityBenchmark qrun = new QualityBenchmark(qqs, qqParser, searcher, docNameField);
    SubmissionReport submitLog = null;//from  www.j a  va2 s  . c om
    QualityStats stats[] = qrun.execute(judge, //#5
            submitLog, logger);

    QualityStats avg = QualityStats.average(stats); //#6
    avg.log("SUMMARY", 2, logger, "  ");
    dir.close();
}

From source file:com.mathworks.xzheng.extsearch.queryparser.NumericQueryParserTest.java

License:Apache License

protected void setUp() throws Exception {
    analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);
    dir = TestUtil.getBookIndexDirectory();
    searcher = new IndexSearcher(DirectoryReader.open(dir));
}

From source file:com.mathworks.xzheng.extsearch.sorting.DistanceSortingTest.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();
    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46,
            new WhitespaceAnalyzer(Version.LUCENE_46));

    IndexWriter writer = new IndexWriter(directory, config);
    addPoint(writer, "El Charro", "restaurant", 1, 2);
    addPoint(writer, "Cafe Poca Cosa", "restaurant", 5, 9);
    addPoint(writer, "Los Betos", "restaurant", 9, 6);
    addPoint(writer, "Nico's Taco Shop", "restaurant", 3, 8);

    writer.close();/*from ww w . j ava 2s  .co  m*/

    searcher = new IndexSearcher(DirectoryReader.open(directory));

    query = new TermQuery(new Term("type", "restaurant"));
}