简体   繁体   English

Neo4j-需要聚合同一对节点之间不同关系上的值

[英]Neo4j - Need to aggregate values on different relationships between same pair of nodes

I have to multiply the properties on two different relationships between same pair of nodes and SUM() it,then ORDER BY the pairs based on the value. 我必须将同一对节点之间的两个不同关系和SUM()的属性相乘,然后根据值对对进行ORDER BY Ex: 例如:

Let (X:amps)-[:coocr{val:1}]->b
    (X:amps)-[:jacc{dist:2}]->b
    (X:amps)-[:coocr{val:2}]->c
    (X:amps)-[:jacc{dist:3}]->c
    (X:amps)-[:coocr]->d
    (X:amps)-[:jacc{dist:4}]->d
    (Y:amps)-[:coocr{val:2}]->b
    (Y:amps)-[:jacc{dist:3}]->b
    (Y:amps)-[:coocr{val:1}]->c
    (Y:amps)-[:jacc{dist:4}]->c
    (Y:amps)-[:coocr{val:4}]->d
    (Y:amps)-[:jacc{dist:3}]->d

Now, 1) Each node b,c,d has two relationships with X,Y, which are multiplies and added pair wise 2)Return the highest value 现在,1)每个节点b,c,d与X,Y有两个关系,它们分别相乘并成对相加2)返回最大值

My attempt to the prob: 我对问题的尝试:

WITH [b,c,d] AS words                                                                
MATCH (i:amps) 
MATCH n where n.word in words
MATCH p=(i-[r:jaccard]->(n)) with i,r.dist as dist UNWIND dist as distances
MATCH q=(i-[s:coocr]->(n)) with distances,i,s.val as co UNWIND co as coocr
WITH i, SUM(distances*coocr) AS agg
RETURN i,agg ORDER BY agg DESC

My aggragate function has to work like: 我的汇总功能必须像这样工作:

X-[]->b has[jaccard,coocr] , so, jacc.dist*coocr.val = 1*2 =2
X-[]->c has[jaccard,coocr] , so, jacc.dist*coocr.val = 2*3 =6
X-[]->d has[jaccard,] , so, jacc.dist*null = null*2 =0
                                                    --------------
                                                      sum  = 8
                                                    --------------

Y-[]->b has[jaccard,coocr] , so, jacc.dist*coocr.val = 4*2 =8
Y-[]->c has[jaccard,coocr] , so, jacc.dist*coocr.val = 1*3 =3
Y-[]->d has[jaccard,coocr] , so, jacc.dist*coocr.val = 4*4 =16
                                                    --------------
                                                      sum  = 27
                                                    --------------

Then returning these aggregate orderly. 然后按顺序返回这些聚合。

My query returning the aggregates, which are only products, not the whole sum. 我的查询返回的聚合只是产品,而不是总和。 Need to achieve the whose sum. 需要实现其总和。

What about this? 那这个呢?

MATCH (a:amps)
OPTIONAL MATCH (a)-[coocr:coocr]-(n)
OPTIONAL MATCH (a)-[jacc:jacc]-(n)
WITH a, COALESCE(coocr.val, 0) AS val, COALESCE(jacc.dist, 0) AS dist
RETURN a, SUM(val * dist) AS agg
ORDER BY agg DESC
WITH ["best", "high", "quality","4k"] AS words                                                                
MATCH (i:amps) 
MATCH n where n.word in words
OPTIONAL MATCH p=(i-[r:jaccard]->(n)) with n,i,COALESCE(r.dist, 0)  as distances
OPTIONAL MATCH q=(i-[s:coocr]->(n)) with distances,n,i,COALESCE(s.val, 0) AS  coocr
WITH i,n,distances,coocr, (distances*coocr) AS agg
WITH i,SUM(agg) AS agg
RETURN i,agg

Aggregation function,in last but one line resolved the problem. 聚合功能,最后只用一行就解决了这个问题。 Thanks for the suggestion Brain. 感谢您的建议Brain。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM