在节点之间并行创建关系 [英] create relationships between nodes in parallel

查看:58
本文介绍了在节点之间并行创建关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用cypher和neo4j 2.0.

Using cypher and neo4j 2.0.

给出两组节点ID(长度相等)和一组权重,我想在相应节点之间创建一个关系并将权重设置为一个属性.例如,如果我有以下三个列表:

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. 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)

我想创建以下表示形式

 101 - knows {w : .1}  - 102
 201 - knows {w : .6}  - 202
 301 - knows {w : .25} - 302

但不是,例如101 - knows - 302

我可以通过遍历我的参数然后创建单个查询来做到这一点. 有没有一种方法可以批量运行它,将我的lsits作为参数传递并询问 cypher匹配节点&属性顺序?

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?

我想了一下,以下面的方式使用参数是可行的,但是它创建了关系的所有排列(如预期的那样),并将权重的 entire 列表分配为每个关系.

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.

否则,您应该可以使用集合和foreach做您想要做的事情:

Otherwise you should be able to use a collection and foreach to do what you want:

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]]
}

这篇关于在节点之间并行创建关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆