简体   繁体   中英

Most efficient way to find connections between the nodes in Cypher / Neo4J?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM