Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-19-2022 10:56 PM
I wrote a python code which will extract meaningful relation from a given text as an input.
Please check the below image for better understanding
The above image show that, I extracted the relation from the text
Now i want to move those relation object to neo4j and form a graph?
how do i do that? what is the query for that?
Solved! Go to Solution.
12-21-2022 07:22 AM
The json file consists of a single json object with key 'relations', whose value is an array. As such, @Estelle query need to be modified to account for that. Try the following:
CALL apoc.load.json("file:///sample.json") YIELD value
UNWIND value.relations as relation
MERGE (head:Entity {name: relation.head})
MERGE (tail:Entity {name: relation.tail})
WITH head, relation, tail, toUpper(replace(relation.type, " ", "_")) as type
CALL apoc.create.relationship(head, type, {}, tail) YIELD rel
RETURN count(rel)
I replaced the relationship values to conform to best practices. Also, the 'meta' value is a json map, which can not be saved as a relationship property. Is there data in 'meta' that you want to save?
12-26-2022 07:45 AM
Try this:
MERGE(main:Entity{name:'main'})
WITH main
CALL apoc.load.json("file:///sample.json") YIELD value
UNWIND value.relations as relation
MERGE (head:Entity {name: relation.head})
MERGE (tail:Entity {name: relation.tail})
MERGE (head)-[:HAS_MAIN]->(main)
WITH head, relation, tail, toUpper(replace(relation.type, " ", "_")) as type
CALL apoc.create.relationship(head, type, {}, tail) YIELD rel
RETURN count(rel)
There are only 15 relationships incoming to the main node. This is because your data only has 15 distinct head nodes.
12-20-2022 12:40 AM
Hello,
You can save your result as JSON and import it with APOC: apoc.load.json (https://neo4j.com/labs/apoc/4.4/overview/apoc.load/apoc.load.json/)
Not tested, but query could look like this:
CALL apoc.load.json("path/to/file.json") YIELD value
MERGE (head:Entity {name: value.head})
MERGE (tail:Entity {name: value.tail})
CALL apoc.create.relationship(head, value.type, value.meta, tail) YIELD rel
RETURN count(rel)
Explantions:
12-20-2022 03:04 AM
12-21-2022 07:00 AM
Its because you have an extra 'with' clause at the end of the first line. You need to put the 'with' between the 'merge' and 'call' on line 3.
WITH head, value, tail
12-20-2022 01:40 AM - edited 12-20-2022 02:05 AM
hi @Estelle , thanks for the response.
Well, Instead of converting the object to JSON
Is it possible to do this via python object?
Pls note that: I already connected python code and neo4j by this command:
from neo4j import GraphDatabase
data_base_connection1 = GraphDatabase.driver(uri = "bolt://localhost:7687", auth=("neo4j", "password"))
session1 = data_base_connection1.session()
In my python program i have a class called KB and inside that class i have a method called def print(self): and some logic inside that..
So whenever i type KB.print()
i get relation object [ the one that i shown in above image]
so what would be the query to send that relation object from python to neo4j and form a graph?
12-20-2022 03:05 AM
Sure you can also do that. In your KB class you can probably have a method returning dictionnaries, then the dict keys are used as variables in the query you execute with the driver, something like:
query = """
MERGE (head:Entity {name: $head})
MERGE (tail:Entity {name: $tail})
CALL apoc.create.relationship(head, $type, $meta, tail) YIELD rel
RETURN count(rel)
"""
params = {
"head": "",
"type": "",
"tail": "",
"meta": {}
}
with driver.session() as s:
s.run(query, params)
12-20-2022 08:33 PM - edited 12-20-2022 08:53 PM
@Estelle hi there, the query seems to be showing many errors like...
in-between merge and call 'with' should come.
could you please test the query and let me know
12-20-2022 11:59 PM
Hi @Jupyter123 ,
The error message seems quite explicit, what have you tried to fix the query and what is your problem?
12-21-2022 02:50 AM
hey @Estelle
I have uploaded JSON file in the below link. Please have a look
I want that JSON data to be displayed in neo4j graph DB with nodes and edges
What is the query for that?
Could you try to test that JSON file in neo4j and let me know if it's displaying in neo4j as graph?
I want the result to be like this
12-21-2022 07:22 AM
The json file consists of a single json object with key 'relations', whose value is an array. As such, @Estelle query need to be modified to account for that. Try the following:
CALL apoc.load.json("file:///sample.json") YIELD value
UNWIND value.relations as relation
MERGE (head:Entity {name: relation.head})
MERGE (tail:Entity {name: relation.tail})
WITH head, relation, tail, toUpper(replace(relation.type, " ", "_")) as type
CALL apoc.create.relationship(head, type, {}, tail) YIELD rel
RETURN count(rel)
I replaced the relationship values to conform to best practices. Also, the 'meta' value is a json map, which can not be saved as a relationship property. Is there data in 'meta' that you want to save?
12-21-2022 07:30 AM - edited 12-21-2022 07:31 AM
hi there @glilienfield @Estelle
i dont want the meta value
i just want the 'head' node, 'tail' node and 'type' relation and that should form a graph.
by the way, i executed your code and i get like this.
please see below image
still i haven't got any graph though.
12-21-2022 07:34 AM
You did not attach your image. This is what I got:
12-21-2022 07:36 AM
i edited it, pls check the image and it's nice you have got my expected graph.
Could you check what's the error i did tho?
12-21-2022 07:45 AM
Interesting. I got 23 relationships. What does the output look like when you execute:
CALL apoc.load.json("file:///sample.json") YIELD value
return value
12-21-2022 07:51 AM
12-21-2022 08:54 AM
Ok, that structure is different from what you posted. That data looks to be an array. It is not a value of the 'relation' key, as was in the file you posted. That would explain the issue. Can you post the final json file? I can modify the query.
12-21-2022 07:50 PM - edited 12-21-2022 07:54 PM
hi @glilienfield , morning.
oh yeah i just checked, i used different wrong json structure.
let me use the one which i sent you and will update you
12-26-2022 02:25 AM - edited 12-26-2022 02:33 AM
hey @glilienfield
just a small help
can you modify the same query in such a way where there should be a top node among all these nodes and that top node should connect every head node.
top node should be named as 'main' and that 'main' top node should relate with every other head node .
pls note that: top node is not in json file.
json file has head node, tail node and type(relation)
i want to create a single top node and that top node should match all head nodes..
what's the query for that?
something like this
12-26-2022 04:40 AM
Is this a main node for each json imported, if so, how will you differentiate each main node? Will you use a unique ‘id’ or ‘name’ property?
12-26-2022 04:44 AM
@glilienfield hi there
There is only one json file and not more than one json.
You can use the existing json itself, and want to create a main node ( top node) named as main. And that top node should connect with each head node in that json file.
I hope it's clear
12-26-2022 07:45 AM
Try this:
MERGE(main:Entity{name:'main'})
WITH main
CALL apoc.load.json("file:///sample.json") YIELD value
UNWIND value.relations as relation
MERGE (head:Entity {name: relation.head})
MERGE (tail:Entity {name: relation.tail})
MERGE (head)-[:HAS_MAIN]->(main)
WITH head, relation, tail, toUpper(replace(relation.type, " ", "_")) as type
CALL apoc.create.relationship(head, type, {}, tail) YIELD rel
RETURN count(rel)
There are only 15 relationships incoming to the main node. This is because your data only has 15 distinct head nodes.
12-26-2022 08:08 AM
cheers @Estelle as expected. i really appreciate your time and patience for helping me out.
+1 kudos to you..
12-21-2022 07:42 AM
@glilienfield i saw your graph, it's close to my expected output.
just one quick change, instead of displaying node as numbers, can you display as corresponding to head and tail name in json?
something like this
12-21-2022 08:03 PM
hey mate, its working now
my bad , earlier i had wrong JSON file.
Now i updated json and it works.
thank you very much
All the sessions of the conference are now available online