从Neo4j中的现有设置添加唯一节点及其之间的关系 [英] Add Unique Nodes and relationship between them from Existing Setup in Neo4j
问题描述
这是此处定义的问题的延续
This is in continuation of the problem defined here
所以情况看起来像下面的图像(请忍受the脚的图像)
So the situation looks like the image below(please bear with the crappy image)
蓝色链接表示 [:RELATES_TO] 关系,黑框中的数字表示 Length 属性的值.所有其他 [:RELATES_TO] 关系也存在相似的值,此处未显示.
The blue links denotes the [:RELATES_TO] relationship with the number in black boxes denoting the value of Length property. Similar values also exists for all such other [:RELATES_TO] relationship which is not shown here.
现在,我想根据执行者节点的名称"属性 查找并创建唯一的节点 .继续该链接中的示例,将只有4个新的唯一节点 [A,B,C,D] .让我们用 name作为属性将它们称为 NewUniqueNodes .
Now I would like to find and create unique Nodes based on 'Name' property of Performer Nodes. Continuing with the example in the link there will be only 4 New Unique Nodes [A,B,C,D]. Lets Call them NewUniqueNodes with name as a property.
然后,我想依次查询每种情况.在每种情况下,我都需要按 Length属性的递增顺序 查询[:RELATES_TO]关系.对于任何这样的一对节点(x,y),我需要从NewUniqueNode(Name:x)向NewUniqueNode(name:y)添加一个关联关系 [:FINALRESULT {strength:0}] ,其强度已更新为(强度+ 值). 值 是与 [:RELATES_TO] 的 value 属性的关联数字.这对 nodes(x,y).
Then I would like to query each case in turn. Within each case, I need to query [:RELATES_TO] relationship in increasing order of Length property. For any such pair of nodes (x,y) I need to add a relationship [:FINALRESULT{strength:0}] from NewUniqueNode(Name:x) to NewUniqueNode(name:y) with strength being updated to (strength + value). The value is the number associated with value property of [:RELATES_TO] for the pair of nodes(x,y).
[示例和预期输出]
在case1中,访问节点的顺序为
[Example and Expected Output]
In case1, the order of visiting nodes will be
Node(ID:3) to Node(ID:4)
Node(ID:1) to Node(ID:2)
Node(ID:1) to Node(ID:3)
在处理这些节点时,结果将是
On processing these nodes, the result would be
NewUniqueNode(name:A)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:D)
NewUniqueNode(name:A)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:B)
NewUniqueNode(name:B)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:A)
在处理全部案例( case1 + case2 + case3 )时,结果将类似于
On processing the full set of cases(case1 + case2 + case3), the result would be something like
NewUniqueNode(name:A)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:D)
NewUniqueNode(name:A)-[:FINALRESULT{strength: 3}]-NewUniqueNode(name:B)
NewUniqueNode(name:B)-[:FINALRESULT{strength: 2}]-NewUniqueNode(name:A)
NewUniqueNode(name:C)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:B)
NewUniqueNode(name:A)-[:FINALRESULT{strength: 1}]-NewUniqueNode(name:A)
推荐答案
根据此Neo4j控制台设置,基于上一个问题 http://console.neo4j.org/r/vci9yd 我有以下查询:
According to this Neo4j console setup, based on the previous question http://console.neo4j.org/r/vci9yd I have the following query :
MATCH (n:Performer)
WITH collect(DISTINCT (n.name)) AS names
UNWIND names as name
MERGE (nn:NewUniqueNode {name:name})
WITH names
MATCH (c:Case)
MATCH (p1)-[r:RELATES_TO]->(p2)<-[:RELATES]-(c)-[:RELATES]->(p1)
WITH r
ORDER BY r.length
MATCH (nn1:NewUniqueNode {name:startNode(r).name})
MATCH (nn2:NewUniqueNode {name:endNode(r).name})
MERGE (nn1)-[rf:FINAL_RESULT]->(nn2)
SET rf.strength = CASE WHEN rf.strength IS NULL THEN r.value ELSE rf.strength + r.value END
说明:
-
首先,我们匹配所有执行者节点,并在names变量中收集不同的名称值.
First we match all performer nodes and collect the distinct name values in the names variable.
第二,我们使用UNWIND子句迭代名称,为名称集合中的每个名称创建一个NewUniqueNode
Secondly, we iterate the names with the UNWIND clause, creating a NewUniqueNode for each name in the names collection
然后我们匹配所有情况,在每种情况下,我们都寻找在这种情况下的:RELATES_TO关系,并根据关系长度值对其进行排序
Then we match all cases, within each case we look for the :RELATES_TO relationships that are inside this case and ordering them by the relationship length value
然后,对于找到的每个关系,我们匹配与startNode名称值对应的NewUniqueNode,并为与endNode名称值对应的NewUniqueNode匹配
Then for each relationship found, we match the NewUniqueNode corresponding to the startNode name value, and same for the NewUniqueNode corresponding to the endNode name value
最后,我们合并这两个唯一节点之间的:FINAL RESULT关系,然后根据:RELATES_TO关系长度值,在该关系上设置强度属性,对于这一部分,我想您可以使用ON进行相同操作在合并中创建并进行匹配
Lastly we merge the :FINAL RESULT relationship between those two unique nodes, we then set the strength property on the relationship depending of the :RELATES_TO relationship length value, for this part I guess you could do the same with ON CREATE and ON MATCH on the MERGE
这篇关于从Neo4j中的现有设置添加唯一节点及其之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!