2
votes

Having a graph of people who like rated movies, I would like to extract for each pair of people their highest rated movie. I'm using the following query which requires sorting movies on their rate for each pair of people.

MATCH (p1:People) -[:LIKES]-> (m:Movie) <-[:LIKES]- (p2:People) WHERE id(p1) < id(p2)
WITH p1, p2, m ORDER BY m.Rating desc
RETURN p1, p2, head(collect(m) as best

I can put movie rating (1/rating or maxRating-rating) into :LIKES relationships, which hence let me identify which movie is in the top rating of both people.

MATCH (p1:People), (p2:People) call apoc.algo.dijkstra(p1, p2, 'LIKES', 'rating') YIELD path as path, weight as weight return path, weight

Is there a way to use a Dijkstra-like algorithm which would find the allOptimumPath through highest scored nodes to improve the performance of my first query and return paths rather than their starting, middle and ending nodes ? Many thanks in advance.

1

1 Answers

0
votes

Here is an alternate solution which preserves the path rather than reporting extracted nodes.

MATCH path=(p1:People) -[:LIKES]-> (m:Movie) <-[:LIKES]- (p2:People) 
WHERE id(p1) < id(p2)
WITH head(nodes(p)) as p1, last(nodes(p)) as p2, path 
ORDER BY m.Rating desc
WITH p1, p2, head(collect(p)) as optPath
RETURN optPath