使用Cypher从Neo4j中的两个列表创建一个带有函数的新列表

时间:2018-02-19 17:44:02

标签: neo4j cypher

我有两个列表,每列有两列,如下所示:

with [{Word:'word1',B1Weight:0.3},{Word:'word2',B1Weight:0.2}] as BOWS1,
[{Word:'word2',B2Weight:1.5},{Word:'word3',B2Weight:2.0}] as BOWS2

如何获得包含三个单词的“Word”列的单个列表,以及一个等于的“NewWeight”列:(B1Weight * 10)+ B2Weight 因此,如果一个单词仅存在于一个列表中,则来自另一个列表的权重为0。 我可以添加一个不同的单词列表:

unwind BOWS1 + BOWS2 as myList
with distinct myList.Word as words
return words

我不确定如何从列表中获取权重并创建新的结果列。我希望得到一个结果:

[{Word:'word1',NewWeight:3.0},{Word:'word2',NewWeight:3.5},{Word:'word3',NewWeight:2.0}]

谢谢!

1 个答案:

答案 0 :(得分:1)

你非常接近。您需要聚合函数sum

with
    [{Word:'word1',B1Weight:0.3},{Word:'word2',B1Weight:0.2}] as BOWS1,
    [{Word:'word2',B2Weight:1.5},{Word:'word3',B2Weight:2.0}] as BOWS2
unwind
    BOWS1 + BOWS2 as wd
with
    wd.Word as word,
    sum(wd.B1Weight) as w1,
    sum(wd.B2Weight) as w2
return
    collect({
        Word: word,
        NewWeight: w1 * 10 + w2
    })

P.S。这是有效的,因为以下查询将返回一个:

unwind [null, 1] as i 
return SUM(i) // = 1