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.

How to retrieve common relations between two nodes in neo4j

I have a dataset of the list of employees working for a company, the dataset consists of different columns. I had loaded this dataset in neo4j idle using cypher query. I had created nodes and relationships between rows and columns.
my cypher query

:auto USING PERIODIC COMMIT 5
                 LOAD CSV WITH HEADERS FROM 'file:///y.csv' AS line
                 merge (jm:JM {employjoining:line.MonthNamofJoining})
                 merge (jy:JY {employjoiningyear:line.YearofJoining})
                 merge (id:ID {Employe_ID:line.EmpID})
                 merge (fn:FN {Employfirstname:line.FirstName})
                 merge (ln:LN {Employlastname:line.LastName})
                 merge (g:G {gender:line.Gender})
                 merge (id)-[:Gender]->(g)-[:FirstName]->(fn)-[:LastName]->(ln)-[:MonthNamofJoining]->(jm)-[:YearofJoining]->(jy)
                 RETURN id,g,fn,ln,jm,jy limit 10

i am new to neo4j i tried very hard to find relationships between nodes. But i failed manytimes in retrieving data. i tried some cypher queries but not able to get exact output. please help me writing cypher queries to below cases

  1. How to get all relations between two given nodes (for example between two employee id )
  2. Select Other employees similar to the given Employee
  3. relation between set of employees
13 REPLIES 13

ameyasoft
Graph Maven
You are creating a node for each employee property. Instead, you should model them as node properties. Create an Employee node add these properties. Here is the Cypher:

MERGE (a:Employee {empid:line.EmpID, firstname:line.FirstName, lastname:line.LastName, gender:line.Gender, joiningMonth:toInteger(ine.MonthNamofJoining), joiningYear:toInteger(line.YearofJoining)})

Here you are creating Employee nodes and they are not connected to each other. 

Run this query to get all Employee nodes with joining year = 2020

MATCH (c:Employee)
WHERE c.joiningYear = 2020    





Thank you. But I want to know all common properties shared by any two employees
I want a similar relationship shared with one employee node to other employees in the dataset (example in terms of joining month, year, location, etc.). for example, from the list of employees, i had selected one employee as interest and I want to know how other employees are similar to that employee

  1. Select Other employees similar to the given Employee
  2. relation between a set of employees

Thank you

Here is another way:
Created Year and Month nodes and connected them to Employee node. Here is the Cypher:

MERGE (y:Year {year:toInteger(line.YearofJoining)})
MERGE (m:Month {month:line.MonthNamofJoining)})
MERGE (y)-[:MONTH]->(m)

MERGE (a:Employee {empid:line.EmpID, firstname:line.FirstName, lastname:line.LastName, gender:line.Gender})
MERGE (m)-[:EMPLOYEE]->(a)

Get all the employees employed in December 2020:

MATCH (y:Year)-[:MONTH]->(m:Month)-[:EMPLOYEE]->(e:Employee)
WHERE y.year = 2020, m.month = "December"
RETURN y, m, e

Get employees with last name = "Doe":

MATCH (e:Employee)
WHERE e.lastname = "Doe"
RETURN e

Like this you can combine many properties and get employees that match the selected filters.

Hi, This query is throwing a syntax error bwtween where y.year=2020 , m.month='December'

WHERE y.year = 2020, m.month = "December"

should be:

WHERE y.year = 2020 AND m.month = "December"

Get two employees list

MATCH (e:Employee)-[:EMPLOYEE]->(y:Year)-[:MONTH]->(m:Month)
WHERE e.empid = "829478" AND e.empid = "940813"
RETURN y, m,e

Here i want to return all relationships and nodes for above two employee id's
But showing the result no changes , no records
how can i get relationships between two nodes

This AND expression will always be FALSE:

WHERE e.empid = "829478" AND e.empid = "940813"

Better is to use IN a list, as you can easily expand or contract the number of ids:

WHERE e.empid IN ["829478", "940813"]

Also, you may want to convert your empids to integers. They are more compact and faster.

MATCH(e:Employee)
  WHERE exists(e.empid) //  this isn't necessary since toInteger(null) is null
set e.empid = toInteger(e.empid)

       MATCH p = (y:Year)-[:MONTH]->(m:Month)-[:EMPLOYEE]->(e:Employee)
       WHERE e.empid IN ["858541", "128466"]
       return p

Here i want to return all relationships and nodes for above two employee id's
But showing the result no changes , no records
how can i get relationships between two nodes

You need to debug this step by step.

First try this:, to see if you get the two employees back.

       MATCH p = (e:Employee)
       WHERE e.empid IN ["858541", "128466"]
       return p

Then try this:, to see if you get the two employees back and the month

       MATCH p = (m:Month)-[:EMPLOYEE]->(e:Employee)
       WHERE e.empid IN ["858541", "128466"]
       return p

etc.

I suspect that you have a typo or having missing data or something...

Thank you, I tried to build a graph model using data, here is the cypher query

MERGE (a:Employee {empid:line.EmpID, firstname:line.FirstName, 
lastname:line.LastName})
MERGE (g:gen{gender:line.Gender})
MERGE (y:Year {year:toInteger(line.YearofJoining)})
MERGE (m:Month {month:line.MonthNamofJoining})
MERGE (ag:Age {age:line.AgeinYrs})
MERGE (wt:Weight{weight:line.WeightinKgs})
MERGE (c:City {city:line.City,pincode:line.PinCode})
MERGE (ct:County {county:line.County})
MERGE (es:State {state:line.State})
MERGE (er:Region {region:line.Region})

MERGE ()-[:EMPLOYEE]->(a)-[:GENDER]->(g)-[:AGE]->(ag)-[:WEIGHT]->(wt)-[:CITY]-> 
(c)-[:COUNTY]-(ct)-[:STATE]->(es)-[:REGION]->(er)-[:YEAR]->(y)-[:MONTH]->(m)

I need to return all connecting path, so i tried below query

 MATCH p = ()-[:EMPLOYEE]->(a)-[:GENDER]->(g)-[:AGE]->(ag)-[:WEIGHT]->(wt)- 
 [:CITY]->(c)-[:COUNTY]->(ct)-[:STATE]->(es)-[:REGION]->(er)-[:YEAR]->(y)-[:MONTH]-> 
 (m)
 WHERE a.empid IN ["858541", "128466"]
 return p limit 25

But the result i got correct but there are many unnecessary paths. i am uploading the resulted graph please check and correct where i am doing wrong.

Thanks in advance

MERGE (g:gen{gender:line.Gender})
MERGE (y:Year {year:toInteger(line.YearofJoining)})
MERGE (m:Month {month:line.MonthNamofJoining})
MERGE (ag:Age {age:line.AgeinYrs})
MERGE (wt:Weight{weight:line.WeightinKgs})

Gender is kinda of ok. But individual nodes for others ?

Month is 12 nodes, Year can be anything, and for each year you have a month relationship. Age is an integer, with any number. Weight is a float and it can be so many ... example 10kg, 10.1kg 10.01 kg, all individual nodes. is there a reason ???

To answer your question about so many relationship is because, in your data there are other "Male" with "Age" 44. There are other people in "TX" who reside in "South".

You should convert the nodes into properties.

what are the nodes should I convert into properties?

share the full header file with description.