Search in sources :

Example 1 with HistoryContext

use of org.opengrok.indexer.search.context.HistoryContext in project OpenGrok by OpenGrok.

the class SearchEngine method search.

/**
 * Execute a search on projects or root file.
 *
 * If @param projects is an empty list it tries to search in @code
 * searchSingleDatabase with root set to @param root
 *
 * Call to search() must be eventually followed by call to destroy()
 * so that IndexSearcher objects are properly freed.
 *
 * @return The number of hits
 */
private int search(List<Project> projects, File root) {
    source = RuntimeEnvironment.getInstance().getSourceRootPath();
    data = RuntimeEnvironment.getInstance().getDataRootPath();
    docs.clear();
    QueryBuilder newBuilder = createQueryBuilder();
    try {
        query = newBuilder.build();
        if (query != null) {
            if (projects.isEmpty()) {
                // search the index database
                // NOTE this assumes that src does not contain any project, just
                // data files - so no authorization can be enforced
                searchSingleDatabase(root, true);
            } else {
                // search all projects
                // TODO support paging per project (in search.java)
                // TODO optimize if only one project by falling back to SingleDatabase ?
                // NOTE projects are already filtered if we accessed through web page @see search(HttpServletRequest)
                searchMultiDatabase(projects, false);
            }
        }
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, SEARCH_EXCEPTION_MSG, e);
    }
    if (!docs.isEmpty()) {
        sourceContext = null;
        summarizer = null;
        try {
            sourceContext = new Context(query, newBuilder);
            if (sourceContext.isEmpty()) {
                sourceContext = null;
            }
            summarizer = new Summarizer(query, analyzer);
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "An error occurred while creating summary", e);
        }
        historyContext = null;
        try {
            historyContext = new HistoryContext(query);
            if (historyContext.isEmpty()) {
                historyContext = null;
            }
        } catch (Exception e) {
            LOGGER.log(Level.WARNING, "An error occurred while getting history context", e);
        }
    }
    int count = hits == null ? 0 : hits.length;
    queryBuilder = newBuilder;
    return count;
}
Also used : HistoryContext(org.opengrok.indexer.search.context.HistoryContext) Context(org.opengrok.indexer.search.context.Context) HistoryContext(org.opengrok.indexer.search.context.HistoryContext) ParseException(org.apache.lucene.queryparser.classic.ParseException) HistoryException(org.opengrok.indexer.history.HistoryException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with HistoryContext

use of org.opengrok.indexer.search.context.HistoryContext in project OpenGrok by OpenGrok.

the class Results method prettyPrint.

/**
 * Prints out results in html form. The following search helper fields are
 * required to be properly initialized: <ul>
 * <li>{@link SearchHelper#dataRoot}</li>
 * <li>{@link SearchHelper#contextPath}</li>
 * <li>{@link SearchHelper#searcher}</li> <li>{@link SearchHelper#hits}</li>
 * <li>{@link SearchHelper#historyContext} (ignored if {@code null})</li>
 * <li>{@link SearchHelper#sourceContext} (ignored if {@code null})</li>
 * <li>{@link SearchHelper#summarizer} (if sourceContext is not
 * {@code null})</li> <li>{@link SearchHelper#sourceRoot} (if
 * sourceContext or historyContext is not {@code null})</li> </ul>
 *
 * @param out write destination
 * @param sh search helper which has all required fields set
 * @param start index of the first hit to print
 * @param end index of the last hit to print
 * @throws HistoryException history exception
 * @throws IOException I/O exception
 * @throws ClassNotFoundException class not found
 */
public static void prettyPrint(Writer out, SearchHelper sh, int start, long end) throws HistoryException, IOException, ClassNotFoundException {
    Project p;
    String contextPath = sh.getContextPath();
    String ctxE = Util.uriEncodePath(contextPath);
    String xrefPrefix = contextPath + Prefix.XREF_P;
    String morePrefix = contextPath + Prefix.MORE_P;
    String xrefPrefixE = ctxE + Prefix.XREF_P;
    File xrefDataDir = new File(sh.getDataRoot(), Prefix.XREF_P.toString());
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    boolean evenRow = true;
    out.write("<tbody class=\"search-result\">");
    for (Map.Entry<String, ArrayList<Integer>> entry : createMap(sh.getSearcher(), sh.getHits(), start, end).entrySet()) {
        String parent = entry.getKey();
        out.write("<tr class=\"dir\"><td colspan=\"3\"><a href=\"");
        out.write(xrefPrefixE);
        out.write(Util.uriEncodePath(parent));
        out.write("/\">");
        out.write(htmlize(parent));
        out.write("/</a>");
        if (sh.getDesc() != null) {
            out.write(" - <i>");
            out.write(sh.getDesc().get(parent));
            out.write("</i>");
        }
        p = Project.getProject(parent);
        String messages = MessagesUtils.messagesToJson(p, MESSAGES_MAIN_PAGE_TAG);
        if (p != null && !messages.isEmpty()) {
            out.write(" <a href=\"" + xrefPrefix + "/" + p.getName() + "\">");
            out.write("<span class=\"note-" + MessagesUtils.getMessageLevel(p.getName(), MESSAGES_MAIN_PAGE_TAG) + " important-note important-note-rounded\" data-messages='" + messages + "'>!</span>");
            out.write("</a>");
        }
        int tabSize = sh.getTabSize(p);
        PrintPlainFinalArgs fargs = new PrintPlainFinalArgs(out, sh, env, xrefPrefix, tabSize, morePrefix);
        out.write("</td></tr>");
        for (int docId : entry.getValue()) {
            Document doc = sh.getSearcher().doc(docId);
            String rpath = doc.get(QueryBuilder.PATH);
            String rpathE = Util.uriEncodePath(rpath);
            if (evenRow) {
                out.write("<tr class=\"search-result-even-row\">");
            } else {
                out.write("<tr>");
            }
            evenRow = !evenRow;
            Util.writeHAD(out, sh.getContextPath(), rpathE, false);
            out.write("<td class=\"f\"><a href=\"");
            out.write(xrefPrefixE);
            out.write(rpathE);
            out.write("\"");
            if (env.isLastEditedDisplayMode()) {
                printLastEditedDate(out, doc);
            }
            out.write(">");
            out.write(htmlize(rpath.substring(rpath.lastIndexOf('/') + 1)));
            out.write("</a>");
            out.write("</td><td><code class=\"con\">");
            if (sh.getSourceContext() != null) {
                AbstractAnalyzer.Genre genre = AbstractAnalyzer.Genre.get(doc.get(QueryBuilder.T));
                Summarizer summarizer = sh.getSummarizer();
                if (AbstractAnalyzer.Genre.XREFABLE == genre && summarizer != null) {
                    String xtags = getTags(xrefDataDir, rpath, env.isCompressXref());
                    // FIXME use Highlighter from lucene contrib here,
                    // instead of summarizer, we'd also get rid of
                    // apache lucene in whole source ...
                    out.write(summarizer.getSummary(xtags).toString());
                } else if (AbstractAnalyzer.Genre.HTML == genre && summarizer != null) {
                    String htags = getTags(sh.getSourceRoot(), rpath, false);
                    out.write(summarizer.getSummary(htags).toString());
                } else if (genre == AbstractAnalyzer.Genre.PLAIN) {
                    printPlain(fargs, doc, docId, rpath);
                }
            }
            HistoryContext historyContext = sh.getHistoryContext();
            if (historyContext != null) {
                historyContext.getContext(new File(sh.getSourceRoot(), rpath), rpath, out, sh.getContextPath());
            }
            out.write("</code></td></tr>\n");
        }
    }
    out.write("</tbody>");
}
Also used : RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) HistoryContext(org.opengrok.indexer.search.context.HistoryContext) ArrayList(java.util.ArrayList) Document(org.apache.lucene.document.Document) Project(org.opengrok.indexer.configuration.Project) AbstractAnalyzer(org.opengrok.indexer.analysis.AbstractAnalyzer) File(java.io.File) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with HistoryContext

use of org.opengrok.indexer.search.context.HistoryContext in project OpenGrok by OpenGrok.

the class SearchHelper method prepareSummary.

/**
 * Prepare the fields to support printing a full blown summary. Does nothing
 * if {@link #redirect} or {@link #errorMsg} have a none-{@code null} value.
 *
 * <p>
 * Parameters which should be populated/set at this time: <ul>
 * <li>{@link #query}</li> <li>{@link #builder}</li> </ul> Populates/sets:
 * Otherwise the following fields are set (includes {@code null}): <ul>
 * <li>{@link #sourceContext}</li> <li>{@link #summarizer}</li>
 * <li>{@link #historyContext}</li> </ul>
 *
 * @return this instance.
 */
public SearchHelper prepareSummary() {
    if (redirect != null || errorMsg != null) {
        return this;
    }
    try {
        sourceContext = new Context(query, builder);
        summarizer = new Summarizer(query, new CompatibleAnalyser());
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "Summarizer: {0}", e.getMessage());
    }
    try {
        historyContext = new HistoryContext(query);
    } catch (Exception e) {
        LOGGER.log(Level.WARNING, "HistoryContext: {0}", e.getMessage());
    }
    return this;
}
Also used : HistoryContext(org.opengrok.indexer.search.context.HistoryContext) Context(org.opengrok.indexer.search.context.Context) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) CompatibleAnalyser(org.opengrok.indexer.analysis.CompatibleAnalyser) HistoryContext(org.opengrok.indexer.search.context.HistoryContext) Summarizer(org.opengrok.indexer.search.Summarizer) ForbiddenSymlinkException(org.opengrok.indexer.util.ForbiddenSymlinkException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.lucene.queryparser.classic.ParseException) IOException(java.io.IOException)

Aggregations

HistoryContext (org.opengrok.indexer.search.context.HistoryContext)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ParseException (org.apache.lucene.queryparser.classic.ParseException)2 Context (org.opengrok.indexer.search.context.Context)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Document (org.apache.lucene.document.Document)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)1 CompatibleAnalyser (org.opengrok.indexer.analysis.CompatibleAnalyser)1 Project (org.opengrok.indexer.configuration.Project)1 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)1 HistoryException (org.opengrok.indexer.history.HistoryException)1 Summarizer (org.opengrok.indexer.search.Summarizer)1 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)1