Search in sources :

Example 1 with Context

use of org.opengrok.indexer.search.context.Context 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 Context

use of org.opengrok.indexer.search.context.Context 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

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 HistoryContext (org.opengrok.indexer.search.context.HistoryContext)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 CompatibleAnalyser (org.opengrok.indexer.analysis.CompatibleAnalyser)1 HistoryException (org.opengrok.indexer.history.HistoryException)1 Summarizer (org.opengrok.indexer.search.Summarizer)1 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)1