Search in sources :

Example 1 with SuperIndexSearcher

use of org.opensolaris.opengrok.configuration.SuperIndexSearcher in project OpenGrok by OpenGrok.

the class SearchHelper method getSuggestions.

/**
 * If a search did not return a hit, one may use this method to obtain
 * suggestions for a new search.
 *
 * <p>
 * Parameters which should be populated/set at this time: <ul>
 * <li>{@link #projects}</li> <li>{@link #dataRoot}</li>
 * <li>{@link #builder}</li> </ul>
 *
 * @return a possible empty list of suggestions.
 */
public List<Suggestion> getSuggestions() {
    if (projects == null) {
        return new ArrayList<>(0);
    }
    String[] name;
    if (projects.isEmpty()) {
        name = new String[] { "/" };
    } else if (projects.size() == 1) {
        name = new String[] { projects.first() };
    } else {
        name = new String[projects.size()];
        int ii = 0;
        for (String proj : projects) {
            name[ii++] = proj;
        }
    }
    List<Suggestion> res = new ArrayList<>();
    List<String> dummy = new ArrayList<>();
    FSDirectory dir;
    IndexReader ir = null;
    Term t;
    for (String proj : name) {
        Suggestion s = new Suggestion(proj);
        try {
            if (!closeOnDestroy) {
                SuperIndexSearcher searcher = RuntimeEnvironment.getInstance().getIndexSearcher(proj);
                searcherList.add(searcher);
                ir = searcher.getIndexReader();
            } else {
                dir = FSDirectory.open(new File(indexDir, proj).toPath());
                ir = DirectoryReader.open(dir);
            }
            if (builder.getFreetext() != null && !builder.getFreetext().isEmpty()) {
                t = new Term(QueryBuilder.FULL, builder.getFreetext());
                getSuggestion(t, ir, dummy);
                s.freetext = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            if (builder.getRefs() != null && !builder.getRefs().isEmpty()) {
                t = new Term(QueryBuilder.REFS, builder.getRefs());
                getSuggestion(t, ir, dummy);
                s.refs = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            if (builder.getDefs() != null && !builder.getDefs().isEmpty()) {
                t = new Term(QueryBuilder.DEFS, builder.getDefs());
                getSuggestion(t, ir, dummy);
                s.defs = dummy.toArray(new String[dummy.size()]);
                dummy.clear();
            }
            // TODO suggest also for path and history?
            if ((s.freetext != null && s.freetext.length > 0) || (s.defs != null && s.defs.length > 0) || (s.refs != null && s.refs.length > 0)) {
                res.add(s);
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, "Got exception while getting " + "spelling suggestions: ", e);
        } finally {
            if (ir != null && closeOnDestroy) {
                try {
                    ir.close();
                } catch (IOException ex) {
                    LOGGER.log(Level.WARNING, "Got exception while " + "getting spelling suggestions: ", ex);
                }
            }
        }
    }
    return res;
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) ArrayList(java.util.ArrayList) IndexReader(org.apache.lucene.index.IndexReader) FSDirectory(org.apache.lucene.store.FSDirectory) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IndexReader (org.apache.lucene.index.IndexReader)1 Term (org.apache.lucene.index.Term)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 SuperIndexSearcher (org.opensolaris.opengrok.configuration.SuperIndexSearcher)1