use of org.apache.lucene.index.MultiReader in project OpenGrok by OpenGrok.
the class SearchEngine method searchMultiDatabase.
/**
* Perform search on multiple indexes in parallel.
* @param paging whether to use paging (if yes, first X pages will load
* faster)
* @param root list of projects to search
* @throws IOException
*/
private void searchMultiDatabase(List<Project> root, boolean paging) throws IOException {
SortedSet<String> projects = new TreeSet<>();
for (Project p : root) {
projects.add(p.getName());
}
// We use MultiReader even for single project. This should
// not matter given that MultiReader is just a cheap wrapper
// around set of IndexReader objects.
MultiReader searchables = RuntimeEnvironment.getInstance().getMultiReader(projects, searcherList);
searcher = new IndexSearcher(searchables);
collector = TopScoreDocCollector.create(hitsPerPage * cachePages);
searcher.search(query, collector);
totalHits = collector.getTotalHits();
if (!paging && totalHits > 0) {
collector = TopScoreDocCollector.create(totalHits);
searcher.search(query, collector);
}
hits = collector.topDocs().scoreDocs;
for (ScoreDoc hit : hits) {
int docId = hit.doc;
Document d = searcher.doc(docId);
docs.add(d);
}
}
use of org.apache.lucene.index.MultiReader in project neo4j-mobile-android by neo4j-contrib.
the class LuceneIndex method search.
private IndexHits<Document> search(IndexSearcherRef searcherRef, Query query, QueryContext additionalParametersOrNull, IndexSearcher additionsSearcher, Collection<Long> removed) {
try {
if (additionsSearcher != null && !removed.isEmpty()) {
letThroughAdditions(additionsSearcher, query, removed);
}
IndexSearcher searcher = additionsSearcher == null ? searcherRef.getSearcher() : new IndexSearcher(new MultiReader(searcherRef.getSearcher().getIndexReader(), additionsSearcher.getIndexReader()));
IndexHits<Document> result = null;
if (additionalParametersOrNull != null && additionalParametersOrNull.getTop() > 0) {
result = new TopDocsIterator(query, additionalParametersOrNull, searcher);
} else {
Sort sorting = additionalParametersOrNull != null ? additionalParametersOrNull.getSorting() : null;
boolean forceScore = additionalParametersOrNull == null || !additionalParametersOrNull.getTradeCorrectnessForSpeed();
Hits hits = new Hits(searcher, query, null, sorting, forceScore);
result = new HitsIterator(hits);
}
return result;
} catch (IOException e) {
throw new RuntimeException("Unable to query " + this + " with " + query, e);
}
}
use of org.apache.lucene.index.MultiReader in project stargate-core by tuplejump.
the class PerVNodeIndexContainer method search.
@Override
public <T> T search(SearcherCallback<T> searcherCallback) {
List<IndexReader> indexReaders = new ArrayList<>();
Map<Indexer, IndexSearcher> indexSearchers = new HashMap<>();
for (Map.Entry<Range<Token>, Indexer> entry : indexers.entrySet()) {
Range<Token> range = entry.getKey();
boolean intersects = intersects(searcherCallback.filterRange(), searcherCallback.isSingleToken(), searcherCallback.isFullRange(), range);
if (intersects) {
Indexer indexer = entry.getValue();
IndexSearcher searcher = indexer.acquire();
indexSearchers.put(indexer, searcher);
indexReaders.add(searcher.getIndexReader());
}
}
IndexReader[] indexReadersArr = new IndexReader[indexReaders.size()];
indexReaders.toArray(indexReadersArr);
MultiReader multiReader = null;
try {
multiReader = new MultiReader(indexReadersArr, false);
IndexSearcher allSearcher = new IndexSearcher(multiReader, executorService);
return searcherCallback.doWithSearcher(allSearcher);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (multiReader != null)
multiReader.close();
} catch (IOException e) {
logger.error("Could not close reader", e);
}
for (Map.Entry<Indexer, IndexSearcher> entry : indexSearchers.entrySet()) {
entry.getKey().release(entry.getValue());
}
}
}
use of org.apache.lucene.index.MultiReader in project alfresco-repository by Alfresco.
the class IndexInfo method createMainIndexReader.
private IndexReader createMainIndexReader() throws IOException {
IndexReader reader = null;
IndexReader oldReader = null;
for (String id : indexEntries.keySet()) {
IndexEntry entry = indexEntries.get(id);
if (entry.getStatus().isCommitted()) {
IndexReader subReader = getReferenceCountingIndexReader(id);
if (reader == null) {
reader = subReader;
} else {
boolean oldReaderIsSubReader = oldReader == null;
oldReader = reader;
reader = mainIndexReaders.get(id);
if (reader == null) {
if (entry.getType() == IndexType.INDEX) {
reader = new MultiReader(new IndexReader[] { oldReader, subReader }, false);
} else if (entry.getType() == IndexType.DELTA) {
try {
IndexReader filterReader = new FilterIndexReaderByStringId(id, oldReader, getDeletions(entry.getName(), INDEX_INFO_DELETIONS), getDeletions(entry.getName(), INDEX_INFO_CONTAINER_DELETIONS), entry.isDeletOnlyNodes());
reader = new MultiReader(new IndexReader[] { filterReader, subReader }, false);
// Cancel out the incRef on the filter reader
filterReader.decRef();
} catch (IOException ioe) {
s_logger.error("Failed building filter reader beneath " + entry.getName(), ioe);
throw ioe;
}
}
reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(id + "multi", reader, true, config);
mainIndexReaders.put(id, reader);
}
}
}
}
if (reader == null) {
reader = IndexReader.open(emptyIndex);
} else {
// Keep this reader open whilst it is referenced by mainIndexReaders / referenceCountingReadOnlyIndexReaders
reader.incRef();
}
reader = ReferenceCountingReadOnlyIndexReaderFactory.createReader(MAIN_READER, reader, false, config);
return reader;
}
Aggregations