0
votes

I have 2 groups of nodes, persons and movies, and I want to find the closest movie to each person. I found the shortest path to the closest movie this way:

match path = shortestPath( (p: {id: 1)-[*]-(:MOVIE))
with path order by length(path)
return collect(path)[0]

Now, and I want to find the closest movie to each person (for each person, the shortest path of the shortest paths between the person and all movies) but, I didn't find a way to take the shortest path for each person separated.

I wish to have something like that:

match (p:PERSON)
with p
match path = shortestPath( (person)-[*]-(:MOVIE) )
with person.id as p_id, path ORDER BY length(path)
return collect(path)[0]

but, the query order by length the whole response. I didn't find a way to order the paths collection per person_id individually

1

1 Answers

0
votes

You just need to add the person id in the return statement, so the collect aggregates by person id

match (p:PERSON)
with p
match path = shortestPath( (person)-[*]-(:MOVIE) )
with person.id as p_id, path ORDER BY length(path)
return p_id, collect(path)[0]