use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class FulltextProcedures method queryFulltextForNodes.
@SystemProcedure
@Description("Query the given full-text index. Returns the matching nodes, and their Lucene query score, ordered by score. " + "Valid keys for the options map are: 'skip' to skip the top N results; 'limit' to limit the number of results returned.")
@Procedure(name = "db.index.fulltext.queryNodes", mode = READ)
public Stream<NodeOutput> queryFulltextForNodes(@Name("indexName") String name, @Name("queryString") String query, @Name(value = "options", defaultValue = "{}") Map<String, Object> options) throws Exception {
if (callContext.isSystemDatabase()) {
return Stream.empty();
}
IndexDescriptor indexReference = getValidIndex(name);
awaitOnline(indexReference);
EntityType entityType = indexReference.schema().entityType();
if (entityType != NODE) {
throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for nodes.");
}
NodeValueIndexCursor cursor = tx.cursors().allocateNodeValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
IndexReadSession indexSession = tx.dataRead().indexReadSession(indexReference);
IndexQueryConstraints constraints = queryConstraints(options);
tx.dataRead().nodeIndexSeek(indexSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
Spliterator<NodeOutput> spliterator = new SpliteratorAdaptor<>() {
@Override
public boolean tryAdvance(Consumer<? super NodeOutput> action) {
while (cursor.next()) {
long nodeReference = cursor.nodeReference();
float score = cursor.score();
NodeOutput nodeOutput = NodeOutput.forExistingEntityOrNull(transaction, nodeReference, score);
if (nodeOutput != null) {
action.accept(nodeOutput);
return true;
}
}
cursor.close();
return false;
}
};
Stream<NodeOutput> stream = StreamSupport.stream(spliterator, false);
return stream.onClose(cursor::close);
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class KernelAPIParallelNodeValueIndexScanStressIT method shouldDoParallelIndexScans.
@Test
void shouldDoParallelIndexScans() throws Throwable {
// Given
try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
createLabeledNodes(tx, N_NODES, "LABEL1", "prop");
createLabeledNodes(tx, N_NODES, "LABEL2", "prop");
createLabeledNodes(tx, N_NODES, "LABEL3", "prop");
tx.commit();
}
IndexDescriptor index1;
IndexDescriptor index2;
IndexDescriptor index3;
try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
index1 = unwrap(tx.schema().indexFor(Label.label("LABEL1")).on("prop").create());
index2 = unwrap(tx.schema().indexFor(Label.label("LABEL2")).on("prop").create());
index3 = unwrap(tx.schema().indexFor(Label.label("LABEL3")).on("prop").create());
tx.commit();
}
try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {
tx.schema().awaitIndexesOnline(10, MINUTES);
tx.commit();
}
// when & then
IndexReadSession[] indexes = new IndexReadSession[3];
try (KernelTransaction tx = kernel.beginTransaction(EXPLICIT, LoginContext.AUTH_DISABLED)) {
indexes[0] = indexReadSession(tx, index1);
indexes[1] = indexReadSession(tx, index2);
indexes[2] = indexReadSession(tx, index3);
tx.commit();
}
KernelAPIParallelStress.parallelStressInTx(kernel, N_THREADS, tx -> tx.cursors().allocateNodeValueIndexCursor(tx.cursorContext(), tx.memoryTracker()), (read, cursor) -> indexSeek(read, cursor, indexes[random.nextInt(indexes.length)]));
}
use of org.neo4j.internal.kernel.api.IndexReadSession in project neo4j by neo4j.
the class RelationshipTypeIndexIT method countRelationshipsInFulltextIndex.
private int countRelationshipsInFulltextIndex(String indexName) throws KernelException {
int relationshipsInIndex;
try (Transaction transaction = db.beginTx()) {
KernelTransaction ktx = ((InternalTransaction) transaction).kernelTransaction();
IndexDescriptor index = ktx.schemaRead().indexGetForName(indexName);
IndexReadSession indexReadSession = ktx.dataRead().indexReadSession(index);
relationshipsInIndex = 0;
try (RelationshipValueIndexCursor cursor = ktx.cursors().allocateRelationshipValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
ktx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), fulltextSearch("*"));
while (cursor.next()) {
relationshipsInIndex++;
}
}
}
return relationshipsInIndex;
}
Aggregations