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;
}
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);
}
}
}
}
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();
}
}
Aggregations