【问题标题】:how to create tree structure in neo4j with multiple relationships?如何在 Neo4j 中创建具有多个关系的树结构?
【发布时间】:2020-10-12 06:04:36
【问题描述】:

1.例如我创建了多个Tweet节点,这些tweet节点有id、reply_to id和retweet_id。假设我有 6 个节点。下面的代码显示了我创建的节点。

CREATE (n:Tweet:Node {id:'123', title:'A'});
CREATE (cl:TweetLeaf:Node {id:'234', title:'IT Team', reply_to:'123'});
CREATE (cl:TweetLeaf:Node {id:'testingTeam', title:'TESTING Team', reply_to:'234'});
CREATE (cl:TweetLeaf:Node {id:'588', title:'TESTING Team', reply_to:'testingTeam'});
CREATE (cl:TweetLeaf:Node {id:'kk', title:'TESTING Team', retweet_to:'588'});
CREATE (cl:TweetLeaf:Node {id:'119', title:'TESTING Team', retweet_to:'kk'});

2.现在我想创建一个层次结构树并尝试创建两个关系。这是我尝试的代码,但是无法成功创建关系转发。

CREATE (n:Tweet:Node {id:'123', title:'A'});
CREATE (cl:TweetLeaf:Node {id:'234', title:'IT Team', reply_to:'123'});
CREATE (cl:TweetLeaf:Node {id:'testingTeam', title:'TESTING Team', reply_to:'234'});
CREATE (cl:TweetLeaf:Node {id:'588', title:'TESTING Team', reply_to:'testingTeam'});
CREATE (cl:TweetLeaf:Node {id:'kk', title:'TESTING Team', retweet_to:'588'});
CREATE (cl:TweetLeaf:Node {id:'119', title:'TESTING Team', retweet_to:'kk'});


MATCH (c:TweetLeaf)
WHERE NOT (c)-[:reply_to]->() or not (c)-[:retweet]->()
MATCH (parent:Node {id:c.reply_to}),(retweet:Node {id:c.retweet_id})
CREATE (c)-[:reply_to]->(parent),(c)-[:retweet]->(retweet)

【问题讨论】:

    标签: neo4j cypher


    【解决方案1】:
    1. TweetLeaf 节点都没有“retweet_id”属性。而且,虽然有些确实有“retweet_to”属性(可能是错字),但没有一个对应的值是“123”。因此,无论哪种方式,您的查询都不会成功。

    2. 另外,您应该使用MERGE 来避免创建重复节点。这个查询可能会更好(一旦你解决了上面的问题 #1):

      MATCH (c:TweetLeaf), (parent:Node {id: c.reply_to}), (retweet:Node {id: c.retweet_id})
      MERGE (c)-[:reply_to]->(parent)
      MERGE (c)-[:retweet]->(retweet)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      相关资源
      最近更新 更多