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:aos.lucene.search.ext.payloads.PayloadsTest.java

License:Apache License

public void testPayloadTermQuery() throws Throwable {
    addDoc("Hurricane warning",
            "Bulletin: A hurricane warning was issued at " + "6 AM for the outer great banks");
    addDoc("Warning label maker", "The warning label maker is a delightful toy for "
            + "your precocious seven year old's warning needs");
    addDoc("Tornado warning",
            "Bulletin: There is a tornado warning for " + "Worcester county until 6 PM today");

    IndexReader r = writer.getReader();/*from www  . j  av a 2 s  .  c o  m*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(r);

    searcher.setSimilarity(new BoostingSimilarity());

    Term warning = new Term("contents", "warning");

    Query query1 = new TermQuery(warning);
    LOGGER.info("\nTermQuery results:");
    TopDocs hits = searcher.search(query1, 10);
    TestUtil.dumpHits(searcher, hits);

    assertEquals("Warning label maker", // #B
            searcher.doc(hits.scoreDocs[0].doc).get("title")); // #B

    Query query2 = new PayloadTermQuery(warning, new AveragePayloadFunction());
    LOGGER.info("\nPayloadTermQuery results:");
    hits = searcher.search(query2, 10);
    TestUtil.dumpHits(searcher, hits);

    assertEquals("Warning label maker", // #C
            searcher.doc(hits.scoreDocs[2].doc).get("title")); // #C
    r.close();
    searcher.close();
}

From source file:aos.lucene.search.ext.sorting.DistanceSortingTest.java

License:Apache License

protected void setUp() throws Exception {
    directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_46),
            IndexWriter.MaxFieldLength.UNLIMITED);
    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();/*  ww w. java  2 s . c o m*/

    searcher = new IndexSearcher(directory);

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

From source file:aos.lucene.search.msc.BasicSearchingTest.java

License:Apache License

public void testTerm() throws Exception {

    Directory dir = FSDirectory.open(new File("target/index"));
    IndexReader reader = DirectoryReader.open(dir);
    IndexSearcher searcher = new IndexSearcher(reader);

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

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

    reader.close();/*from w  w w. java 2 s . c  o m*/
    dir.close();
}

From source file:aos.lucene.search.msc.BasicSearchingTest.java

License:Apache License

/**
 * #A Obtain directory from TestUtil #B Create IndexSearcher #C Confirm one
 * hit for "ant" #D Confirm two hits for "junit"
 *///from w  w w .  j  a v a  2s .c om
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();
    dir.close();
}

From source file:aos.lucene.search.msc.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_46, "contents", new SimpleAnalyzer());

    Query query = parser.parse("+JUNIT +ANT -MOCK");
    TopDocs docs = searcher.search(query, 10);
    assertEquals(1, docs.totalHits);//  w w  w  . ja v  a  2 s.  c  om
    Document d = searcher.doc(docs.scoreDocs[0].doc);
    assertEquals("Ant in Action", d.get("title"));

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

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

From source file:aos.lucene.search.msc.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();
    IndexSearcher searcher = new IndexSearcher(dir);
    TopDocs matches = searcher.search(searchingBooks2010, 10);

    assertTrue(TestUtil.hitsIncludeTitle(searcher, matches, "Lucene in Action, Second Edition"));
    searcher.close();/*from  w  ww  . ja v  a 2  s  .c om*/
    dir.close();
}

From source file:aos.lucene.search.msc.BooleanQueryTest.java

License:Apache License

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

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

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

    Directory dir = TestUtil.getBookIndexDirectory();
    IndexSearcher searcher = new IndexSearcher(dir);
    TopDocs matches = searcher.search(enlightenmentBooks, 10);
    LOGGER.info("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  ww  .  j a  v  a2s.c  o m
    dir.close();
}

From source file:aos.lucene.search.msc.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);// w  w w  .  ja v  a 2  s . c o m
    }

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

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

    LOGGER.info("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

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

From source file:aos.lucene.search.msc.Fragments.java

License:Apache License

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

From source file:aos.lucene.search.msc.Fragments.java

License:Apache License

public void nrtReader() throws Exception {
    IndexReader reader = null;// ww  w  .j  av a 2 s  .c o  m
    IndexSearcher searcher;

    IndexReader newReader = reader.reopen();
    if (reader != newReader) {
        reader.close();
        reader = newReader;
        searcher = new IndexSearcher(reader);
    }

}