0
votes

Is it possible to return in a single cypher query distinct nodes and edges of a specific path. For instance, using the movies graph the query below return separately movies and actors, I'd like to return all nodes together. using path p = (...) and nodes(p) actually returns pairs of nodes regardless the use of distinct.

match (m:Movie {name: "Rain"}) -- (p:Person) return {nodes: collect(distinct {name: m.title}), actors: collect(distinct {name: a.name}), links: collect({source: m.title, target: a.name})}

Thanks in advance for any help, Pierre

1
By the way, I didn't manage to use union to group actors and movies either.Pierre
You can't return a JSON object from Cypher like that. Consider backing up a few steps and thinking through how neo4j connects to your javascript app. Broadly, you'll need to get a result set from Cypher and then have separate tooling (not neo4j) convert that into a JSON object suitable for d3js.FrobberOfBits

1 Answers

2
votes

Got some help internally, so I'm sharing the answer. With neo'j 2.1.5, one can use unwind. The query following query returns in once the list of distinct nodes and distinct edges in the path - at least it worked with my examples:

match path = (p:Person {Name: 'Rain'})-[]-(m:Movie) unwind nodes(path) as p unwind rels(path) as r
return {nodes: collect(distinct p), links: collect(DISTINCT {source: id(startNode(r)), target: id(endNode(r))})}