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: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);/*from w  w  w .  ja v a2s.  co m*/
    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.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);//from www  .  java  2s .  co  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.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(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"));
    searcher.close();/*from w  w  w . j a  v  a2 s . c om*/
}

From source file:aos.lucene.tools.HighlightTest.java

License:Apache License

public void testHits() throws Exception {
    IndexSearcher searcher = new IndexSearcher(TestUtil.getBookIndexDirectory());
    TermQuery query = new TermQuery(new Term("title", "action"));
    TopDocs hits = searcher.search(query, 10);

    QueryScorer scorer = new QueryScorer(query, "title");
    Highlighter highlighter = new Highlighter(scorer);
    highlighter.setTextFragmenter(new SimpleSpanFragmenter(scorer));

    Analyzer analyzer = new SimpleAnalyzer();

    for (ScoreDoc sd : hits.scoreDocs) {
        Document doc = searcher.doc(sd.doc);
        String title = doc.get("title");

        TokenStream stream = TokenSources.getAnyTokenStream(searcher.getIndexReader(), sd.doc, "title", doc,
                analyzer);/*  w  ww .  ja  v a2s .  com*/
        String fragment = highlighter.getBestFragment(stream, title);

        LOGGER.info(fragment);
    }
}

From source file:aos.lucene.tools.SpatialLuceneExample.java

License:Apache License

public void findNear(String what, double latitude, double longitude, double radius)
        throws CorruptIndexException, IOException {
    IndexSearcher searcher = new IndexSearcher(directory);

    DistanceQueryBuilder dq;/*  ww  w.jav  a  2s  .c om*/
    dq = new DistanceQueryBuilder(latitude, // #A
            longitude, // #A
            radius, // #A
            latField, // #A
            lngField, // #A
            tierPrefix, // #A
            true); // #A

    Query tq;
    if (what == null)
        tq = new TermQuery(new Term("metafile", "doc")); // #B
    else
        tq = new TermQuery(new Term("name", what));

    DistanceFieldComparatorSource dsort; // #C
    dsort = new DistanceFieldComparatorSource( // #C
            dq.getDistanceFilter()); // #C
    Sort sort = new Sort(new SortField("foo", dsort)); // #C

    TopDocs hits = searcher.search(tq, dq.getFilter(), 10, sort);

    Map<Integer, Double> distances = // #D
            dq.getDistanceFilter().getDistances(); // #D

    LOGGER.info("Number of results: " + hits.totalHits);
    LOGGER.info("Found:");
    for (ScoreDoc sd : hits.scoreDocs) {
        int docID = sd.doc;
        Document d = searcher.doc(docID);

        String name = d.get("name");
        double rsLat = NumericUtils.prefixCodedToDouble(d.get(latField));
        double rsLng = NumericUtils.prefixCodedToDouble(d.get(lngField));
        Double geo_distance = distances.get(docID);

        System.out.printf(name + ": %.2f Miles\n", geo_distance);
        LOGGER.info("\t\t(" + rsLat + "," + rsLng + ")");
    }
}

From source file:app.finder.topicsource.service.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.
 * //from  ww  w .j  av a  2s. 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 List<String> doSearch(BufferedReader in, IndexSearcher searcher, Query query, int hitsPerPage,
        boolean raw, boolean interactive) throws IOException {

    List<String> list = new ArrayList<String>();

    // Collect enough docs to show 5 pages
    TopDocs results = searcher.search(query, 5 * 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);

    for (int i = start; i < end; i++) {
        Document doc = searcher.doc(hits[i].doc);
        String path = doc.get("path");
        if (path != null) {

            //System.out.println((i + 1) + ". " + path);

            list.add(path);

            String title = doc.get("title");
            //            if (title != null) {
            //               System.out.println("   Title: " + doc.get("title"));
            //            }
        } else {
            System.out.println((i + 1) + ". " + "No path for this document.");
        }

    }

    return list;
}

From source file:app.SearchFiles.java

License:Apache License

/**
139   * This demonstrates a typical paging search scenario, where the search engine presents 
140   * pages of size n to the user. The user can then go to the next page if interested in
141   * the next hits./* w  w w .  ja va2  s  . co  m*/
142   * 
143   * When the query is executed for the first time, then only enough results are collected
144   * to fill 5 result pages. If the user wants to page beyond this limit, then the query
145   * is executed another time and all hits are collected.
146   * 
147   */
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);
    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 path = doc.get("path");
            if (path != null) {
                System.out.println((i + 1) + ". " + path);
                String title = doc.get("title");
                if (title != null) {
                    System.out.println("   Title: " + doc.get("title"));
                }
            } else {
                System.out.println((i + 1) + ". " + "No path 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:Application.mediaIndexer.java

public static void SearchFiles(String index, String queryString, String selected, TextArea results)
        throws IOException, ParseException {
    IndexReader reader = DirectoryReader.open(FSDirectory.open(Paths.get(index)));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer();
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
    QueryParser parser = new QueryParser(selected, analyzer);
    String line = queryString != null ? queryString : in.readLine();
    line = line.trim();//from  w  w  w . j ava2 s . c  o  m
    Query query = parser.parse(line);
    int maxHits = 100;
    TopDocs docsResults = searcher.search(query, maxHits);
    ScoreDoc[] hits = docsResults.scoreDocs;
    for (int i = 0; i < hits.length; i++) {
        Document doc = searcher.doc(hits[i].doc);
        results.appendText("Title: " + doc.get("title") + "\n");
        results.appendText("Artists: " + doc.get("xmpDM:artist") + "\n");
        results.appendText("Genre: " + doc.get("xmpDM:genre") + "\n");
        results.appendText("Year: " + doc.get("xmpDM:releaseDate") + "\n");
    }
    // Playlist.
    playlist.clear();
    for (int i = 0; i < hits.length; i++) {
        Document doc = searcher.doc(hits[i].doc);
        String path = doc.get("path");
        if (path != null)
            playlist.add(new File(path));
    }
    reader.close();
}

From source file:au.edu.unimelb.csse.servlet.PagingServlet.java

License:Apache License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    String pageNumParam = req.getParameter("j");
    String hash = req.getParameter("h");
    String prevQuery = req.getParameter("p");
    String prevCorpus = req.getParameter("c");
    String docNumsParam = req.getParameter("d");
    String totalHits = req.getParameter("t");
    if ((pageNumParam == null || hash == null || prevQuery == null || docNumsParam == null || prevCorpus == null
            || totalHits == null)//from   w ww.jav  a 2s  . c o  m
            || (hashValue(prevQuery, prevCorpus, docNumsParam, totalHits) != Integer.parseInt(hash))) {
        req.setAttribute("error", "Oops! An error has occurred.");
        logger.warning("Error searching: " + prevQuery + ". Incorrect hidden parameters in page.");
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
        return;
    }
    int requestedPage = Integer.valueOf(pageNumParam);

    String[] docStrings = docNumsParam.split(" ");
    int[] docNums = new int[docStrings.length];
    for (int i = 0; i < docStrings.length; i++) {
        docNums[i] = Integer.valueOf(docStrings[i]);
    }

    if (requestedPage - 1 > docNums.length) {
        req.setAttribute("error", "Oops! An error has occurred.");
        logger.warning("Error searching: " + prevQuery + ". Requested page exceeds number of result pages.");
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
        return;
    }

    String corpus = getCorpus(prevCorpus);
    req.setAttribute("corpus", corpus);
    res.setCharacterEncoding("UTF-8");

    IndexSearcher searcher = getSearcher(corpus, req, res);
    if (searcher == null) {
        req.setAttribute("error", "Oops! An error has occurred. Search engine not initialized.");
        logger.warning("Error searching: " + prevQuery + ". Search engine not initialized.");
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
        return;
    }

    String queryView = getReturnQuery(prevQuery);
    req.setAttribute("query-view", queryView);

    try {
        TreebankQuery tq = getTreebankQuery(req, res, corpus, prevQuery, pageNumParam);
        long start = System.nanoTime();
        SimpleHitCollector hitCollector = null;
        if (requestedPage == 1) {
            hitCollector = new SimpleHitCollector(MAX_RESULTS_PER_PAGE);
            searcher.search(tq, hitCollector);
        } else if (requestedPage % 10 < 6 && requestedPage % 10 > 1) {
            hitCollector = new SimpleHitCollector(MAX_RESULTS_PER_PAGE);
            searcher.truncatedSearch(tq, hitCollector, MAX_RESULTS_PER_PAGE, docNums[requestedPage - 2]);
        } else {
            if (requestedPage > docNums.length - 5) {
                int hitsToLoad = (docNums.length - requestedPage + 11) * MAX_RESULTS_PER_PAGE;
                hitCollector = new SimpleHitCollector(hitsToLoad);
                searcher.truncatedSearch(tq, hitCollector, hitsToLoad, docNums[requestedPage - 2]);
                int[] docs = hitCollector.lastDocOfEachPage(MAX_RESULTS_PER_PAGE);
                StringBuilder builder = new StringBuilder(docNumsParam);
                for (int i = docNums.length - requestedPage + 1; i < docs.length; i++) {
                    builder.append(" ");
                    builder.append(docs[i]);
                }
                docNumsParam = builder.toString();
            } else {
                // it has been previously loaded
                hitCollector = new SimpleHitCollector(MAX_RESULTS_PER_PAGE);
                searcher.truncatedSearch(tq, hitCollector, MAX_RESULTS_PER_PAGE, docNums[requestedPage - 2]);
            }
        }
        int numberOfResults = hitCollector.totalHits < MAX_RESULTS_PER_PAGE ? hitCollector.totalHits
                : MAX_RESULTS_PER_PAGE;
        AllResults allResults = new AllResults(hitCollector.hits, numberOfResults, tq);
        Result[] resultMeta = allResults.collect(searcher);
        long end = System.nanoTime();

        setSearchTimeAttribute(req, start, end);
        req.setAttribute("totalhits", Integer.valueOf(totalHits));
        req.setAttribute("pagenum", requestedPage);
        req.setAttribute("docnums", docNumsParam);
        req.setAttribute("hash", hashValue(prevQuery, prevCorpus, docNumsParam, totalHits)); //should hash prevQuery and not queryview
        String[] results = new String[numberOfResults];
        for (int i = 0; i < numberOfResults; i++) {
            results[i] = searcher.doc(hitCollector.hits[i]).get("sent").trim();
        }
        req.setAttribute("results", results);
        req.setAttribute("metadata", resultMeta);
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/results.jsp");
        view.forward(req, res);
    } catch (ParseException e) {
        req.setAttribute("error", "Sorry! Cannot parse your query");
        logger.info("Q=\"" + prevQuery + "\";C=\"" + corpus + "\";S=\"no\"");
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
    } catch (Exception e) {
        req.setAttribute("error",
                "Oops! An error has occurred. " + e.getMessage() + ". The administrator will be informed.");
        logger.warning("Error searching: " + prevQuery);
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
    }

}

From source file:au.edu.unimelb.csse.servlet.QueryServletFull.java

License:Apache License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    // req.setCharacterEncoding("utf-16");
    final String corpusParam = req.getParameter("corpus");
    String corpus = getCorpus(corpusParam);
    req.setAttribute("corpus", corpus);
    res.setCharacterEncoding("UTF-8");

    IndexSearcher searcher = getSearcher(corpus, req, res);
    if (searcher == null)
        return;/* w w  w. ja  v  a2s. c o m*/

    String query = getQuery(req, res);
    if (query == null)
        return;

    String queryView = getReturnQuery(query);
    req.setAttribute("query-view", queryView);

    try {
        TreebankQuery tq = getTreebankQuery(req, res, corpus, query, null);
        SimpleHitCollector hitCollector = new SimpleHitCollector(100);
        long start = System.nanoTime();
        searcher.search(tq, hitCollector);
        int numberOfResults = hitCollector.totalHits < MAX_RESULTS_PER_PAGE ? hitCollector.totalHits
                : MAX_RESULTS_PER_PAGE;
        AllResults allResults = new AllResults(hitCollector.hits, numberOfResults, tq);
        Result[] resultMeta = allResults.collect(searcher);
        long end = System.nanoTime();
        setSearchTimeAttribute(req, start, end);

        req.setAttribute("totalhits", hitCollector.totalHits);
        String[] results = new String[numberOfResults];
        for (int i = 0; i < numberOfResults; i++) {
            results[i] = searcher.doc(hitCollector.hits[i]).get("sent").trim();
        }
        req.setAttribute("results", results);
        req.setAttribute("metadata", resultMeta);

        // attributes for pagination
        int[] docNumInts = hitCollector.lastDocOfEachPage(MAX_RESULTS_PER_PAGE);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < docNumInts.length; i++) {
            sb.append(docNumInts[i] + " ");
        }
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        req.setAttribute("pagenum", 1);
        final String docNums = sb.toString();
        req.setAttribute("docnums", docNums);
        req.setAttribute("hash",
                hashValue(query, corpusParam, docNums, String.valueOf(hitCollector.totalHits))); //should hash value of `query' and not `queryview' 
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/results.jsp");
        view.forward(req, res);
    } catch (ParseException e) {
        req.setAttribute("error", "Sorry! Cannot parse your query");
        logger.info("Q=\"" + query + "\";C=\"" + corpus + "\";S=\"no\"");
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
    } catch (Exception e) {
        req.setAttribute("error",
                "Oops! An error has occurred. " + e.getMessage() + ". The administrator will be informed.");
        logger.severe("Error searching: " + query);
        logger.severe(e.getMessage());
        RequestDispatcher view = req.getRequestDispatcher("/WEB-INF/error.jsp");
        view.forward(req, res);
    }
}