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

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

Introduction

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

Prototype

public Document doc(int docID) throws IOException 

Source Link

Document

Sugar for .getIndexReader().document(docID)

Usage

From source file:lia.chapter3.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    IndexSearcher searcher = Utils.getBookIndexSearcher();
    QueryParser parser = new QueryParser("content", new WhitespaceAnalyzer());
    Query query = parser.parse("+JUNIT +ANT -MOCK"); //B
    TopDocs docs = searcher.search(query, 10);
    assertEquals(1, docs.totalHits);/*from ww  w  .j  a  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"); //B
    docs = searcher.search(query, 10);
    assertEquals("Ant in Action, " + "JUnit in Action, Second Edition", 2, docs.totalHits);

}

From source file:lia.chapter5.SortingExample.java

License:Apache License

public void displayResults(Query query, Sort sort) // #1
        throws IOException {
    IndexSearcher searcher = Utils.getBookIndexSearcher();

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

    System.out.println("\nResults for: " + // #4
            query.toString() + " sorted by " + sort);

    /*   System.out.println(StringUtils.rightPad("Title", 30) +
        StringUtils.rightPad("pubmonth", 10) +
        StringUtils.center("id", 4) +//from  w  w w. j  a va2  s .c  o  m
        StringUtils.center("score", 15));*/

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

    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(                                                  // #6
                    StringUtils.abbreviate(doc.get("title"), 29), 30) +                // #6
                    StringUtils.rightPad(doc.get("pubmonth"), 10) +                        // #6
                    StringUtils.center("" + docID, 4) +                                    // #6
                    StringUtils.leftPad(                                                   // #6
                            scoreFormatter.format(score), 12));   */ // #6
        out.println("   " + doc.get("category"));
        //out.println(searcher.explain(query, docID));   // #7
    }

}

From source file: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);
    //    }// www  . ja va  2 s.  com

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

    String indexDir = "indexes/MeetLucene";
    String queryExpression = "junit";

    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:lia.searching.TermRangeQueryTest.java

License:Apache License

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

    TopDocs matches = searcher.search(query, 100);

    for (int i = 0; i < matches.totalHits; i++) {
        System.out.println("match " + i + ": " + searcher.doc(matches.scoreDocs[i].doc).get("title2"));
    }//from www  .  j ava2  s  . c  o m

    assertEquals(3, matches.totalHits);
    searcher.close();
    dir.close();
}

From source file:lucandra.LucandraTests.java

License:Apache License

public void testUnicode() throws Exception {
    IndexReader indexReader = new IndexReader(indexName, client);
    IndexSearcher searcher = new IndexSearcher(indexReader);

    QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "key", analyzer);
    Query q = qp.parse("+key:\u5639\u563b");

    TopDocs docs = searcher.search(q, 10);

    assertEquals(1, docs.totalHits);/*w w  w.  ja v  a2 s.  c o  m*/

    Document doc = searcher.doc(docs.scoreDocs[0].doc);

    assertNotNull(doc.getField("key"));
}

From source file:lucandra.LucandraTests.java

License:Apache License

public void testMultiValuedFields() throws Exception {

    IndexReader indexReader = new IndexReader(indexName, client);
    IndexSearcher searcher = new IndexSearcher(indexReader);

    QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "key", analyzer);
    Query q = qp.parse("+key:samefield");

    TopDocs docs = searcher.search(q, 10);

    assertEquals(1, docs.totalHits);//from   w w w. j  a  va 2 s  .c o m

    Document doc = searcher.doc(docs.scoreDocs[0].doc);

    Field[] fields = doc.getFields("key");
    String[] tests = new String[] { "\u5639\u563b", "samefield" };

    assertEquals(2, fields.length);

    for (int i = 0; i < fields.length; i++) {
        assertEquals(tests[i], fields[i].stringValue());
    }

}

From source file:lucandra.LucandraTests.java

License:Apache License

public void testSearch() throws Exception {

    IndexReader indexReader = new IndexReader(indexName, client);
    IndexSearcher searcher = new IndexSearcher(indexReader);

    QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "key", analyzer);
    Query q = qp.parse("+key:another");

    TopDocs docs = searcher.search(q, 10);

    assertEquals(1, docs.totalHits);//from   ww  w  .  j a va 2 s  .  co  m

    Document doc = searcher.doc(docs.scoreDocs[0].doc);

    assertNotNull(doc.getField("key"));
}

From source file:lucandra.LucandraTests.java

License:Apache License

public void testScore() throws Exception {

    IndexReader indexReader = new IndexReader(indexName, client);
    IndexSearcher searcher = new IndexSearcher(indexReader);

    QueryParser qp = new QueryParser(Version.LUCENE_CURRENT, "key", analyzer);
    Query q = qp.parse("+key:example");

    TopDocs docs = searcher.search(q, 10);

    assertEquals(2, docs.totalHits);/*w w  w .j  ava 2  s. co m*/

    Document doc = searcher.doc(docs.scoreDocs[0].doc);

    String fld = doc.getField("key").stringValue();
    // Highest scoring doc should be the one with higher boost
    assertEquals(fld, "this is another example");

}

From source file:lucandra.NumericRangeTests.java

License:Apache License

@Test
public void testLongRangeInclusive() throws Exception {

    NumericRangeQuery query = NumericRangeQuery.newLongRange("long", mid, null, true, true);

    IndexReader reader = new IndexReader(indexName, connection);

    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs docs = searcher.search(query, 1000);

    assertEquals(2, docs.totalHits);//from   www .  j  av a2s  . c  o  m

    Set<String> results = new HashSet<String>();

    for (ScoreDoc doc : docs.scoreDocs) {
        Document returned = searcher.doc(doc.doc);
        results.add(returned.get("Id"));
    }

    assertTrue(results.contains("second"));

    assertTrue(results.contains("third"));

}

From source file:lucandra.NumericRangeTests.java

License:Apache License

@Test
public void testLongRangeExclusive() throws Exception {

    // now we'll query from the middle inclusive

    NumericRangeQuery query = NumericRangeQuery.newLongRange("long", mid, null, false, true);

    IndexReader reader = new IndexReader(indexName, connection);

    IndexSearcher searcher = new IndexSearcher(reader);

    TopDocs docs = searcher.search(query, 1000);

    assertEquals(1, docs.totalHits);//  w  w w  . j  a  v a2  s.co m

    Set<String> results = new HashSet<String>();

    for (ScoreDoc doc : docs.scoreDocs) {
        Document returned = searcher.doc(doc.doc);
        results.add(returned.get("Id"));
    }

    assertTrue(results.contains("third"));

}