Question

I need to find connections between all the nodes (with a "Hashtag" label) connected to the main user node.

So far, I came up with a solution like this, but it seems to me a bit inefficient, because I traverse the graph twice to first find c1 and then c2.

Anybody has better ideas?

MATCH (u:User{uid:"777"}), (c1:Hashtag), (c2:Hashtag), 
c1-[:BY]->u, c2-[:BY]->u, c1-[rel:TO]->c2 RETURN rel,c1,c2;

(I'm working with Neo4J / Cypher 2.0)

Était-ce utile?

La solution

Try this, play around with it and let me know the output.

MATCH (u:User {uid:"777"})
WITH u
MATCH u<-[:BY]-(c1:Hashtag)-[rel:TO]-(c2:Hashtag)--(u)
RETURN rel, c1, c2

Basically, the idea here is as follow:

  1. Match the User node first
  2. Use it to match all 'Hashtag' nodes
  3. Use it to match to all 'Hashtag' nodes connected to the previous 'Hashtag nodes
  4. Return the 'rel', which is all the relationships from 'Hashtag' nodes to 'Hashtag' nodes which are connected to the user 777
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top