Nikita Ignatov
Nikita Ignatov

Reputation: 7163

Cypher temp relationship

I'm trying to combine / merge a path into a new relationship. The problem is that I'm not interested in storing it but rather return it as a result of a cypher query.

Lets say I have something like this:

(a)-[:CALLS_METHOD]->(b)-[:RETURNS_TYPE]->(c)

How can I create a temporary relationship like this one:

(a)-[:DEPENDS_ON]->(c)

Only for a result of that particular query, so that I don't have to store it. Because I'm really only interested in the dependency from a to c and not the details about of that dependency.

Upvotes: 1

Views: 1606

Answers (2)

vsvin
vsvin

Reputation: 11

There is a function in apoc library to do this: apoc.create.vRelationship.

Create example data set:

CREATE (from:Account), (to:Account)
WITH from, to
CREATE (from)-[:SENT]->(:Payment {amount: 250})-[:RECEIVED]->(to)
CREATE (from)-[:SENT]->(:Payment {amount: 750})-[:RECEIVED]->(to)

Perform query with apoc.create.vRelationship function:

MATCH (from:Account)-[:SENT]->(p:Payment)-[:RECEIVED]->(to:Account)
RETURN from, to, apoc.create.vRelationship(from,'PAID',{amount:sum(p.amount)},to) as rel;

SEE RESULT

Upvotes: 1

FrobberOfBits
FrobberOfBits

Reputation: 18002

You can't return a relationship from the database that doesn't exist. The purpose of the queries is to return stuff that does exist.

Perhaps what you're interested in is inferred pairs, rather than a relationship. Something like:

MATCH (a)-[r:CALLS_METHOD|RETURNS_TYPE*]->(b)
RETURN a, "depends on", b

Your other alternative is to materialize/save the relationship, and then query for it:

MATCH (a)-[r:CALLS_METHOD|RETURNS_TYPE*]->(b)
CREATE a-[newRel:DEPENDS_ON]->b
RETURN newRel;

But this has the side-effect of creating it.

Upvotes: 1

Related Questions