cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Get similarity node pairs with all the common relationships

Hi all,
This is my initial graph.

I created a cypher projection(memory graph) and ran the node similarity algorithm to find nodes that are similar to the node 'AE2001'. I ran it in stream mode, creating a relationship 'SIMILAR' having a property 'score' which has the similarity score(we got from the result of the algorithm) between the nodes. (I did not use write mode because it created 2 relationships for every node).

This is my code for the memory graph:

call gds.graph.create('graph','*',['reports','condition','reported_on'])

My code to run the algorithm:

CALL gds.nodeSimilarity.stream('graph')
YIELD node1, node2, similarity
WITH gds.util.asNode(node1) AS n1, gds.util.asNode(node2) AS n2, similarity
WHERE n1:Adverse_Event
AND n1.name='AE2001'
AND n2:Adverse_Event 
MERGE (n1)-[s:SIMILAR]-(n2)
SET s.score = similarity
RETURN n1, n2, similarity
order by similarity desc

My graph after creating the relationships looks like this:

I want to show all the similarity pairs with the relationships they have in common. (In this case, I should get the 3 following pairs:

  1. Node AE2001 and Node AE0001
  2. Node AE2001 and Node AE007
  3. Node AE2001 and Node AE0005
    I am attaching the picture for the first case below for more clarity

    I researched a lot but couldn't find a solution. Any help is greatly appreciated. Thanks in advance!
    PS: Apologies for the last image, I just used the eraser tool in paint
7 REPLIES 7

Would something like this work? So if two nodes have a SIMILARITY relationship, you would like to see all other nodes the both have relationships as well?

match (e1:Adverse_Event)-[s:SIMILARITY]-(e2:Adverse_Event)
where id(e1) < id(e2)
with e1, e2
match (e1)-[r1]->(other)<-[r2]-(e2)
return *

Hi @johnmattgrogan , thanks for your reply. This will not work because I have multiple relationships of type 'SIMILAR' going out from the node 'AE2001'. I want to get the node pairs with 'SIMILAR' relationship. This querry gave me the following result.

On the other hand, I wanted to return pairs of nodes (of type Adverse_Event) having all the common nodes, as I showed in my main post picture 3.

I see. Is the problem having more than two Adverse_Event nodes displaying in the same graph? Is it looks, the only difference between your picture 3 and the picture with my query is the additional 'AE007' and 'AE0005' nodes. However, I could be miss understanding what your asking

You are correct. That is the only difference. I want to compare 'AE2001' with all the other nodes one by one, and the only problem in your query is we're getting 'AE007' and 'AE0005' nodes.

Gotcha. So are you wanting to visualize these pairs relationships in the Neo4j browsers? When you specify the patttern (n)-[:SIMILAR]->(m) the cypher will connect all nodes together that match this pattern. This pattern is then rendered in the browser.

If you need the individual pairs visualization and the number of nodes is small you could hard code those individual nodes into the browser and get a new viz for each query. On the other hand, if you have any experience using using Python and one of the drivers (py2neo, neo4j) you can write a little script to query each of the pairs and return the data for later processing.

I'm not saying there isn't another way to do this, however to the best of my knowledge there is no way to "plot" pairs of non distinct nodes in the same visualization. However, I'll leave this open to any others out there who know better then I.

-Matt

Thanks for your quick reply Matt. I am using Neovis to visualize the graph in an HTML file. I have also worked with neo4j(python driver) before and I can use a loop to compare it node-wise. Thanks for the suggestion

Great! If you get something that works I’d love to see what you did. I’ve never really used any of the viz techniques outside the browser so I’d be very interested in seeing that.

Thanks!

-Matt