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:SearchFilesTest.java

License:Apache License

public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query) throws IOException {

    TopDocs results = searcher.search(query, 100);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    for (int i = 0; i < hits.length; i++) {
        Document doc = searcher.doc(hits[i].doc);
        String path = doc.get("path");
        if (path != null) {
            System.out.println((i + 1) + ". " + path + " score = " + hits[i].score);
            String title = doc.get("title");
            if (title != null) {
                System.out.println("   Title: " + doc.get("title"));
            }//from   ww  w. j a v a2s .co m

        } else {
            System.out.println((i + 1) + ". " + "No path for this document");
        }
    }
}

From source file:luceneInterface.java

License:Apache License

public static List<Document> query(String index, String stoppath, String question, int numResult, String sim)
        throws Exception {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);

    Analyzer analyzer = new EnglishAnalyzer(StopFilter.makeStopSet(mygetStopwords(stoppath)));

    if (sim.equals("TFIDF"))
        searcher.setSimilarity(new ClassicSimilarity());
    else if (sim.equals("BM25"))
        searcher.setSimilarity(new BM25Similarity());
    else//from  w w  w.j  av a 2 s  .  co m
        searcher.setSimilarity(new BM25Similarity());

    String field = "contents";
    QueryParser parser = new QueryParser(field, analyzer);
    Query query = parser.parse(parser.escape(question));

    BooleanQuery.Builder bqb = new BooleanQuery.Builder();
    bqb.add(new TermQuery(new Term("contents", parser.escape(question))), BooleanClause.Occur.SHOULD);
    bqb.add(new TermQuery(new Term("sec", parser.escape(question))), BooleanClause.Occur.SHOULD);

    //        Term term = new Term(field, question);
    //        Query query = new TermQuery(term);

    //        TopDocs results = searcher.search(query, numResult);
    TopDocs results = searcher.search(parser.parse(bqb.build().toString()), numResult);

    ScoreDoc[] hits = results.scoreDocs;
    List<Document> docs = new ArrayList<Document>();

    int numTotalHits = results.totalHits;
    //        System.out.println(numTotalHits + " total matching documents");

    int end = Math.min(numTotalHits, numResult);

    String searchResult = "";
    //        System.out.println("Only results 1 - " + hits.length);

    for (int i = 0; i < end; i++) {
        Document doc = searcher.doc(hits[i].doc);
        docs.add(doc);
    }

    return docs;
}

From source file:LuceneSearchFiles.java

License:Apache License

/** Simple command-line based search demo. */
public static void search(String phrase, String field, int hitsPerPage) {
    try {/*from   w  w w  . j  ava 2 s  .  c om*/
        IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(m_index)));
        IndexSearcher searcher = new IndexSearcher(reader);
        Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);

        QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);

        Query query = parser.parse(phrase);
        System.out.println("Searching for: " + query.toString(field));

        searcher.search(query, null, hitsPerPage);

        TopDocs results = searcher.search(query, hitsPerPage);
        ScoreDoc[] hits = results.scoreDocs;

        for (ScoreDoc hit : hits) {
            Document doc = searcher.doc(hit.doc);
            //String path = doc.get("path");   
            String title = doc.get("title");
            System.out.println(hit.score + " -" + title);
        }
        reader.close();
    } catch (IOException e) {

    } catch (ParseException e) {

    }
}

From source file:action.meetlucene.Searcher.java

License:Apache License

public static void search(String indexDir, String q) throws IOException, ParseException {

    Directory dir = FSDirectory.open(new File(indexDir)); //3
    IndexSearcher is = new IndexSearcher(dir); //3   

    QueryParser parser = new QueryParser(Version.LUCENE_30, "contents",
            new StandardAnalyzer(Version.LUCENE_30)); //4
    Query query = parser.parse(q); //4   
    long start = System.currentTimeMillis();
    TopDocs hits = is.search(query, 10); //5
    long end = System.currentTimeMillis();

    System.err.println("Found " + hits.totalHits + //6  
            " document(s) (in " + (end - start) + // 6
            " milliseconds) that matched query '" + // 6
            q + "':"); // 6

    for (ScoreDoc scoreDoc : hits.scoreDocs) {
        Document doc = is.doc(scoreDoc.doc); //7      
        System.out.println(doc.get("fullpath")); //8  
    }//from  w w w .j a  va  2 s  .c  om

    is.close(); //9
}

From source file:action.searching.BasicSearchingTest.java

License:Apache License

public void testQueryParser() throws Exception {
    Directory dir = FSDirectory.open(new File(System.getProperty("index.dir")));
    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);//from   w ww .  ja v  a2  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);

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

From source file:antnlp.opie.indexsearch.SearchFiles.java

License:Apache License

/**
 * This demonstrates a typical paging search scenario, where the search engine presents 
 * pages of size n to the user. The user can then go to the next page if interested in
 * the next hits.//ww  w . j a  v a  2 s. co m
 * 
 * When the query is executed for the first time, then only enough results are collected
 * to fill 5 result pages. If the user wants to page beyond this limit, then the query
 * is executed another time and all hits are collected.
 * 
 */
public static void doPagingSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,
        boolean raw, boolean interactive) throws IOException {

    // Collect enough docs to show 5 pages
    //TopDocs results = searcher.search(query, 5 * hitsPerPage);
    TopDocs results = searcher.search(query, hitsPerPage);
    ScoreDoc[] hits = results.scoreDocs;

    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching documents");

    int start = 0;
    int end = Math.min(numTotalHits, hitsPerPage);

    while (true) {
        if (end > hits.length) {
            System.out.println("Only results 1 - " + hits.length + " of " + numTotalHits
                    + " total matching documents collected.");
            System.out.println("Collect more (y/n) ?");
            String line = in.readLine();
            if (line.length() == 0 || line.charAt(0) == 'n') {
                break;
            }

            hits = searcher.search(query, numTotalHits).scoreDocs;
        }

        end = Math.min(hits.length, start + hitsPerPage);

        for (int i = start; i < end; i++) {
            if (raw) { // output raw format
                System.out.println("doc=" + hits[i].doc + " score=" + hits[i].score);
                continue;
            }

            Document doc = searcher.doc(hits[i].doc);
            String docid = doc.get("docid");
            if (docid != null) {
                System.out.println((i + 1) + ". " + docid);
                String title = doc.get("title");
                if (title != null) {
                    System.out.println("   Title: " + doc.get("title"));
                }
            } else {
                System.out.println((i + 1) + ". " + "No docid for this document");
            }

        }

        if (!interactive || end == 0) {
            break;
        }

        if (numTotalHits >= end) {
            boolean quit = false;
            while (true) {
                System.out.print("Press ");
                if (start - hitsPerPage >= 0) {
                    System.out.print("(p)revious page, ");
                }
                if (start + hitsPerPage < numTotalHits) {
                    System.out.print("(n)ext page, ");
                }
                System.out.println("(q)uit or enter number to jump to a page.");

                String line = in.readLine();
                if (line.length() == 0 || line.charAt(0) == 'q') {
                    quit = true;
                    break;
                }
                if (line.charAt(0) == 'p') {
                    start = Math.max(0, start - hitsPerPage);
                    break;
                } else if (line.charAt(0) == 'n') {
                    if (start + hitsPerPage < numTotalHits) {
                        start += hitsPerPage;
                    }
                    break;
                } else {
                    int page = Integer.parseInt(line);
                    if ((page - 1) * hitsPerPage < numTotalHits) {
                        start = (page - 1) * hitsPerPage;
                        break;
                    } else {
                        System.out.println("No such page");
                    }
                }
            }
            if (quit)
                break;
            end = Math.min(numTotalHits, start + hitsPerPage);
        }
    }
}

From source file:aos.lucene.analysis.codec.MetaphoneAnalyzerTest.java

License:Apache License

public void testKoolKat() throws Exception {

    RAMDirectory directory = new RAMDirectory();
    Analyzer analyzer = new MetaphoneReplacementAnalyzer();

    IndexWriter writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);

    Document doc = new Document();
    doc.add(new Field("contents", "cool cat", Field.Store.YES, Field.Index.ANALYZED));
    writer.addDocument(doc);/* ww w . j a v  a2s .com*/
    writer.close();

    IndexSearcher searcher = new IndexSearcher(directory);

    Query query = new QueryParser(Version.LUCENE_46, "contents", analyzer).parse("kool kat");

    TopDocs hits = searcher.search(query, 1);
    assertEquals(1, hits.totalHits);
    int docID = hits.scoreDocs[0].doc;
    Document storedDoc = searcher.doc(docID);
    assertEquals("cool cat", storedDoc.get("contents"));

    searcher.close();
}

From source file:aos.lucene.remote.SearchClient.java

License:Apache License

private static void search(String name, String word) throws Exception {
    TermQuery query = new TermQuery(new Term("word", word));

    IndexSearcher searcher = (IndexSearcher) searcherCache.get(name);

    if (searcher == null) {
        searcher = new IndexSearcher(new IndexSearcher[] { lookupRemote(name) });
        searcherCache.put(name, searcher);
    }/*from w ww.  j av a2s . c o  m*/

    long begin = new Date().getTime();
    TopDocs hits = searcher.search(query, 10);
    long end = new Date().getTime();

    System.out.print("Searched " + name + " for '" + word + "' (" + (end - begin) + " ms): ");

    if (hits.scoreDocs.length == 0) {
        System.out.print("<NONE FOUND>");
    }

    for (ScoreDoc sd : hits.scoreDocs) {
        Document doc = searcher.doc(sd.doc);
        String[] values = doc.getValues("syn");
        for (String syn : values) {
            System.out.print(syn + " ");
        }
    }
    LOGGER.info();
    LOGGER.info();

}

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. jav a 2  s  .  co m*/

    searcher.close();
}

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 w w  w  .  ja v a  2s  . 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();
}