cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher query, breaking down result

i need help with minor adjustment to a code,
i am using path expand to returns paths; the nodes and the relationships connecting them. however, it return the nodes in one column and the relationships in another. i also need to make this process more efficient.

match (n:Movie), (m:Person)
CALL apoc.path.expandConfig(n, {
relationshipFilter:"",
minLevel: 1,
maxLevel: 4,
uniqueness: "NODE_PATH"
})
YIELD path
WITH [x in nodes(path) | x.name] AS nodes,
[a in relationships(path) |type(a)] as relationships
return nodes, relationships
RETURNS something like this.
[null,"Joel Silver",null,"Andy Wachowski"] │["PRODUCED","PRODUCED","WROTE"]

3 REPLIES 3

You are not using the Person match (m) in this case. The Cartesian product of n and m, will cause each movie node to be identically processed multiple time equal to the number of Person nodes in your database. Removing it from the match should greatly improve performance and reduce the redundant data returned.

What is the format of the output you are looking for?

something like x- causes -y-causes-z-causes-w.

now i am getting 

{x|y|z|w} | {causes|causes|causes}

notice how they are in separate 

Ok, this may be a little out there. I tested it on a graph I have and it worked. For each path, it will format the path as follows, where the name is output as a name or title, depending on if the node is a person or movie, respectively. Of course, you can change the query to output the specific property from each node that you want. 

movie_title <-> RELATIONSHIP_TYPE <-> node(name or title) <-> RELATIONSHIP_TYPE <-> ...

 

match (n:Movie)
CALL apoc.path.expandConfig(n, {
relationshipFilter:"",
minLevel: 1,
maxLevel: 4,
uniqueness: "NODE_PATH"
})
YIELD path
with relationships(path) as relationships
with startNode(head(relationships)) as headNode, tail(relationships) as tail
return reduce(s=headNode.title, r in tail | s + " <-> " + type(r) + " <-> " + coalesce(endNode(r).name, endNode(r).title)) as path

 

 

Let me know how it goes.