Frage

In extension to this Multiple relationships in Match Cypher

   MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag),
     (t)<-[h2:HAS_TAG]-(sm:Movie),
     (m)-[h:HAS_TAG]->(t0:Tag),
    (sm)-[H:HAS_TAG]->(t1:Tag)
   WHERE m <> sm
   WITH DISTINCT sm, h
   RETURN sm, collect(h.weight)

I am finding trouble in getting the distinct values of h1, h2, H, h all at the same time. I want to calculate the similarity index between any two movies which will be dependent on h1, h2, h, H (h1.h2/|h||H|)

 MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag),
  (t)<-[h2:HAS_TAG]-(sm:Movie),
  (m)-[h:HAS_TAG]->(t0:Tag),
  (sm)-[H:HAS_TAG]->(t1:Tag) 
 WHERE m <> sm 
 WITH sum(h1.weight*h2.weight) as num, sm, H, m, h
 WITH DISTINCT m, sqrt(sum(h.weight^2)) as den1, sm, H, num
 WITH DISTINCT sm, sqrt(sum(H.weight^2)) as den2, den1, num 
 RETURN num/(den1*den2)

This is all messed up..But I am unable to figure out the right way to solve this. Please help.

War es hilfreich?

Lösung

This works and gives the correct answer...

  MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag)<-[h2:HAS_TAG]-(sm)
  WHERE m <> sm
  WITH SUM(h1.weight * h2.weight) AS num,
     SQRT(REDUCE(xDot = 0.0, a IN COLLECT(h1)| xDot + a.weight^2)) AS xLength,
     SQRT(REDUCE(yDot = 0.0, b IN COLLECT(h2)| yDot + b.weight^2)) AS yLength, m, sm
  RETURN num, xLength, yLength

Andere Tipps

Take a look at this example I generated using the Neo4j Console:

http://console.neo4j.org/?id=aq6cb3

The query should be:

MATCH (m:Movie { title: "The Matrix" })-[h1:HAS_TAG]->(t:Tag),
    (t)<-[h2:HAS_TAG]-(sm:Movie),
    (m)-[h:HAS_TAG]->(t0:Tag),
    (sm)-[H:HAS_TAG]->(t1:Tag)
WHERE m <> sm
WITH m, sm, 
    collect(DISTINCT h) AS h, 
    collect(DISTINCT H) AS H, 
    sum(h1.weight*h2.weight) AS num
WITH m, sm, num,
    sqrt(reduce(s = 0.0, x IN h | s +(x.weight^2))) AS den1, 
    sqrt(reduce(s = 0.0, x IN H | s +(x.weight^2))) AS den2
RETURN m.title, sm.title, (num/(den1*den2)) AS similarity

Which results in the following:

+---------------------------------------------------------------+
| m.title      | sm.title                  | similarity         |
+---------------------------------------------------------------+
| "The Matrix" | "The Matrix: Revolutions" | 3.859767091086958  |
| "The Matrix" | "The Matrix: Reloaded"    | 1.4380667053087486 |
+---------------------------------------------------------------+

I used the reduce function to aggregate the relationship values from a distinct collection and perform the similarity index calculation.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top