Example usage for org.apache.lucene.search IndexSearcher IndexSearcher

List of usage examples for org.apache.lucene.search IndexSearcher IndexSearcher

Introduction

In this page you can find the example usage for org.apache.lucene.search IndexSearcher IndexSearcher.

Prototype

public IndexSearcher(IndexReaderContext context) 

Source Link

Document

Creates a searcher searching the provided top-level IndexReaderContext .

Usage

From source file:com.leavesfly.lia.searching.BasicSearchingTest.java

License:Apache License

public void testTerm() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory(); //A
    IndexSearcher searcher = new IndexSearcher(dir); //B

    Term t = new Term("subject", "ant");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 10);
    assertEquals("Ant in Action", //C
            1, docs.totalHits); //C

    t = new Term("subject", "junit");
    docs = searcher.search(new TermQuery(t), 10);
    assertEquals("Ant in Action, " + //D
            "JUnit in Action, Second Edition", //D
            2, docs.totalHits); //D

    searcher.close();// ww w.  ja  v  a 2 s. c  o  m
    dir.close();
}

From source file:com.leavesfly.lia.searching.BasicSearchingTest.java

License:Apache License

public void testKeyword() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir);

    Term t = new Term("isbn", "9781935182023");
    Query query = new TermQuery(t);
    TopDocs docs = searcher.search(query, 10);
    assertEquals("JUnit in Action, Second Edition", 1, docs.totalHits);

    searcher.close();// w ww .j a va 2s .c  o m
    dir.close();
}

From source file:com.leavesfly.lia.searching.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir);

    QueryParser parser = new QueryParser(Version.LUCENE_30, //A
            "contents", //A
            new SimpleAnalyzer()); //A

    Query query = parser.parse("+JUNIT +ANT -MOCK"); //B
    TopDocs docs = searcher.search(query, 10);
    assertEquals(1, docs.totalHits);//  ww w  .j  ava2s.com
    Document d = searcher.doc(docs.scoreDocs[0].doc);
    assertEquals("Ant in Action", d.get("title"));

    query = parser.parse("mock OR junit"); //B
    docs = searcher.search(query, 10);
    assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits);

    searcher.close();
    dir.close();
}

From source file:com.leavesfly.lia.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();
    IndexSearcher searcher = new IndexSearcher(dir);
    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"));
    searcher.close();//from w  w  w.j av a 2  s  .  c  om
    dir.close();
}

From source file:com.leavesfly.lia.searching.Explainer.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println("Usage: Explainer <index dir> <query>");
        System.exit(1);/*from   w  w w .j  a  v a  2  s.  c om*/
    }

    String indexDir = args[0];
    String queryExpression = args[1];

    Directory directory = FSDirectory.open(new File(indexDir));
    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new SimpleAnalyzer());
    Query query = parser.parse(queryExpression);

    System.out.println("Query: " + queryExpression);

    IndexSearcher searcher = new IndexSearcher(directory);
    TopDocs topDocs = searcher.search(query, 10);

    for (ScoreDoc match : topDocs.scoreDocs) {
        Explanation explanation = searcher.explain(query, match.doc); //#A

        System.out.println("----------");
        Document doc = searcher.doc(match.doc);
        System.out.println(doc.get("title"));
        System.out.println(explanation.toString()); //#B
    }
    searcher.close();
    directory.close();
}

From source file:com.leavesfly.lia.searching.Fragments.java

License:Apache License

public void openSearcher() throws Exception {
    Directory dir = FSDirectory.open(new File("/path/to/index"));
    IndexReader reader = IndexReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);
}

From source file:com.leavesfly.lia.searching.Fragments.java

License:Apache License

public void nrtReader() throws Exception {
    IndexReader reader = null;//from   w  w w .j a  va2  s  .  c o m
    IndexSearcher searcher;
    // START
    IndexReader newReader = reader.reopen();
    if (reader != newReader) {
        reader.close();
        reader = newReader;
        searcher = new IndexSearcher(reader);
    }
    // END
}

From source file:com.leavesfly.lia.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
            IndexWriter.MaxFieldLength.UNLIMITED);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new Field("id", "" + i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));
        doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc);//  ww  w . jav a2s . co  m
    }
    IndexReader reader = writer.getReader(); // #1
    IndexSearcher searcher = new IndexSearcher(reader); // #A

    Query query = new TermQuery(new Term("text", "aaa"));
    TopDocs docs = searcher.search(query, 1);
    assertEquals(10, docs.totalHits); // #B

    writer.deleteDocuments(new Term("id", "7")); // #2

    Document doc = new Document(); // #3
    doc.add(new Field("id", // #3
            "11", // #3
            Field.Store.NO, // #3
            Field.Index.NOT_ANALYZED_NO_NORMS)); // #3
    doc.add(new Field("text", // #3
            "bbb", // #3
            Field.Store.NO, // #3
            Field.Index.ANALYZED)); // #3
    writer.addDocument(doc); // #3

    IndexReader newReader = reader.reopen(); // #4
    assertFalse(reader == newReader); // #5
    reader.close(); // #6
    searcher = new IndexSearcher(newReader);

    TopDocs hits = searcher.search(query, 10); // #7
    assertEquals(9, hits.totalHits); // #7

    query = new TermQuery(new Term("text", "bbb")); // #8
    hits = searcher.search(query, 1); // #8
    assertEquals(1, hits.totalHits); // #8

    newReader.close();
    writer.close();
}

From source file:com.leavesfly.lia.searching.NumericRangeQueryTest.java

License:Apache License

public void testInclusive() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir);
    // pub date of TTC was September 2006
    NumericRangeQuery query = NumericRangeQuery.newIntRange("pubmonth", 200605, 200609, true, true);

    TopDocs matches = searcher.search(query, 10);
    /*//from w  w w .  ja  v  a 2  s  .c o  m
    for(int i=0;i<matches.totalHits;i++) {
      System.out.println("match " + i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("author"));
    }
    */
    assertEquals(1, matches.totalHits);
    searcher.close();
    dir.close();
}

From source file:com.leavesfly.lia.searching.PhraseQueryTest.java

License:Apache License

protected void setUp() throws IOException {
    dir = new RAMDirectory();
    IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc = new Document();
    doc.add(new Field("field", // 1
            "the quick brown fox jumped over the lazy dog", // 1
            Field.Store.YES, // 1
            Field.Index.ANALYZED)); // 1
    writer.addDocument(doc);//from   w w w. ja v  a  2s  .  c om
    writer.close();

    searcher = new IndexSearcher(dir);
}