Reputation: 123
I would like to know how to display the names of every node and relationship generated from a path p in Neo4j Cypher.
I have this query:
MATCH (m { name: 'porsche' }),(n { name: 'vehicle' }), p =(m)-[r*]->(n)
return collect(p);
══════════════════════════════╕
│"collect(p)" │
╞══════════════════════════════╡
│[[{"name":"porsche"},{"name":"│
│is a"},{"name":"car","type":"l│
│abel"},{"name":"car","type":"l│
│abel"},{"name":"is a subtype o│
│f"},{"name":"vehicle","type":"│
│label"}],[{"name":"porsche"},{│
│"name":"is a"},{"name":"car","│
│type":"label"},{"name":"car","│
│type":"label"},{},{"name":"veh│
│icle","type":"label"}]] │
└──────────────────────────────┘
But I want it do display each node's name and then each Relationship's name in sequence like this:
'porsche' 'is a' 'car'
'car' 'is a subtype of' vehicle
Upvotes: 1
Views: 1333
Reputation: 67044
The output shown in your question indicates that your data is not well formed. For example, not all relationships actually have a name
. This answer assumes well-formed data, but can be tweaked to handle missing name
properties if needed.
Since your question was not clear about what you wanted, here are some options.
This query will return a name triplet (in a list) for each relationship of each path:
MATCH ({ name: 'porsche' })-[rels*]->({ name: 'vehicle' })
WITH [r IN rels | [STARTNODE(r).name, r.name, ENDNODE(r).name]] AS steps
UNWIND steps AS step
RETURN step;
Sample output (for one path):
["porsche", "is a", "car"]
["car", "is a subtype of", "vehicle"]
If you want to keep apart the results for each path, you can replace UNWIND path_names AS names RETURN names;
with RETURN path_names;
. That would produce somehting like this for each path:
[["porsche", "is a", "car"], ["car", "is a subtype of", "vehicle"]]
If you want to get the distinct "steps" from all paths, you can do this:
MATCH ({ name: 'porsche' })-[rels*]->({ name: 'vehicle' })
WITH [r IN rels | [STARTNODE(r).name, r.name, ENDNODE(r).name]] AS steps
UNWIND steps AS step
RETURN DISTINCT step;
The result will look like the result for Option 1, except each "step" will be distinct.
Upvotes: 1