[英]Computing similarity between all nodes neo4j - getting different values for a node pair
[英]Neo4j - Need to aggregate values on different relationships between same pair of nodes
我必須將同一對節點之間的兩個不同關系和SUM()的屬性相乘,然后根據值對對進行ORDER BY 。 例如:
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
現在,1)每個節點b,c,d與X,Y有兩個關系,它們分別相乘並成對相加2)返回最大值
我對問題的嘗試:
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
我的匯總功能必須像這樣工作:
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
--------------
然后按順序返回這些聚合。
我的查詢返回的聚合只是產品,而不是總和。 需要實現其總和。
那這個呢?
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
聚合功能,最后只用一行就解決了這個問題。 感謝您的建議Brain。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.