2
votes

I'm using neo4j 2.1.2 version community edition. I have one issue in finding the path which i'm looking for using cypher. I have data in csv file and loaded the data into neo4j database using LOAD CSV option. The data looks like below:

Query Used:

LOAD CSV WITH HEADERS FROM "file:C:\\WorkingFolder\\Neo4j\\EDGE_Graph_POC\\newdata\\test.csv " as file
MERGE (role:Role {Role:file.RoleName})
MERGE (u:User {User:file.UserName})
Merge (c:Country {Country:file.Country})
MERGE (s:State {State: file.State})
MERGE (a:Address {Address:file.Address})
MERGE (comp:Company {Company: file.Company})

MERGE (u)-[:Has]->(role)-[:GivenAccess_To]->(a)-[:Present_IN]->(s)-[:Belongs_TO]->(c)- [:Comes_Under]->(comp)

Data file Used:

enter image description here

Resultant Graph: enter image description here

Then I merged two addresses into a single Address with a relationship as below and given access to a User called 'Susan Smith' to access the newly merged address which is shown below: Graph: enter image description here

Issue I'm facing: Here If someone wants to see the merged address which belongs to which country and state and also to see which user is given access.Also in the above image i have marked the path which i'm looking for.

The query i used to get the path is :

match path = (x)-[r1:Merged_TO]->(y)
with y
match path1  = (u:User)-[:Has]->()-[:GivenAccess_To]->(y)-[:Present_IN]->(s)-[:Belongs_TO]->(b)-[:Comes_Under]->(comp)
return path1;

Output Graph : enter image description here

But here i'm supposed to get only one user called Susen Smith but getting all the users who are attached to Publisher node. So how can i get that information.

Expected Graph is : enter image description here**NOTE :** Here i do not want to pass the User name. wheneven i enter the Merged_TO relationship, i should get the new merged node and user information who is supposed to access that node.

How can i do it?

1

1 Answers

1
votes

I believe that your model has been setup to give all Publishers access to the Address rather than a specific Person.

In your original import this occurs in the fragment:

(u)-[:Has]->(role)-[:GivenAccess_To]->(a)

Maybe it should have been:

(u)-[:HAS]->(role), (u)-[:GivenAccess_To]->(a)

This would make the access not dependent on the role but on the User. You can then use your query with one minor adjustment (but it will not contain the role).

MATCH (x)-[r1:Merged_TO]->(y)
WITH y
MATCH path1  = (u:User)-[:GivenAccess_To]->(y)-[:Present_IN]->(s)-[:Belongs_TO]->(b)-[:Comes_Under]->(comp)
return path1;

Update: After Roles clarified.
If it is the case that a User does not have access to an Address, and that a Role does not have access to an Address, but that it is a User in a particular Role that does, then you will have to add a further level of indirection. Something like:

...
MERGE (u)-[:IN_USER_ROLE]->(ur:UserRole)<-[:HAS_ROLE]-(role)
(ur)-[:GivenAccess_To]->(a)
...

Now to get the lot back you can use something like:

MATCH (x)-[r1:Merged_TO]->(y)
WITH y
MATCH path1  = (u:User)-[:IN_USER_ROLE]->(ur:UserRole)-[:GivenAccess_To]->(y)-[:Present_IN]->(s)-[:Belongs_TO]->(b)-[:Comes_Under]->(comp)
MATCH (ur)<-[:HAS_ROLE]-(role)
return path1, role;