Search in sources :

Example 46 with MultiReader

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);
    }
}
Also used : SuperIndexSearcher(org.opensolaris.opengrok.configuration.SuperIndexSearcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Project(org.opensolaris.opengrok.configuration.Project) TreeSet(java.util.TreeSet) MultiReader(org.apache.lucene.index.MultiReader) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 47 with MultiReader

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);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexHits(org.neo4j.graphdb.index.IndexHits) MultiReader(org.apache.lucene.index.MultiReader) Sort(org.apache.lucene.search.Sort) IOException(java.io.IOException) Document(org.apache.lucene.document.Document)

Example 48 with MultiReader

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());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MultiReader(org.apache.lucene.index.MultiReader) Token(org.apache.cassandra.dht.Token) IOException(java.io.IOException) Range(org.apache.cassandra.dht.Range) IOException(java.io.IOException) Indexer(com.tuplejump.stargate.lucene.Indexer) BasicIndexer(com.tuplejump.stargate.lucene.BasicIndexer) IndexReader(org.apache.lucene.index.IndexReader)

Example 49 with MultiReader

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;
}
Also used : FilterIndexReaderByStringId(org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId) MultiReader(org.apache.lucene.index.MultiReader) IndexReader(org.apache.lucene.index.IndexReader) IOException(java.io.IOException)

Aggregations

MultiReader (org.apache.lucene.index.MultiReader)49 IndexReader (org.apache.lucene.index.IndexReader)22 Term (org.apache.lucene.index.Term)20 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 IOException (java.io.IOException)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 Document (org.apache.lucene.document.Document)6 Query (org.apache.lucene.search.Query)6 TermQuery (org.apache.lucene.search.TermQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 HashSet (java.util.HashSet)3 List (java.util.List)3 TreeSet (java.util.TreeSet)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)3 Sort (org.apache.lucene.search.Sort)3 FileNotFoundException (java.io.FileNotFoundException)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 FilterIndexReaderByStringId (org.alfresco.repo.search.impl.lucene.FilterIndexReaderByStringId)2