Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-20-2021 01:47 PM
Hi, this was likely addressed before but I am new to graph databases so the answers confuse me.
I am extracting relations from text and want to store the extracted network in neo4j while keeping a pointer back to the text from which the relation came. I have Sentence
nodes, as well as Entity
nodes. Ideally, I would have relations between Entity
nodes, and each relation would point to the Sentence
whence it came from - but this is not possible in neo4j.
I read somewhere that I could make additional Relation
nodes, so something like:
MATCH (s: Sentence {sentence: "some sentence"})
CREATE (e1:Entity {entity: "something"})
CREATE (e2: Entity {entity: "something_else"})
CREATE (r:Relation {id: xxx})
CREATE (e1) - [:RELATION_FROM] -> (r)
CREATE (r) - [:RELATION_TO] -> (e2)
CREATE (r) - [:APPEARS_IN] -> (s)
But I'm not really convinced - it relies on arbitrary IDs for relations, and also makes visualizations much less intuitive (the entire idea of using a graph database was that it would represent the network of entities natively).
Is there some better way? I also thought of just giving IDs to sentences and storing the IDs as relation attributes (so:
CREATE (e1:Entity {entity: "something"})
CREATE (e2:Entity {entity: "something_else"})
CREATE (e1) - [r:RELATION {sentence_id: xxx}] -> (e2)
But I'm not sure if it's efficient or even possible to then use this to figure out which relation appears in which sentence?
Any help appreciated.
10-20-2021 02:57 PM
Your :Relation node doesn't need to have an id if it's not useful to you. Sometimes nodes in a graph are just there for grouping purposes, so you could use them for example to traverse to pairs of related entities given a sentence.
What should drive your model are the requirements for the queries that will use it. What are you expecting as your input, and what are the desired outputs? If you want, for a sentence input, to get the pairs of related entities, then the proposed model with :Relationship nodes is a good one.
10-21-2021 03:47 AM
Hi Andrew, thank you for your answer.
Your :Relation node doesn't need to have an id if it's not useful to you
You're right. My script initially used MERGE instead of CREATE for adding relations, and that meant that without an ID, all relations would become a single node - but I see now that's not necessary
The main reason I dislike this approach is that it really breaks the intuitive mapping between a relation between entities and a relation in the graph - but maybe after getting a bit more used to Cypher and graph database that won't be an issue.
What are you expecting as your input, and what are the desired outputs? If you want, for a sentence input, to get the pairs of related entities, then the proposed model with :Relationship nodes is a good one.
No, 99% of the queries will start from entities with the objective to find related entities and analyze the structure of the entity network (which is why I don't really like the approach I suggested, it doesn't allow to directly analyze the network of entities). Being able to link relations back to sentences is more of an occasional thing - mostly to be able to check that a relation was extracted correctly.
All the sessions of the conference are now available online