[英]Creating directed edges between nodes based on Time property in Neo4j
I am new to Neo4j and Cypher and while converting our Company's Relational DB to Graph Based Model I encountered a problem and I would appreciate any answer.我是 Neo4j 和 Cypher 的新手,在将我们公司的关系数据库转换为基于图形的模型时,我遇到了一个问题,我将不胜感激。
I have nodes with type person in my model like this:我的模型中有一个类型为 person 的节点,如下所示:
(:Person {Fname: 'John', catID: 1})
(:Person {Fname: 'George', catID: 2})
(:Person {Fname: 'Natalie', catID: 3})
......
I also have time based Category nodes like this:我也有这样的基于时间的类别节点:
(:Category {Id: 1, Date: '2015-02-05'})
(:Category {Id: 1, Date: '2015-01-05'})
(:Category {Id: 1, Date: '2015-03-10'})
(:Category {Id: 3, Date: '2014-03-10'})
(:Category {Id: 3, Date: '2015-05-10'})
......
Now I want to created direct edges from each person to the node in its category with minimum date.现在我想创建从每个人到其类别中具有最小日期的节点的直接边。 What I mean in the above example:
我在上面的例子中的意思是:
(:Person {Fname: 'John', catID: 1}) ---> (:Category {Id: 1, Date: '2015-01-05'})
(:Person {Fname: 'Natalie', catID: 3}) ---> (:Category {Id: 3, Date: '2014-03-10'})
and after that, I want to create directed edges between nodes in each category based on their Date property in an ascending order.之后,我想根据它们的 Date 属性按升序在每个类别中的节点之间创建有向边。 I mean:
我的意思是:
(:Category {Id: 1, Date: '2015-01-05'}) ---> (:Category {Id: 1, Date: '2015-02-05'}) ---> (:Category {Id: 1, Date: '2015-03-10'})
(:Category {Id: 3, Date: '2014-03-10'}) ---> (:Category {Id: 3, Date: '2015-05-10'})
What is the cypher code required to do these things.做这些事情所需的密码是什么。 Thanks a lot in advance
非常感谢提前
Here's how you'd do the first part of your question, attaching people to their corresponding category nodes with the minimum date:以下是您如何完成问题的第一部分,将人们附加到具有最短日期的相应类别节点:
MATCH (p:Person)
MATCH (c:Category)
WHERE p.catID = c.Id
WITH p, c
ORDER BY p.Fname, c.Date
WITH p, HEAD(COLLECT(c)) AS most_recent
MERGE (p)-[:IN_CATEGORY]->(most_recent)
Now we can do:现在我们可以这样做:
MATCH (p:Person)-[:IN_CATEGORY]->(c:Category)
RETURN p.Fname, c.Date
And we get the result:我们得到了结果:
p.Fname c.Date
John 2015-01-05
Natalie 2014-03-10
To answer your second question, we can do this:要回答您的第二个问题,我们可以这样做:
MATCH (c:Category) WITH c
ORDER BY c.Id, c.Date
WITH c.Id AS id,
COLLECT(c) AS categories,
COUNT(c) AS count
UNWIND range(1, count - 1) AS idx
WITH categories[idx - 1] AS from , categories[idx] AS to
CREATE (from)-[:NEXT]->(to);
I stole a few ideas from Michael's answer on that one.我从迈克尔的回答中窃取了一些想法。 :) You can now write queries like this:
:) 您现在可以编写这样的查询:
MATCH p = (:Person)-[:IN_CATEGORY]->(:Category)-[:NEXT*]->(:Category)
RETURN p
Check out the finished product here: http://console.neo4j.org/r/999rzg在此处查看成品: http : //console.neo4j.org/r/999rzg
It is almost straightforward :)这几乎很简单:)
MATCH (c:Category)
WITH c order by c.Date
// ordered list
WITH collect(c) as cats
// index for collection
UNWIND range(1,size(cats)-1) as idx
// subsequent entries
WITH cats[idx-1] as first, cats[idx] as second
CREATE (first)-[:NEXT]->(second)
RETURN count(*);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.