[英]create relationships between nodes in parallel
Using cypher and neo4j 2.0. 使用cypher和neo4j 2.0。
Given two sets of node ids (of equal length) and a set of weights, I'd like to create a relationship between the corresponding nodes and set the weight as a property. 给定两组节点ID(长度相等)和一组权重,我想在相应节点之间创建一个关系,并将权重设置为一个属性。 For example if I have the following three lists: 例如,如果我有以下三个列表:
node list 1: (101, 201, 301)
node list 2: (102, 202, 302)
weights: (0.1, 0.6, 0.25)
I would like to create the following representation 我想创建以下表示形式
101 - knows {w : .1} - 102
201 - knows {w : .6} - 202
301 - knows {w : .25} - 302
but NOT, for example, 101 - knows - 302
但不是,例如101 - knows - 302
I can do this by iterating over my parameters and then creating the individual queries. 我可以通过遍历参数然后创建单个查询来做到这一点。 Is there a way to batch run this, passing my lsits as parameters and asking cypher
to match the nodes & properties in order? 有没有一种方法来批量运行此,通过我的lsits作为参数,并要求cypher
相匹配才能节点和属性?
I thought for a moment that using parameters in the following fashion would work, but it instead creates all permutations of relationships (as expected) and assigns as the entire list of weights as a property to each relationship. 我想了一会儿,以下面的方式使用参数是可行的,但是它创建了关系的所有排列(如预期的那样),并将权重的整个列表分配为每个关系的属性。
{
"query":
"START a1=node({starts}), a2=node({ends})
CREATE UNIQUE a1-[r:knows {w : {weights}}]-a2
RETURN type(r), r.w, a1.name, a2.name",
"params": {
"starts" : [101, 201, 301],
"ends" : [102, 202, 302],
"weights" : [0.1, 0.6, 0.25]
}
}
How large are your lists in real life? 您的清单在现实生活中有多大? I'd probably send in one triple at a time. 我可能一次发送三倍。
Otherwise you should be able to use a collection and foreach to do what you want: 否则,您应该能够使用collection和foreach来执行所需的操作:
START a1=node({starts}), a2=node({ends})
FOREACH(w in filter(w in weights : head(w)=id(a1) AND head(tail(w))=id(a2)) :
CREATE UNIQUE a1-[r:knows {w : last(w)}]-a2
)
"params": {
"starts" : [101, 201, 301],
"ends" : [102, 202, 302],
"weights" : [[101,102,0.1], [201,202,0.6], [301,302,0.25]]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.