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