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

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

Introduction

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

Prototype

public <C extends Collector, T> T search(Query query, CollectorManager<C, T> collectorManager)
        throws IOException 

Source Link

Document

Lower-level search API.

Usage

From source file:com.mathworks.xzheng.searching.NearRealTimeTest.java

License:Apache License

public void testNearRealTime() throws Exception {
    Directory dir = new RAMDirectory();

    IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46,
            new StandardAnalyzer(Version.LUCENE_46));
    IndexWriter writer = new IndexWriter(dir, config);
    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  .  ja v a  2  s .  c o m*/
    }
    IndexReader reader = DirectoryReader.open(writer.getDirectory()); // #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
    IndexReader newReader = DirectoryReader.open(writer.getDirectory()); // #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.mathworks.xzheng.searching.NumericRangeQueryTest.java

License:Apache License

public void testInclusive() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(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 ww. java2 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);

    dir.close();
}

From source file:com.mathworks.xzheng.searching.NumericRangeQueryTest.java

License:Apache License

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

    // pub date of TTC was September 2006
    NumericRangeQuery query = NumericRangeQuery.newIntRange("pubmonth", 200605, 200609, false, false);
    TopDocs matches = searcher.search(query, 10);
    assertEquals(0, matches.totalHits);//from   w w w.ja va2  s. com

    dir.close();
}

From source file:com.mathworks.xzheng.searching.PrefixQueryTest.java

License:Apache License

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

    Term term = new Term("category", //#A
            "/technology/computers/programming"); //#A
    PrefixQuery query = new PrefixQuery(term); //#A

    TopDocs matches = searcher.search(query, 10); //#A
    int programmingAndBelow = matches.totalHits;

    matches = searcher.search(new TermQuery(term), 10); //#B
    int justProgramming = matches.totalHits;

    assertTrue(programmingAndBelow > justProgramming);

    dir.close();//from   w w w .  ja v  a2 s  .  co  m
}

From source file:com.mathworks.xzheng.searching.ScoreTest.java

License:Apache License

public void testSimple() throws Exception {
    indexSingleFieldDocs(new Field[] { new Field("contents", "x", Field.Store.YES, Field.Index.ANALYZED) });
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
    searcher.setSimilarity(new SimpleSimilarity());

    Query query = new TermQuery(new Term("contents", "x"));
    Explanation explanation = searcher.explain(query, 0);
    System.out.println(explanation);

    TopDocs matches = searcher.search(query, 10);
    assertEquals(1, matches.totalHits);/*  w  ww . j  av a 2  s. co m*/

    assertEquals(1F, matches.scoreDocs[0].score, 0.0);

}

From source file:com.mathworks.xzheng.searching.ScoreTest.java

License:Apache License

public void testWildcard() throws Exception {
    indexSingleFieldDocs(new Field[] { new Field("contents", "wild", Field.Store.YES, Field.Index.ANALYZED),
            new Field("contents", "child", Field.Store.YES, Field.Index.ANALYZED),
            new Field("contents", "mild", Field.Store.YES, Field.Index.ANALYZED),
            new Field("contents", "mildew", Field.Store.YES, Field.Index.ANALYZED) });

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
    Query query = new WildcardQuery(new Term("contents", "?ild*")); //#A
    TopDocs matches = searcher.search(query, 10);
    assertEquals("child no match", 3, matches.totalHits);

    assertEquals("score the same", matches.scoreDocs[0].score, matches.scoreDocs[1].score, 0.0);
    assertEquals("score the same", matches.scoreDocs[1].score, matches.scoreDocs[2].score, 0.0);

}

From source file:com.mathworks.xzheng.searching.ScoreTest.java

License:Apache License

public void testFuzzy() throws Exception {
    indexSingleFieldDocs(new Field[] { new Field("contents", "fuzzy", Field.Store.YES, Field.Index.ANALYZED),
            new Field("contents", "wuzzy", Field.Store.YES, Field.Index.ANALYZED) });

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory));
    Query query = new FuzzyQuery(new Term("contents", "wuzza"));
    TopDocs matches = searcher.search(query, 10);
    assertEquals("both close enough", 2, matches.totalHits);

    assertTrue("wuzzy closer than fuzzy", matches.scoreDocs[0].score != matches.scoreDocs[1].score);

    Document doc = searcher.doc(matches.scoreDocs[0].doc);
    assertEquals("wuzza bear", "wuzzy", doc.get("contents"));

}

From source file:com.mathworks.xzheng.searching.TermRangeQueryTest.java

License:Apache License

public void testTermRangeQuery() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir));
    TermRangeQuery query = new TermRangeQuery("title2", new BytesRef("d"), new BytesRef("j"), true, true);

    TopDocs matches = searcher.search(query, 100);
    /*/*w  w  w.  j  a  va  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("title2"));
    }
    */
    assertEquals(3, matches.totalHits);

    dir.close();
}

From source file:com.mathworks.xzheng.tools.BerkeleyDbJESearcher.java

License:Apache License

public static void main(String[] args) throws IOException, DatabaseException {
    if (args.length != 1) {
        System.err.println("Usage: BerkeleyDbSearcher <index dir>");
        System.exit(-1);/*w w w  .  java 2 s  .co m*/
    }
    File indexFile = new File(args[0]);

    EnvironmentConfig envConfig = new EnvironmentConfig();
    DatabaseConfig dbConfig = new DatabaseConfig();

    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);
    dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

    Environment env = new Environment(indexFile, envConfig);

    Database index = env.openDatabase(null, "__index__", dbConfig);
    Database blocks = env.openDatabase(null, "__blocks__", dbConfig);

    JEDirectory directory = new JEDirectory(null, index, blocks);

    IndexSearcher searcher = new IndexSearcher(directory, true);
    TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10);
    System.out.println(hits.totalHits + " documents found");

    index.close();
    blocks.close();
    env.close();
}

From source file:com.mathworks.xzheng.tools.BerkeleyDbSearcher.java

License:Apache License

public static void main(String[] args) throws IOException, DatabaseException {
    if (args.length != 1) {
        System.err.println("Usage: BerkeleyDbSearcher <index dir>");
        System.exit(-1);//from   w ww.  jav  a 2 s. com
    }
    File indexFile = new File(args[0]);

    EnvironmentConfig envConfig = new EnvironmentConfig();
    DatabaseConfig dbConfig = new DatabaseConfig();

    envConfig.setTransactional(true);
    envConfig.setInitializeCache(true);
    envConfig.setInitializeLocking(true);
    envConfig.setInitializeLogging(true);
    envConfig.setAllowCreate(true);
    envConfig.setThreaded(true);
    dbConfig.setAllowCreate(true);
    dbConfig.setType(DatabaseType.BTREE);

    Environment env = new Environment(indexFile, envConfig);

    Database index = env.openDatabase(null, "__index__", null, dbConfig);
    Database blocks = env.openDatabase(null, "__blocks__", null, dbConfig);

    DbDirectory directory = new DbDirectory(null, index, blocks, 0);

    IndexSearcher searcher = new IndexSearcher(directory, true);
    TopDocs hits = searcher.search(new TermQuery(new Term("contents", "fox")), 10);
    System.out.println(hits.totalHits + " documents found");

    index.close();
    blocks.close();
    env.close();
}