Create graph of node similarity result

When we execute CALL gds.nodeSimilarity.stream we get data in form of table. Is there way to display result in graph format ?

The tabular data returned is the node id of each nodes and their corresponding similarity score. You can use the following utility function to return each node instead of their id values.

gds.util.asNode(node_id)

You could create a virtual relationship between each pair of nodes with a similarity property set to the nodes’ similarity score. You can return the virtual relationship with the nodes so it is displayed in the browser. Change the display of the relationship type you assigned to the virtual relationship to show the similarity property, so it shows in the browser.

I can help with query if you need assistance. I am just not near my computer now to do it, but can later if needed.

Hello @glilienfield I am trying to achieve following use case

Some one gave idea to create BI partite graph . So I created that .
One set of node representing users and other documents. Then I tried with similarity . But I want result to display as shown in use case or something similar . Could you please help .

CALL gds.nodeSimilarity.stream('myGraph')

YIELD node1, node2, similarity

WITH gds.util.asNode(node1) AS Person1, gds.util.asNode(node2) AS Person2, similarity

WHERE Person1.pcode = 33

RETURN Person1.ClientFirstName, Person2.ClientFirstName, similarity

ORDER BY similarity DESCENDING,Person1.ClientFirstName , Person2.ClientFirstName

Result I am getting is :

@glilienfield Please let me know if more details are required .

You can try the following to visualize the users and their similarity.

Test Data

create 
(n1:User{name:"User_A"}), 
(n2:User{name:"User_B"}), 
(n3:User{name:"User_C"}),
(b1:Book{title:"A"}),
(b2:Book{title:"B"}),
(b3:Book{title:"C"}),
(b4:Book{title:"D"}),
(b5:Book{title:"E"}),
(b6:Book{title:"F"}),
(b7:Book{title:"G"}),
(b8:Book{title:"H"}),
(n1)-[:HAS_READ]->(b1),
(n1)-[:HAS_READ]->(b3),
(n1)-[:HAS_READ]->(b7),
(n2)-[:HAS_READ]->(b1),
(n2)-[:HAS_READ]->(b2),
(n2)-[:HAS_READ]->(b3),
(n2)-[:HAS_READ]->(b4),
(n2)-[:HAS_READ]->(b5),
(n2)-[:HAS_READ]->(b6),
(n3)-[:HAS_READ]->(b4),
(n3)-[:HAS_READ]->(b5),
(n3)-[:HAS_READ]->(b6),
(n3)-[:HAS_READ]->(b8)

Projection:

CALL gds.graph.project(
  'users',
  ['User','Book'],
  'HAS_READ'
)
YIELD
  graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels

Query:

CALL gds.nodeSimilarity.stream('users')
YIELD node1, node2, similarity
WITH gds.util.asNode(node1) AS user1, gds.util.asNode(node2) AS user2, similarity
WHERE elementId(user1) < elementId(user2)
RETURN user1, user2, apoc.create.vRelationship(user1,'HAS_SIMILARITY',{similarity:similarity}, user2) as rel

Result:

1 Like

I will run this for actual data and share my input. Apologies I cannot share actual data.

1 Like

Hello @glilienfield

I ran all the steps and I am trying to see only similarities with respect to one user i.e. graph of all users who has read same documents as read by particular user by adding where condition then why in result am I seeing all the outputs which are not even related to this user ? Not seeing anything related to this user only

CALL gds.nodeSimilarity.stream('users')

YIELD node1, node2, similarity

WITH gds.util.asNode(node1) AS user1, gds.util.asNode(node2) AS user2, similarity

WHERE elementId(user1) < elementId(user2)

and user1.ClientFirstName = "Aaron"

RETURN user1, user2, apoc.create.vRelationship(user1,'HAS_SIMILARITY',{similarity:similarity}, user2) as rel

It should work. Can you provide a sample of the output?

BTW - this is not the cause of your issue, but you would not need the elementId(user1) < elementId(user2) condition if you are specifying a specific user1. The condition was to eliminate adding a virtual relationship in both directions between a pair of nodes.

If you see result it does not have Aaron as name still showing in graph result .

Do you want me to share anything else which I am missing .Please let me know

Can you click on the "text" icon to the left and share that?

Just a note, you are seeing the "SIMILAR" relationship between nodes, which are not returned in the query, because you have the "Connect result nodes" option checked in the settings. This is the default behavior.

Unselect that if you want to clean up the output.

Screen Shot 2024-01-02 at 1.00.05 PM

1 Like