Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
07-01-2020 04:04 PM
I have a node-link model ( which is a graph ) of an electricity grid in Neo4j, so this should fit like a glove.
The nodes and links are objects of their own, which are traversable in an abstract manner.
Link has a starting node (:Link) – [:begint_bij] –> (:Node) and an ending node (:Link) – [:eindigt_bij] –> (:Node)
Every :Node and :Link has references to one or more objects. Links and Nodes are uniquely defined by their references in this model.
The model is generated by the object-oriented GIS model and because of the generating method ( which I have no influence on ) redundant Node Link (pairs) are in the dataset.
In the detail you can see the redundant pairs clearly with a pattern:
A Node-Link pair where the Node has only relationship to ( cables, gisreferences and links ) and the link has only relationships to ( cables, gisreferences and nodes )
For removing the redundant Node-Link pairs I have come up with this query:
call apoc.periodic.iterate (
" MATCH (fld:e_lv_function_cable ) where not exists (fld.done) Return fld",
" match (fld) - [:GenObject] - (fn:Node)
match (fn) - [:begint_bij] - (l1:Link)
with [(fn) - - (obj1) | labels(obj1)] AS conn1, [(l1) - - (obj2) | labels(obj2)] AS conn2, fn, l1, fld
where all( lb1 IN conn1 where lb1[0] in ['e_lv_function_cable','GisReference','Link'] )
and all( lb2 IN conn2 where lb2[0] in ['e_lv_function_cable','GisReference','Node'] )
with l1, fn, fld
match (l1) - [:eindigt_bij] - (n2)
match (l2:Link) - [r] - (fn) - [:begint_bij] - (l1)
Call apoc.do.when (
n2 is not null and l2 is not null and r is not null,
'Call apoc.create.relationship (l2, type(r), {}, n2) YIELD rel Detach Delete l1, fn',
'',
{n2: n2, l2: l2, r: r, l1: l1, fn: fn}) YIELD value
with fld
set fld.done=true
", {batchSize:100, parallel:false}) yield batches, total return batches, total
And it does the job.
However in a few cases I get an error ( in the debug log 😞
Failed to invoke procedure apoc.do.when
: Caused by: org.neo4j.graphdb.NotFoundException: Node is deleted and cannot be used to create a relationship
And sometimes:
Failed to invoke procedure apoc.do.when
: Caused by: java.lang.IllegalStateException: Kernel API returned non-existent relationship type: -1
And it almost makes sense to me.
Can anyone please:
Thx.
07-03-2020 12:14 AM
Hello @marcel.olij and welcome to the Neo4j community
When you load the nodes and relationships, did you use MERGE
?
Regards,
Cobra
07-03-2020 12:51 AM
Cobra,
The redundant nodes are in the input and can't be merged on basis of their ids or rels.
BR,
Marcel
07-05-2020 02:08 PM
07-05-2020 10:54 PM
Cobra,
The nodes and links are themselves unigue in the input, so unique constraints would not do the job.
The software that creates the node link model has a reason to generate these extra node-link pairs ( I don't see what it is though ). They are of no use to me and I want them out for better perfomance and model hygiëne.
If i could use a unique constraint, I still must be able to connect the node-links at import again and that would probably harder than removing these pairs after creation.
BR,
Marcel
BR,
Marcel
All the sessions of the conference are now available online