Example usage for org.apache.lucene.index IndexReader totalTermFreq

List of usage examples for org.apache.lucene.index IndexReader totalTermFreq

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexReader totalTermFreq.

Prototype

public abstract long totalTermFreq(Term term) throws IOException;

Source Link

Document

Returns the total number of occurrences of term across all documents (the sum of the freq() for each doc that has this term).

Usage

From source file:servlets.TermStatsComparator.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w w w .  j  a v  a 2s .c  om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        IndexReader reader = retriever.getReader();
        String term = request.getParameter("term");

        if (isNumber(term)) {
            TermStats[] termStats = null;
            try {
                termStats = HighFreqTerms.getHighFreqTerms(reader, Integer.parseInt(term),
                        TrecDocRetriever.FIELD_ANALYZED_CONTENT, new TermStatsComparator());
            } catch (Exception ex) {
                out.println("Error in obtaining term stats");
            }
            if (termStats == null)
                out.println("Error in obtaining term stats");

            StringBuffer responseBuff = new StringBuffer("<table><tbody>");
            responseBuff.append("<tr>").append("<th>").append("Term").append("</th>").append("<th>")
                    .append("Doc Freq").append("</th>").append("<th>").append("Coll Freq").append("</th>")
                    .append("</tr>");

            for (TermStats ts : termStats) {
                responseBuff.append("<tr>").append("<td>").append(ts.termtext.utf8ToString()).append("</td>")
                        .append("<td>").append(ts.docFreq).append("</td>").append("<td>")
                        .append(ts.totalTermFreq).append("</td>").append("</tr>");
            }
            responseBuff.append("</tbody></table>");
            out.println(responseBuff.toString());
        } else {
            String analyzedTerm = analyze(term);
            Term t = new Term(TrecDocRetriever.FIELD_ANALYZED_CONTENT, analyzedTerm);
            int docFreq = reader.docFreq(t);
            long collFreq = reader.totalTermFreq(t);
            out.println("Doc freq: " + docFreq + "&nbsp;&nbsp;" + "Coll Freq: " + collFreq);
        }
    }
}