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.

Calculating Similarity of Nodes based on relations

Hi Team,

I was looking for calculating the user similarity represented as nodes in the below figure. I wanted to calculate the similarity based on all the three relations Age, Gender, and Location.

Most of the documentation talks about a single relation to find similarity and I am looking for a query that returns the similarity between the user nodes based on all the relations. Could you please help.

3X_0_a_0aa6da889ee31b16bc8117a23cf302321acab306.png

The code for creating the above graph is as below

MERGE (A:AgeRange{Name:"5-10"})
MERGE (A1:AgeRange{Name:"25-30"})
MERGE (L:Location{Name:"USA"})
MERGE (L1:Location{Name:"Japan"})
MERGE (G:Gender{Name:"Male"})
MERGE (G1:Gender{Name:"Female"})

MERGE (P:User{Name:"User A"})
MERGE (P)-[:HAS_AGE]-(A)
MERGE (P)-[:LIVES_IN]-(L)
MERGE (P)-[:HAS_GENDER]-(G)


MERGE (P1:User{Name:"User B"})
MERGE (P1)-[:HAS_AGE]-(A1)
MERGE (P1)-[:LIVES_IN]-(L1)
MERGE (P1)-[:HAS_GENDER]-(G1)



MERGE (P2:User{Name:"User C"})
MERGE (P2)-[:HAS_AGE]-(A1)
MERGE (P2)-[:LIVES_IN]-(L)
MERGE (P2)-[:HAS_GENDER]-(G)


MERGE (P3:User{Name:"User D"})
MERGE (P3)-[:HAS_AGE]-(A)
MERGE (P3)-[:LIVES_IN]-(L1)
MERGE (P3)-[:HAS_GENDER]-(G1)



MERGE (P4:User{Name:"User E"})
MERGE (P4)-[:HAS_AGE]-(A)
MERGE (P4)-[:LIVES_IN]-(L1)
MERGE (P4)-[:HAS_GENDER]-(G)


MERGE (P5:User{Name:"User F"})
MERGE (P5)-[:HAS_AGE]-(A1)
MERGE (P5)-[:LIVES_IN]-(L)
MERGE (P5)-[:HAS_GENDER]-(G1)


MERGE (P6:User{Name:"User G"})
MERGE (P6)-[:HAS_AGE]-(A)
MERGE (P6)-[:LIVES_IN]-(L)
MERGE (P6)-[:HAS_GENDER]-(G1)


MERGE (P7:User{Name:"User H"})
MERGE (P7)-[:HAS_AGE]-(A1)
MERGE (P7)-[:LIVES_IN]-(L1)
MERGE (P7)-[:HAS_GENDER]-(G)
1 REPLY 1

ameyasoft
Graph Maven
Try this:
match (b:AgeRange) where b.Name = "25-30"
match (a:User)-[]-(b)
with collect(id(a)) as u1, count(a) as cnt where cnt > 1
match (c:User) where id(c) in u1
with c order by id(c) ASC
with collect(c) as u2
CALL apoc.nodes.link(u2, 'CONNECTED_BY_AGERANGE {name:"25-30"}')
return u2

match (b:Location) where b.Name = "USA"
match (a:User)-[]-(b)
with collect(id(a)) as u1, count(a) as cnt where cnt > 1
match (c:User) where id(c) in u1
with c order by id(c) ASC
with collect(c) as u2
CALL apoc.nodes.link(u2, 'CONNECTED_BY_LOCATION {name:"USA"}')
return u2

match (b:Gender) where b.Name = "Female"
match (a:User)-[]-(b)
with collect(id(a)) as u1, count(a) as cnt where cnt > 1
match (c:User) where id(c) in u1
with c order by id(c) ASC
with collect(c) as u2
CALL apoc.nodes.link(u2, 'CONNECTED_BY_GENDER {name:"Female"}')
return u2

Run the above queries by changing the age range , location, gender

Here is the result: