Search in sources :

Example 1 with RelationshipValueIndexCursor

use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.

the class LuceneFulltextTestSupport method assertQueryFindsIds.

void assertQueryFindsIds(KernelTransaction ktx, boolean nodes, String indexName, String query, long... ids) throws Exception {
    IndexDescriptor index = ktx.schemaRead().indexGetForName(indexName);
    IndexReadSession indexSession = ktx.dataRead().indexReadSession(index);
    MutableLongSet set = LongSets.mutable.of(ids);
    if (nodes) {
        try (NodeValueIndexCursor cursor = ktx.cursors().allocateNodeValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            ktx.dataRead().nodeIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch(query));
            while (cursor.next()) {
                long nodeId = cursor.nodeReference();
                assertTrue(set.remove(nodeId), format("Result returned node id %d, expected one of %s", nodeId, Arrays.toString(ids)));
            }
        }
    } else {
        try (RelationshipValueIndexCursor cursor = ktx.cursors().allocateRelationshipValueIndexCursor(ktx.cursorContext(), ktx.memoryTracker())) {
            ktx.dataRead().relationshipIndexSeek(indexSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch(query));
            while (cursor.next()) {
                long relationshipId = cursor.relationshipReference();
                assertTrue(set.remove(relationshipId), format("Result returned relationship id %d, expected one of %s", relationshipId, Arrays.toString(ids)));
            }
        }
    }
    if (!set.isEmpty()) {
        fail("Number of results differ from expected. " + set.size() + " IDs were not found in the result: " + set);
    }
}
Also used : MutableLongSet(org.eclipse.collections.api.set.primitive.MutableLongSet) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) NodeValueIndexCursor(org.neo4j.internal.kernel.api.NodeValueIndexCursor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Example 2 with RelationshipValueIndexCursor

use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.

the class AbstractIndexQueryingTest method relationshipIndexSeekMustThrowOnWrongIndexEntityType.

@Test
void relationshipIndexSeekMustThrowOnWrongIndexEntityType() throws IndexNotFoundKernelException {
    IndexDescriptor index = schemaRead.indexGetForName("ftsNodes");
    IndexReadSession indexReadSession = read.indexReadSession(index);
    try (RelationshipValueIndexCursor cursor = cursors.allocateRelationshipValueIndexCursor(NULL, EmptyMemoryTracker.INSTANCE)) {
        assertThrows(IndexNotApplicableKernelException.class, () -> read.relationshipIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch("search")));
    }
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 3 with RelationshipValueIndexCursor

use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.

the class KernelReadTracerTxStateTest method shouldTraceRelationshipIndexCursor.

@Test
void shouldTraceRelationshipIndexCursor() throws KernelException, TimeoutException {
    // given
    int connection;
    int name;
    String indexName = "myIndex";
    IndexDescriptor index;
    try (KernelTransaction tx = beginTransaction()) {
        connection = tx.tokenWrite().relationshipTypeGetOrCreateForName("Connection");
        name = tx.tokenWrite().propertyKeyGetOrCreateForName("name");
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        SchemaDescriptor schema = SchemaDescriptor.fulltext(EntityType.RELATIONSHIP, array(connection), array(name));
        IndexPrototype prototype = IndexPrototype.forSchema(schema, DESCRIPTOR).withName(indexName).withIndexType(IndexType.FULLTEXT);
        index = tx.schemaWrite().indexCreate(prototype);
        tx.commit();
    }
    try (KernelTransaction tx = beginTransaction()) {
        Predicates.awaitEx(() -> tx.schemaRead().indexGetState(index) == ONLINE, 1, MINUTES);
        long n1 = tx.dataWrite().nodeCreate();
        long n2 = tx.dataWrite().nodeCreate();
        long r = tx.dataWrite().relationshipCreate(n1, connection, n2);
        tx.dataWrite().relationshipSetProperty(r, name, Values.stringValue("transformational"));
        tx.commit();
    }
    // when
    TestKernelReadTracer tracer = new TestKernelReadTracer();
    try (KernelTransaction tx = beginTransaction();
        RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(NULL, tx.memoryTracker())) {
        cursor.setTracer(tracer);
        IndexReadSession indexReadSession = tx.dataRead().indexReadSession(index);
        tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, unconstrained(), PropertyIndexQuery.fulltextSearch("transformational"));
        assertTrue(cursor.next());
        tracer.assertEvents(OnIndexSeek(), OnRelationship(cursor.relationshipReference()));
        assertFalse(cursor.next());
        tracer.assertEvents();
    }
}
Also used : KernelTransaction(org.neo4j.kernel.api.KernelTransaction) SchemaDescriptor(org.neo4j.internal.schema.SchemaDescriptor) RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexPrototype(org.neo4j.internal.schema.IndexPrototype) IndexDescriptor(org.neo4j.internal.schema.IndexDescriptor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession) Test(org.junit.jupiter.api.Test)

Example 4 with RelationshipValueIndexCursor

use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.

the class FulltextProcedures method queryFulltextForRelationships.

@SystemProcedure
@Description("Query the given full-text index. Returns the matching relationships, 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.queryRelationships", mode = READ)
public Stream<RelationshipOutput> queryFulltextForRelationships(@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 != RELATIONSHIP) {
        throw new IllegalArgumentException("The '" + name + "' index (" + indexReference + ") is an index on " + entityType + ", so it cannot be queried for relationships.");
    }
    RelationshipValueIndexCursor cursor = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker());
    IndexReadSession indexReadSession = tx.dataRead().indexReadSession(indexReference);
    IndexQueryConstraints constraints = queryConstraints(options);
    tx.dataRead().relationshipIndexSeek(indexReadSession, cursor, constraints, PropertyIndexQuery.fulltextSearch(query));
    Spliterator<RelationshipOutput> spliterator = new SpliteratorAdaptor<>() {

        @Override
        public boolean tryAdvance(Consumer<? super RelationshipOutput> action) {
            while (cursor.next()) {
                long relationshipReference = cursor.relationshipReference();
                float score = cursor.score();
                RelationshipOutput relationshipOutput = RelationshipOutput.forExistingEntityOrNull(transaction, relationshipReference, score);
                if (relationshipOutput != null) {
                    action.accept(relationshipOutput);
                    return true;
                }
            }
            cursor.close();
            return false;
        }
    };
    return StreamSupport.stream(spliterator, false).onClose(cursor::close);
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) 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 5 with RelationshipValueIndexCursor

use of org.neo4j.internal.kernel.api.RelationshipValueIndexCursor in project neo4j by neo4j.

the class RelationshipIndexTransactionStateTest method assertEntityAndValueForScan.

@Override
void assertEntityAndValueForScan(Set<Pair<Long, Value>> expected, KernelTransaction tx, IndexDescriptor index, boolean needsValues, Object anotherValueFoundByQuery) throws Exception {
    IndexReadSession indexSession = tx.dataRead().indexReadSession(index);
    try (RelationshipValueIndexCursor relationships = tx.cursors().allocateRelationshipValueIndexCursor(tx.cursorContext(), tx.memoryTracker())) {
        tx.dataRead().relationshipIndexScan(indexSession, relationships, unordered(needsValues));
        assertEntityAndValue(expected, tx, needsValues, anotherValueFoundByQuery, new RelationshipCursorAdapter(relationships));
    }
}
Also used : RelationshipValueIndexCursor(org.neo4j.internal.kernel.api.RelationshipValueIndexCursor) IndexReadSession(org.neo4j.internal.kernel.api.IndexReadSession)

Aggregations

RelationshipValueIndexCursor (org.neo4j.internal.kernel.api.RelationshipValueIndexCursor)13 IndexReadSession (org.neo4j.internal.kernel.api.IndexReadSession)12 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)9 Test (org.junit.jupiter.api.Test)5 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)5 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 Transaction (org.neo4j.graphdb.Transaction)2 IndexPrototype (org.neo4j.internal.schema.IndexPrototype)2 SchemaDescriptor (org.neo4j.internal.schema.SchemaDescriptor)2 Consumer (java.util.function.Consumer)1 MutableLongSet (org.eclipse.collections.api.set.primitive.MutableLongSet)1 EntityType (org.neo4j.common.EntityType)1 KernelException (org.neo4j.exceptions.KernelException)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Node (org.neo4j.graphdb.Node)1 Relationship (org.neo4j.graphdb.Relationship)1 RelationshipType (org.neo4j.graphdb.RelationshipType)1 IndexQueryConstraints (org.neo4j.internal.kernel.api.IndexQueryConstraints)1 NodeValueIndexCursor (org.neo4j.internal.kernel.api.NodeValueIndexCursor)1 Read (org.neo4j.internal.kernel.api.Read)1