Ernestina Juan
Ernestina Juan

Reputation: 967

Cypher - Only show node name, not full node in path variable

In Cypher I have the following query:

MATCH p=(n1 {name: "Node1"})-[r*..6]-(n2 {name: "Node2"})
RETURN p, reduce(cost = 0, x in r | cost + x.cost) AS cost

It is working as expected. However, it prints the full n1 node, then the full r relationship (with all its attributes), and then full n2.

What I want instead is to just show the value of the name attribute of n1, the type attribute of r and again the name attribute of n2.

How could this be possible?

Thank you.

Upvotes: 0

Views: 233

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

The tricky part of your request is the type attribute of r, as r is a collection of relationships of the path, not a single relationship. We can use EXTRACT to produce a list of relationship types for all relationships in your path. See if this will work for you:

MATCH (n1 {name: "Node1"})-[r*..6]-(n2 {name: "Node2"})
RETURN n1.name, EXTRACT(rel in r | TYPE(rel)) as types, n2.name, reduce(cost = 0, x in r | cost + x.cost) AS cost

You also seem to be calculating a cost for the path. Have you looked at the shortestPath() function?

Upvotes: 1

Related Questions