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 map nodes dynamically to corresponding section?

This is my second post. I have 5 authors and 2 organization. I want to create a relationship between them. I want to connect [Author,Affliation]-->[org,university] to matching organization. so output will be like [abc,organization 1]--->[organization 1,a university] and [bcd,organization 2]--->[organization 2,b university], [xyz]--->[organization 2, b university],[abcd]--->[organization 2, b university]. please have look on sample json file below...

[
"Authors":
{
Author:"abc",
Affliation:"organization1"
},
{
Author:"bcd",
Affliation:"organization2"
},
{
Author:"xyz",
Affliation:"organization2"
},
{
Author:"abcd",
Affliation:"organization2"
}
],
[
"organization":[
{
org:"organization1"
university:"a university"
}
{
org:"organization2"
university:"b university"
}
]

I'm new to neo4j just exploring. Any hint to this problem will be great help.
Thank you

1 ACCEPTED SOLUTION

Your code seems about right, you could also use UNWIND instead of FOREACH in these cases, but if your creating what you need, that's enough. The only thing is that you aren't actually creating the relationships, you should have something like:

MERGE (author)-[:BELONGS_TO]->(organization)

right after your last MATCH. I used the type of relationship BELONGS_TO and the direction of the relationship is from the author to the organization, feel free to use any other name that best suits your use case. Hope this helps, and I'm sorry to answer until now, it was a bit of a heavy week!

View solution in original post

5 REPLIES 5

Hi @antonyj453 ! If I understand correctly you have three principal entities: Authors, organizations and universities/organization components. Authors are affiliated to organizations, organizations have universities and maybe some other entities, is this right? Maybe an organization can have lots of universities, or an university could be part of multiple organizations.
In order to help you get around the problem I have this questions:

  • What would you want to make with this entities?
  • What kind of questions would you like to answer with this data model?

Thank you very much for your reply. Your understanding is absolute right. I was just exploring some data with neo4j since I'm new to this and want to master it.

Answers to your question

  1. I want to have a clear understanding how entities are dynamically between nodes and their relation, needs to know how that happens.

  2. I would like to know how author is dynamically connected to corresponding university to organization section if there are 'n' number of author and organization.

For example: if a node have A['Author','org1'] and if there are two organization with node B['a university','org1] and node with C['b','org1'] then, node A --->[:connected]->[B,C]

Any help would be great.

Thanks in Advance.

Excellent!
Ok, when you model in Neo4j you have to take into account which are the most important entities in your problem, such as Persons, Hospitals, Banks or in your case Authors, Organizations and Universities; this will be your nodes' labels (which are the way you can categorize nodes in Neo4j). Then you pay attention in the way these entities interact, for example, Persons are Treated in Hospitals, Persons Have Bank Accounts in Banks, these will be the relationships; and in your case Authors are Affiliated to Organizations, Organizations Have Universities, or Universities Are Inside Organizations; it's semantically the same, but it could be useful to have the relationship in this direction (keep in my mind that you should only have one semantically symmetrical relationship between two nodes, unless it is necessary for your model; i.e. if Peter is friends with John, then it's inferred that John is also friends with Peter). Finally, the data that adds context and uniqueness to your nodes or relationships, like a Person's name, their social security number, a Bank Account's id, etc., will be the properties (you can have properties inside the nodes and the relationships). The name of the Author, the Organization and the University could be our properties for this case

Now, this would be visualization of the model that we could do with the data you provided:


I'ts an example, of course, we could change it if we identify that the requirements of the model aren´t fulfilled, but this will suffice for now.
Note that the authors are related only to the organizations, and these organizations have their relationships towards universities, this way even if there's no direct link, the authors are linked to universities.
In order to create this model we would have to generate the proper Cypher queries, and adapt the input of data according to its format, using LOAD CSV, or the APOC Library, etc.
Also, there's no need to store the linking properties, like A['Author', 'org1'], the link is stored as a relationship, Neo4j doesn't require foreign keys, relationships are stored as data, not inferred with joins like in SQL.

So, I hope this helps you, please let me know if this was actually of aid or if I misunderstood your question.

Regards!

Thank you for the explanations. This is awesome. I got the logic. I tried to implement using cypher query using above example. But its not working as per my understanding. I have made some changes in the json file. Please have a look into it:

[
"Authors":
{
givenName:"abc",
hasAffliation:"organization1",
Identifier:"123"
},
{
givenName:"bcd",
hasAffliation:"organization2",
Identifier:"456"
},
{
givenName:"xyz",
hasAffliation:"organization2",
Identifier:"678"
},
{
givenName:"abcd",
hasAffliation:"organization2",
Identifier:"111"
}
],
[
"originatedAt":[
{
identifier:"67701",
id:"organization1",
text:"a university"
}
{
Identifier:"899901",
id:"organization2",
text:"b university"
}
]

Again i want the same result. Please find my code below:

CALL apoc.load.json("file:///D:/abc.json") YIELD value AS data FOREACH (tagName IN data.Authors | MERGE (author:Author {authid:tagName.identifier}) ON CREATE SET author.firstname=tagName.givenName,author.Affiliation=tagName.hasAffiliation FOREACH (org IN data.originatedAt | MERGE (organization:Organization{afid:org.identifier}) ON CREATE SET organization.University=org.text,organization.Affiliation=org.id) ) MATCH (author:Author {Aff: author.hasAffiliation}) , (organization:Organization{Aff:org.id})
Note: "originatedAt" tag refers to organization and "giveName" as Author name

I was successfully able to create the author nodes and organization , that is 4 author nodes and two organization nodes as per json file, but I'm failing in my linking part , is my approach to this problem is correct or not. Author--[r:linking]-->organization is still bottleneck for me. Any clue could be great help for me.

Thank you

Your code seems about right, you could also use UNWIND instead of FOREACH in these cases, but if your creating what you need, that's enough. The only thing is that you aren't actually creating the relationships, you should have something like:

MERGE (author)-[:BELONGS_TO]->(organization)

right after your last MATCH. I used the type of relationship BELONGS_TO and the direction of the relationship is from the author to the organization, feel free to use any other name that best suits your use case. Hope this helps, and I'm sorry to answer until now, it was a bit of a heavy week!