Search in sources :

Example 91 with IndexReadSession

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);
}
Also used : NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) IndexQueryConstraints(org.neo4j.internal.kernel.api.IndexQueryConstraints) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) EntityType(org.neo4j.common.EntityType) Consumer(java.util.function.Consumer) Description(org.neo4j.procedure.Description) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) SystemProcedure(org.neo4j.kernel.api.procedure.SystemProcedure) Procedure(org.neo4j.procedure.Procedure)

Example 92 with IndexReadSession

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)]));
}
Also used : IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 93 with IndexReadSession

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;
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) KernelTransaction(org.neo4j.kernel.api.KernelTransaction) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Aggregations

IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)93 Test (org.junit.jupiter.api.Test)62 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)47 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)22 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)20 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)17 LongHashSet (org.eclipse.collections.impl.set.mutable.primitive.LongHashSet)15 ArrayList (java.util.ArrayList)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 EnumSource (org.junit.jupiter.params.provider.EnumSource)12 RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)12 IndexValueCapability (org.neo4j.internal.schema.IndexValueCapability)12 Pair (org.neo4j.internal.helpers.collection.Pair)11 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)10 Transaction (org.neo4j.graphdb.Transaction)9 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)9 IndexOrderCapability (org.neo4j.internal.schema.IndexOrderCapability)7 Read (org.neo4j.internal.kernel.api.Read)6 TokenRead (org.neo4j.internal.kernel.api.TokenRead)6 PropertyIndexQuery (org.neo4j.internal.kernel.api.PropertyIndexQuery)5