Search code examples
neo4jpy2neo

iterating over subpaths vs path elements


The question applies to any neo4j path, but for clarity, let's borrow the bus route data from Answer 2 to this post

Neo4J | Cypher Aggregation on list of relationships

We can run this query

MATCH p=(o:BusStop)-[*]->(d:BusStop) RETURN p

and get a bunch of Nodes and Relationships, as we would expect.

p [ { "id": 1 } , { "fare": 10, "day": "Sat" }... and so on

My problem with the above is that when processing the results in Python, I'm forced to iterate over individual Nodes and Relationships.

But what if I wanted to iterate over complete (startnode)-[*]-(endnode) sub-paths?

In the browser, individual subpaths are separated by horizontal lines. How do I get an equivalent to those separators in py2neo?

Thanks.


Solution

  • Neo4j results are a stream of records. For the given query, each record in the stream will be a separate path, so you would use the methods of the returned results cursor to iterate over each path.

    If you wanted a single result that was a collection of paths, you could return that instead:

    RETURN collect(p) as results