Search in sources :

Example 1 with SuperIndexSearcher

use of org.opengrok.indexer.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 suggestion = 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);
                suggestion.setFreetext(dummy.toArray(new String[0]));
                dummy.clear();
            }
            if (builder.getRefs() != null && !builder.getRefs().isEmpty()) {
                t = new Term(QueryBuilder.REFS, builder.getRefs());
                getSuggestion(t, ir, dummy);
                suggestion.setRefs(dummy.toArray(new String[0]));
                dummy.clear();
            }
            if (builder.getDefs() != null && !builder.getDefs().isEmpty()) {
                t = new Term(QueryBuilder.DEFS, builder.getDefs());
                getSuggestion(t, ir, dummy);
                suggestion.setDefs(dummy.toArray(new String[0]));
                dummy.clear();
            }
            // TODO suggest also for path and history?
            if (suggestion.isUsable()) {
                res.add(suggestion);
            }
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, String.format("Got exception while getting spelling suggestions for project %s:", proj), 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.opengrok.indexer.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)

Example 2 with SuperIndexSearcher

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

the class SuggesterServiceImpl method getSuggestions.

/**
 * {@inheritDoc}
 */
@Override
public Suggestions getSuggestions(final Collection<String> projects, final SuggesterQuery suggesterQuery, final Query query) {
    List<SuperIndexSearcher> superIndexSearchers = new LinkedList<>();
    lock.readLock().lock();
    try {
        if (suggester == null) {
            return new Suggestions(Collections.emptyList(), true);
        }
        List<NamedIndexReader> namedReaders = getNamedIndexReaders(projects, superIndexSearchers);
        return suggester.search(namedReaders, suggesterQuery, query);
    } finally {
        lock.readLock().unlock();
        for (SuperIndexSearcher s : superIndexSearchers) {
            try {
                s.getSearcherManager().release(s);
            } catch (IOException e) {
                logger.log(Level.WARNING, "Could not release " + s, e);
            }
        }
    }
}
Also used : Suggestions(org.opengrok.suggest.Suggester.Suggestions) SuperIndexSearcher(org.opengrok.indexer.configuration.SuperIndexSearcher) NamedIndexReader(org.opengrok.suggest.Suggester.NamedIndexReader) IOException(java.io.IOException) LinkedList(java.util.LinkedList)

Example 3 with SuperIndexSearcher

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

the class SuggesterServiceImpl method getNamedIndexReaders.

private List<NamedIndexReader> getNamedIndexReaders(final Collection<String> projects, final List<SuperIndexSearcher> superIndexSearchers) {
    if (env.isProjectsEnabled()) {
        return projects.stream().map(project -> {
            try {
                SuperIndexSearcher searcher = env.getIndexSearcher(project);
                superIndexSearchers.add(searcher);
                return new NamedIndexReader(project, searcher.getIndexReader());
            } catch (IOException e) {
                logger.log(Level.WARNING, "Could not get index reader for " + project, e);
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    } else {
        SuperIndexSearcher searcher;
        try {
            searcher = env.getIndexSearcher("");
            superIndexSearchers.add(searcher);
            return Collections.singletonList(new NamedIndexReader("", searcher.getIndexReader()));
        } catch (IOException e) {
            logger.log(Level.WARNING, "Could not get index reader", e);
        }
        return Collections.emptyList();
    }
}
Also used : SuperIndexSearcher(org.opengrok.indexer.configuration.SuperIndexSearcher) NamedIndexReader(org.opengrok.suggest.Suggester.NamedIndexReader) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)3 SuperIndexSearcher (org.opengrok.indexer.configuration.SuperIndexSearcher)3 NamedIndexReader (org.opengrok.suggest.Suggester.NamedIndexReader)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 IndexReader (org.apache.lucene.index.IndexReader)1 Term (org.apache.lucene.index.Term)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 Suggestions (org.opengrok.suggest.Suggester.Suggestions)1