Richard
Richard

Reputation: 65510

Cypher: return both nodes and edges from a query?

I have a beginner Cypher question. I have a query like this:

MATCH (rebecca:Person {name:"Rebecca"})-[1..2]->(companies:Company)
RETURN companies

This returns all the companies within two hops of Rebecca, but it only returns the final company nodes. How do I see the edges and the intermediate nodes as well?

Upvotes: 0

Views: 760

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20185

You can return only entities that are aliased.

MATCH (rebecca:Person {name:"Rebecca"})-[1..2]->(companies:Company)
RETURN companies

Add a r alias to your relationships :

MATCH (rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies, r

For returning intermediate nodes, you can make it a path :

MATCH p=(rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies, relationships(p), nodes(p)

You can also return the company along with a map of (relationship, startnode, endnode) :

MATCH (rebecca:Person {name:"Rebecca"})-[r*1..2]->(companies:Company)
RETURN companies,
       extract(x IN r | {rel: x, start: startNode(x), end: endNode(x)})

Upvotes: 4

Related Questions