ddanurwenda
ddanurwenda

Reputation: 153

Get all node-relation originating from a node in Neo4J

Let's say we have this cypher

match (n:Person{pid:322})-[k*1..2]->(s) return k

k would be a Collection of all relation originating from a specific node n in at most 2 hops.

How can I get all Node-Relation-Node where Relation is in k? It's something like match (a)-[b]->(c) foreach b in k return a,b,c but I know this query is invalid since foreach keyword in Neo4J can't be used in this case.

Edit: I think I should add some illustration to make things clearer. Here I use an example from Neo4J documentation: Neo4J example

When I start from Charlie Sheen and using 1..2 range, the query must return (assuming the query ends with return a,b,c)

a | b | c
Charlie Sheen | ACTED_IN | Wall Street
Charlie Sheen | FATHER | Martin Sheen
Martin Sheen | ACTED_IN | The American President
Martin Sheen | ACTED_IN | Wall Street

Upvotes: 1

Views: 258

Answers (3)

cybersam
cybersam

Reputation: 67044

This query should produce the a, b, and c values your edited question asks for (assuming you always want the name property values):

MATCH (n:Person{pid:322})-[k*1..2]->(s)
WITH LAST(k) AS lk
RETURN STARTNODE(lk).name AS a, TYPE(lk) AS b, ENDNODE(lk).name AS c;

Upvotes: 1

Luanne
Luanne

Reputation: 19373

A path will give you the sequence of nodes and relationsjips from n to s via k

 MATCH p=(n:Person{pid:322})-[k*1..2]->(s) return p

You can also extract only the nodes and/or only the relationships from the path using nodes(p) or rels(p)

Upvotes: 0

Paul
Paul

Reputation: 36349

I'm not sure why that wouldn't just be:

match (n:Person{pid:322})-[k*1..2]->(s) return n,k,s

Unless I'm misunderstanding your question.

Upvotes: 0

Related Questions