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.advanced.FilterTest.java

License:Apache License

protected void setUp() throws Exception { //
    allBooks = new MatchAllDocsQuery();
    dir = TestUtil.getBookIndexDirectory();
    searcher = new IndexSearcher(dir);
}

From source file:aos.lucene.search.advanced.FunctionQueryTest.java

License:Apache License

public void testRecency() throws Throwable {
    Directory dir = TestUtil.getBookIndexDirectory();
    IndexReader r = DirectoryReader.open(dir);
    IndexSearcher s = new IndexSearcher(r);
    s.setDefaultFieldSortScoring(true, true);

    QueryParser parser = new QueryParser(Version.LUCENE_46, "contents",
            new StandardAnalyzer(Version.LUCENE_46));
    Query q = parser.parse("java in action"); // #A
    Query q2 = new RecencyBoostingQuery(q, // #B
            2.0, 2 * 365, "pubmonthAsDay");
    Sort sort = new Sort(new SortField[] { SortField.FIELD_SCORE, new SortField("title2", SortField.STRING) });
    TopDocs hits = s.search(q2, null, 5, sort);

    for (int i = 0; i < hits.scoreDocs.length; i++) {
        Document doc = r.document(hits.scoreDocs[i].doc);
        LOGGER.info((1 + i) + ": " + doc.get("title") + ": pubmonth=" + doc.get("pubmonth") + " score="
                + hits.scoreDocs[i].score);
    }/*from w  w w.j  a  v a2s  .  c o m*/
    s.close();
    r.close();
    dir.close();
}

From source file:aos.lucene.search.advanced.MultiPhraseQueryTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_46),
            IndexWriter.MaxFieldLength.UNLIMITED);
    Document doc1 = new Document();
    doc1.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES,
            Field.Index.ANALYZED));
    writer.addDocument(doc1);/*from w w w  .  j  a  v  a2s  . c  o m*/
    Document doc2 = new Document();
    doc2.add(new Field("field", "the fast fox hopped over the hound", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc2);
    writer.close();

    searcher = new IndexSearcher(directory);
}

From source file:aos.lucene.search.advanced.IndexSearcherTest.java

License:Apache License

public void setUp() throws Exception {
    String[] animals = { "aardvark", "beaver", "coati", "dog", "elephant", "frog", "gila monster", "horse",
            "iguana", "javelina", "kangaroo", "lemur", "moose", "nematode", "orca", "python", "quokka", "rat",
            "scorpion", "tarantula", "uromastyx", "vicuna", "walrus", "xiphias", "yak", "zebra" };

    Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);

    Directory aTOmDirectory = new RAMDirectory(); //
    Directory nTOzDirectory = new RAMDirectory(); //

    IndexWriter aTOmWriter = new IndexWriter(aTOmDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
    IndexWriter nTOzWriter = new IndexWriter(nTOzDirectory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

    for (int i = animals.length - 1; i >= 0; i--) {
        Document doc = new Document();
        String animal = animals[i];
        doc.add(new Field("animal", animal, Field.Store.YES, Field.Index.NOT_ANALYZED));
        if (animal.charAt(0) < 'n') {
            aTOmWriter.addDocument(doc); //
        } else {// w w w.jav a  2  s  .c  om
            nTOzWriter.addDocument(doc); //
        }
    }

    aTOmWriter.close();
    nTOzWriter.close();

    searchers = new IndexSearcher[2];
    searchers[0] = new IndexSearcher(aTOmDirectory);
    searchers[1] = new IndexSearcher(nTOzDirectory);
}

From source file:aos.lucene.search.advanced.IndexSearcherTest.java

License:Apache License

public void testMulti() throws Exception {

    IndexSearcher searcher = new IndexSearcher(searchers);

    TermRangeQuery query = new TermRangeQuery("animal", //
            "h", //
            "t", //
            true, true);// #3

    TopDocs hits = searcher.search(query, 10);
    assertEquals("tarantula not included", 12, hits.totalHits);
}

From source file:aos.lucene.search.advanced.SecurityFilterTest.java

License:Apache License

protected void setUp() throws Exception {
    Directory directory = new RAMDirectory();
    IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(Version.LUCENE_46),
            IndexWriter.MaxFieldLength.UNLIMITED);

    Document document = new Document();
    document.add(new Field("owner", "elwood", Field.Store.YES, Field.Index.NOT_ANALYZED));
    document.add(new Field("keywords", "elwood's sensitive info", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(document);/*from   w  w w  . ja  va  2 s .c o m*/

    document = new Document();
    document.add(new Field("owner", "jake", Field.Store.YES, Field.Index.NOT_ANALYZED));
    document.add(new Field("keywords", "jake's sensitive info", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(document);

    writer.close();
    searcher = new IndexSearcher(directory);
}

From source file:aos.lucene.search.advanced.SortingExample.java

License:Apache License

public void displayResults(Query query, Sort sort) //
        throws IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    searcher.setDefaultFieldSortScoring(true, false); //

    TopDocs results = searcher.search(query, null, //
            20, sort); //

    LOGGER.info("\nResults for: " + //
            query.toString() + " sorted by " + sort);

    LOGGER.info(StringUtils.rightPad("Title", 30) + StringUtils.rightPad("pubmonth", 10)
            + StringUtils.center("id", 4) + StringUtils.center("score", 15));

    PrintStream out = new PrintStream(System.out, true, "UTF-8"); //

    DecimalFormat scoreFormatter = new DecimalFormat("0.######");
    for (ScoreDoc sd : results.scoreDocs) {
        int docID = sd.doc;
        float score = sd.score;
        Document doc = searcher.doc(docID);
        out.println(StringUtils.rightPad( //
                StringUtils.abbreviate(doc.get("title"), 29), 30) + //
                StringUtils.rightPad(doc.get("pubmonth"), 10) + //
                StringUtils.center("" + docID, 4) + //
                StringUtils.leftPad( //
                        scoreFormatter.format(score), 12)); //
        out.println("   " + doc.get("category"));
        //out.println(searcher.explain(query, docID));   //
    }// w ww. j  a  v a  2 s  .  co m

    searcher.close();
}

From source file:aos.lucene.search.advanced.SpanQueryTest.java

License:Apache License

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

      analyzer = new WhitespaceAnalyzer(Version.LUCENE_46);
      IndexWriter writer = new IndexWriter(directory, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

      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);// w w w  .  j a  v  a2  s.c om

      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(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:aos.lucene.search.advanced.TimeLimitingCollectorTest.java

License:Apache License

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

    TopScoreDocCollector topDocs = TopScoreDocCollector.create(10, false);
    Collector collector = new TimeLimitingCollector(topDocs, // #A
            1000); // #A
    try {/*from w ww  .  j  a  v  a  2  s.c om*/
        searcher.search(q, collector);
        assertEquals(numAllBooks, topDocs.getTotalHits()); // #B
    } catch (TimeExceededException tee) { // #C
        LOGGER.info("Too much time taken."); // #C
    } // #C
    searcher.close();
    dir.close();
}

From source file:aos.lucene.search.ext.collector.CollectorTest.java

License:Apache License

public void testCollecting() throws Exception {
    Directory dir = TestUtil.getBookIndexDirectory();
    TermQuery query = new TermQuery(new Term("contents", "junit"));
    IndexSearcher searcher = new IndexSearcher(dir);

    BookLinkCollector collector = new BookLinkCollector();
    searcher.search(query, collector);/*from   ww w  .  j  a  va 2s .  co  m*/

    Map<String, String> linkMap = collector.getLinks();
    assertEquals("ant in action", linkMap.get("http://www.manning.com/loughran"));

    TopDocs hits = searcher.search(query, 10);
    TestUtil.dumpHits(searcher, hits);

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