Neo4j - Calculate similarity and Insert a new relationship between nodes if not exists

谁说胖子不能爱 提交于 2019-12-13 05:19:41

问题


Edited: I need to check whether the relationship exists or not. If not exists then calculate similarity between nodes, which is bit time taking. And then, i need to insert the relation ship between them. I need to do the repeat this for all the pair of nodes in the graph.

Programmatic paradigm for this scenario is like:

 If relationship exits
     then calculate similarity and insert relationship
 else
     do nothing (or) return value

There is also problem with this query is, it may also cause memory exceptions. If so, how to overcome this problem.

This is my query,

MATCH (a{word:"review"}),(b{word:"nothing"}) 
MERGE  (a)-[r:jsim]->(b)
MERGE  (a)<-[s:jsim]-(b)
SET r.val = 
  CASE WHEN NOT (HAS (r.val)) 
    THEN [1]
  ELSE 2 END 
SET s.val = 
  CASE WHEN NOT (HAS (s.val)) 
    THEN [2]
  ELSE 1 END 
RETURN r,s

In my actual problem, the FALSE case has a big query which iterate through all the nodes in the graph which has to store many values in the stack. So, here the memory exception may arise.

My IF ELSE CASE query is :

MATCH (a)-[r]->(b) where r.val>1
WITH collect(DISTINCT b.word) as our_word_pairs,a
MATCH (c)-[r]->(d) where r.val>1 AND Not c = a
WITH collect(DISTINCT d.word) as other_word_pairs,a,c,our_word_pairs
WITH FILTER(X in our_word_pairs where X in other_word_pairs) AS word_pair_intersection,
(our_word_pairs+other_word_pairs) AS all_word_pairs,other_word_pairs,a,c,our_word_pairs
WITH DISTINCT(all_word_pairs) as all_word_pairs,word_pair_intersection,a,c
WITH (1.0*SIZE(word_pair_intersection)/SIZE(all_word_pairs)) AS jsim

Now the jsim is the value I need to assign.

Assume a and b are two nodes, I have to find similarity between them and add the relationship with the value. Similarity of A and B is the common nodes between them divided by the total no.of nodes they are connected with.

Ex: A-->p,A-->q.A-->r,A-->s
    B-->r,B-->s,B-->t,B-->u,B-->v

   Sim(A,B) = Common nodes/Total Nodes
            = 2/7

来源:https://stackoverflow.com/questions/33366621/neo4j-calculate-similarity-and-insert-a-new-relationship-between-nodes-if-not

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!