Search in sources :

Example 1 with Neo4jIndexSearcher

use of org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher in project neo4j by neo4j.

the class FulltextIndexReader method searchLucene.

private ValuesIterator searchLucene(Query query, IndexQueryConstraints constraints, QueryContext context, CursorContext cursorContext, MemoryTracker memoryTracker) {
    try {
        // We are replicating the behaviour of IndexSearcher.search(Query, Collector), which starts out by re-writing the query,
        // then creates a weight based on the query and index reader context, and then we finally search the leaf contexts with
        // the weight we created.
        // The query rewrite does not really depend on any data in the index searcher (we don't produce such queries), so it's fine
        // that we only rewrite the query once with the first searcher in our partition list.
        query = searchers.get(0).getIndexSearcher().rewrite(query);
        boolean includeTransactionState = context.getTransactionStateOrNull() != null && !isEventuallyConsistent(index);
        // If we have transaction state, then we need to make our result collector filter out all results touched by the transaction state.
        // The reason we filter them out entirely, is that we will query the transaction state separately.
        LongPredicate filter = includeTransactionState ? transactionState.isModifiedInTransactionPredicate() : ALWAYS_FALSE;
        List<PreparedSearch> searches = new ArrayList<>(searchers.size() + 1);
        for (SearcherReference searcher : searchers) {
            Neo4jIndexSearcher indexSearcher = searcher.getIndexSearcher();
            searches.add(new PreparedSearch(indexSearcher, filter));
        }
        if (includeTransactionState) {
            SearcherReference reference = transactionState.maybeUpdate(context, cursorContext, memoryTracker);
            searches.add(new PreparedSearch(reference.getIndexSearcher(), ALWAYS_FALSE));
        }
        // The StatsCollector aggregates index statistics across all our partitions.
        // Weights created based on these statistics will produce scores that are comparable across partitions.
        StatsCollector statsCollector = new StatsCollector(searches);
        List<ValuesIterator> results = new ArrayList<>(searches.size());
        for (PreparedSearch search : searches) {
            // Weights are bonded with the top IndexReaderContext of the index searcher that they are created for.
            // That's why we have to create a new StatsCachingIndexSearcher, and a new weight, for every index partition.
            // However, the important thing is that we re-use the statsCollector.
            StatsCachingIndexSearcher statsCachingIndexSearcher = new StatsCachingIndexSearcher(search, statsCollector);
            Weight weight = statsCachingIndexSearcher.createWeight(query, ScoreMode.COMPLETE, 1);
            results.add(search.search(weight, constraints));
        }
        return ScoreEntityIterator.mergeIterators(results);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) LongPredicate(java.util.function.LongPredicate) Weight(org.apache.lucene.search.Weight) SearcherReference(org.neo4j.kernel.api.impl.index.SearcherReference) Neo4jIndexSearcher(org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher) ValuesIterator(org.neo4j.kernel.api.impl.index.collector.ValuesIterator)

Example 2 with Neo4jIndexSearcher

use of org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher in project neo4j by neo4j.

the class StatsCollector method computeTermStatistics.

private Optional<TermStatistics> computeTermStatistics(Term term) {
    TermStatistics result;
    List<TermStatistics> statistics = new ArrayList<>(searches.size());
    for (PreparedSearch search : searches) {
        Neo4jIndexSearcher searcher = search.searcher();
        try {
            TermStates context = TermStates.build(searcher.getTopReaderContext(), term, true);
            TermStatistics statistic = searcher.termStatistics(term, context);
            if (statistic != null) {
                statistics.add(statistic);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    long docFreq = 0;
    long totalTermFreq = 0;
    for (TermStatistics statistic : statistics) {
        docFreq += statistic.docFreq();
        totalTermFreq += statistic.totalTermFreq();
    }
    if (docFreq == 0) {
        return Optional.empty();
    }
    BytesRef bytesTerm = statistics.get(0).term();
    result = new TermStatistics(bytesTerm, docFreq, totalTermFreq);
    return Optional.of(result);
}
Also used : TermStates(org.apache.lucene.index.TermStates) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Neo4jIndexSearcher(org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher) TermStatistics(org.apache.lucene.search.TermStatistics) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with Neo4jIndexSearcher

use of org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher in project neo4j by neo4j.

the class LuceneAllDocumentsReaderTest method createPartitionSearcher.

private static PartitionSearcher createPartitionSearcher(int maxDoc, int partition, int maxSize) throws IOException {
    PartitionSearcher partitionSearcher = mock(PartitionSearcher.class);
    Neo4jIndexSearcher indexSearcher = mock(Neo4jIndexSearcher.class);
    IndexReader indexReader = mock(IndexReader.class);
    when(partitionSearcher.getIndexSearcher()).thenReturn(indexSearcher);
    when(indexSearcher.getIndexReader()).thenReturn(indexReader);
    when(indexReader.maxDoc()).thenReturn(maxDoc);
    when(indexSearcher.doc(0)).thenReturn(createDocument(uniqueDocValue(1, partition, maxSize)));
    when(indexSearcher.doc(1)).thenReturn(createDocument(uniqueDocValue(2, partition, maxSize)));
    when(indexSearcher.doc(2)).thenReturn(createDocument(uniqueDocValue(3, partition, maxSize)));
    return partitionSearcher;
}
Also used : PartitionSearcher(org.neo4j.kernel.api.impl.index.partition.PartitionSearcher) IndexReader(org.apache.lucene.index.IndexReader) Neo4jIndexSearcher(org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher)

Example 4 with Neo4jIndexSearcher

use of org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher in project neo4j by neo4j.

the class TransactionStateLuceneIndexWriter method getNearRealTimeSearcher.

SearcherReference getNearRealTimeSearcher() throws IOException {
    DirectoryReader directoryReader = DirectoryReader.open(writer);
    Neo4jIndexSearcher searcher = new Neo4jIndexSearcher(directoryReader);
    return new DirectSearcherReference(searcher, directoryReader);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) Neo4jIndexSearcher(org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher)

Aggregations

Neo4jIndexSearcher (org.neo4j.kernel.api.impl.index.partition.Neo4jIndexSearcher)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 UncheckedIOException (java.io.UncheckedIOException)1 LongPredicate (java.util.function.LongPredicate)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexReader (org.apache.lucene.index.IndexReader)1 TermStates (org.apache.lucene.index.TermStates)1 TermStatistics (org.apache.lucene.search.TermStatistics)1 Weight (org.apache.lucene.search.Weight)1 BytesRef (org.apache.lucene.util.BytesRef)1 SearcherReference (org.neo4j.kernel.api.impl.index.SearcherReference)1 ValuesIterator (org.neo4j.kernel.api.impl.index.collector.ValuesIterator)1 PartitionSearcher (org.neo4j.kernel.api.impl.index.partition.PartitionSearcher)1