Unable to load NODE with id - neo4j

I recently upgraded my Neo4j database to v. 3 (3.0.6). Since then I am having trouble when trying to delete nodes with Cypher. This is a new problem since the upgrade. They Cypher query is:
MATCH (p) WHERE id(p) = 83624
OPTIONAL MATCH (p)-[r]-(n)
OPTIONAL MATCH (p)-[r2]-(n2)
WHERE NOT ('Entity' in labels(n2))
DELETE r, r2, p, n2
This now results in the error Unable to load NODE with id 83624
The exact query with RETURN instead of DELETE returns the node. I've also tried swapping out DELETE with DETACH DELETE of the nodes, but that gives me the same error.
It looks like this question has been asked before but without a solution. Any idea what is causing this error?

I'm a little confused by this query. Both of your optional matches are identical, in that they will match to any relationship to any node connected to p.
Let me make sure I understand what you're trying to do: Find a node by ID, then delete that node along with any connected node that is not an Entity (as well as the relationships connecting them). Is that right?
If so, this query might work better:
MATCH (p) WHERE id(p) = 83624
OPTIONAL MATCH (p)--(n)
WHERE NOT n:Entity
DETACH DELETE n
DETACH DELETE p
Detaching a node deletes all relationships connected to that node, and you can only delete a node that has zero relationships.
Also, just to note, it's not a good idea to use the internal ids for uniquely identifying nodes. It's recommended to use your own unique IDs instead, and create unique constraints on that property for that label.

Related

Neo4j node/relation inconsistencies

When fetching or when I try to delete a specifc node like
MATCH (p)
where ID(p)=79259223
OPTIONAL MATCH (p)-[r]-()
//drops p's relations
DELETE r,p
I get the following error
While loading relationships for Node[79259223] a Relationship[87331456] was encountered that had startNode: 80312215 and endNode: 83719851, i.e. which had neither start nor end node as the node we're loading relationships for
I also run the ConsistencyChecker what resulted in a big list of inconsistencys. However how can you fix these inconsistencys? I can not delete the nodes for instance
Here is a possible way to "fix" an occurrence of this error. Unfortunately, it is a fairly manual approach that has to be used for every node that encounters the same problem.
Before you delete the node, you can try to delete the inconsistent relationship by its native neo4j ID. For example:
MATCH ()-[r]->()
WHERE ID(r) = 87331456
DELETE r;
NOTE: Before deleting that relationship, you should first try to take a look at it (e.g., replace DELETE WITH RETURN) to understand what you are planning to delete. You may want to do something else first or instead.
If that deletion works, then try to delete the node again, as follows:
MATCH (p)
WHERE ID(p) = 79259223
DETACH DELETE p;
Notice that I use the DETACH DELETE syntax, which will attempt to delete all the relationships for the specified node.

Neo4j cypher: Deleting a collection of relationships

I think this may be a bug, however, when deleting a collection of relationships the start node is also deleted. I am running neo4j community edition 2.3.3 and have run the following queries in order.
match (u:User)-[]->(p:Pie)-[c:CONSISTS_OF*1]->() where id(u) = 6398 and id(p)= 6436 return p,c Which returns Displaying 3 nodes, 2 relationships. and the graph in the image
match (u:User)-[]->(p:Pie)-[c:CONSISTS_OF*1]->() where id(u) = 6398 and id(p)= 6436 FOREACH( q in c | DELETE q) Which results in Deleted 2 relationships, statement executed in 103 ms.
And then what I am finding is instead of the collection of relationships c being deleted, the node p is also deleted…
match (u:User)-[]->(p:Pie)-[c:CONSISTS_OF*1]->() where id(u) = 6398 and id(p)= 6436 return p Which results in (no rows)
I would simply like to delete the collection of relationships. I also feel like this feature is something that makes sense but hasn’t been revisited since late 2013… https://github.com/neo4j/neo4j/issues/1612
EDIT: In an earlier version of the question I had DETACH DELETE instead of DELETE. This was a mistake in the posting of the question. I have since revised it. It should be noted that the reason I think this may be a bug is because both DETACH DELETE and DELETE behave the same in this scenario.
In the MATCH query that you use to check whether the (p:Pie) node exists after deleting the relationships you still include the -[c:CONSISTS_OF*1]-> part of the pattern. That part doesn't match anything, because you deleted those relationships. But then the whole pattern won't match anything either, whether or not the node still exists.
You can use MATCH (p:Pie) WHERE id(p) = 6436 to test that the (p:Pie) node still exists.

How to delete and merge outgoing relationships on the same query

I am using cypher. I am trying to delete all out going relationships before creating new ones on the same query.
i have weird situation if the relations/nodes already existed it's working as expected. if They never been created before I get:
(no changes, no rows)
This is my query:
match (user{userId:'a'})-[r:nearby_wifi]->() delete r
MERGE (p1:BT{userId:'a'}) WITH p1, [{bssid:"0a:18:d6:c1:3d:fd",level:"-51",timestamp:"1973-08-27 02:26:35.423",venueName:""},{bssid:"04:18:d6:c2:3e:2a",level:"-55",timestamp:"1973-08-27 02:26:35.425",venueName:""},{bssid:"0e:18:d6:c1:3d:fd",level:"-53",timestamp:"1973-08-25 11:06:07.392",venueName:""}] AS wifis
UNWIND wifis AS wifi
MERGE (p2:WIFI{bssid: wifi.bssid})
MERGE (p1)-[r1:nearby_wifi]->(p2)
SET r1.dist=wifi.dist
SET p1.lastTimeActive=1460378030215
SET p2.level=wifi.level
SET p2.timestamp=wifi.timestamp
SET p2.venueName=wifi.venueName
Any idea why when combining delete and the merge executions I got no changes(when graph empty)?
Thanks.
Replace first match with optional match
For example if you have no client nodes in your database, but have some person nodes query
Match (p:Client) with p Match (r:Person) return *
will get nothing, but query
Optional Match (p:Client) with p Match (r:Person) return *
will give you Persons. I think neo4j optimizer stops executing query after it gets no results and with optional match it gets null, and continues executing.

How to delete nodes in reverse order in neo4j

I have a database with nodes like
(u : Update)-[:HAS_COMMENT]->(latest_comment:Comment)-[:NEXT]->(c1: Comment)->(c2: Comment)
And so on.. Each Comment node has a relation with User node
(c : Comment)<-[:HAS_COMMENTED]-(u : User).
Now I have to delete the update node so with this all the comment nodes should be deleted and relation between User and Comment should also be deleted.
The solution that came to my mind is to traverse from last Comment node and start deleting relation with User node and delete Comment node and "NEXT" relation with the previous comment node. I am facing the problem to write such query.
Can someone help me with this?
Use a variable length path in an OPTIONAL MATCH statement to match on all Comments in the path (chained together with :NEXT relationships) then use DETACH DELETE to delete the nodes and relationships:
MATCH (u:Update {name: "UpdateToDelete"})-[:HAS_COMMENT]->(c:Comment)
OPTIONAL MATCH (c)-[:NEXT*]->(r:Comment)
DETACH DELETE u,c,r
DETACH DELETE will remove nodes and any relationships connected to the nodes being deleted.
Here is a Neo4j console to test.
Edit
The DETACH DELETE statement was added in Neo4j version 2.3. To accomplish this without using DETACH DELETE try this query:
MATCH (u:Update {name: "UpdateToDelete"})-[hc:HAS_COMMENT]->(c:Comment)
MATCH (c)-[n:NEXT*0..]->(r:Comment), (r)<-[h:HAS_COMMENTED]-(:User)
FOREACH (x IN n | DELETE x)
DELETE r,u,hc,c,h
The key difference is that without using DETACH DELETE we have to match on each relationship that connects a given node and delete those relationships when we delete the node. Since we have a variable length path (Comments connected by an arbitrary number of NEXT relationships) we can use the FOREACH function to iterate through the collection of NEXT relationships and delete them as we delete the Comment node(s).

Neo4J Cypher Match by multiple relationship types - strange behavior

My graph looks like this
medium-[:firstChapter]->chapter1-[:nextChapter]->chapter2_to_N
there is only one node connected via :firstChapter and then several nodes may follow, connected via :nextChapter
I tried to match all nodes that are either connected via relationship :firstChapter to medium or connected via :nextChapter from one chapter to another
The query I tried looks like this
start n=node(543) match n-[:firstChapter|nextChapter*]->m return m;
node(543) is the node medium.
Surprisingly, this query returns all nodes in the path, even though the nodes are not connected to n (=medium node)
If I leave out the * sign after nextChapter, only the first node with the :firstChapter relationship is returned (chapter1), which seems to be correct.
start n=node(543) match n-[:firstChapter|nextChapter*]->m return m;
Why does the query above return nodes not connected to n? As far as I understand it, the * sign usually returns nodes that are an unlimited number of relationships away, right?
What is the best way to match all nodes of a path (only once) that are either connected via :firstChapter or :nextChapter to a start node? In this case all chapters
The query above serves that purpose, but I don't think the output is correct...
EDIT:
Added a diagramm to clarify.
As you can see, the first chapter may only be reached via :firstChapter,
So it is still unclear, why the query above returns ALL chapter nodes
Try doing match p=n-[:firstChapter|nextChapter*]->m to see what p is. Hopefully that provides some insight about how they are connected.
What you might be looking for in the end is:
start n=node(543)
match n-[:firstChapter|nextChapter*]->m
return collect(distinct m);
To get a collection of distinct chapter nodes that follow n.
update
Here's another one--didn't actually test it but it might get you closer to what you want:
start n=node(543)
match n-[:firstChapter]->f-[:nextChapter*]-m
return f + collect(distinct m);
Using the * operator, the query looks for all relationships along the line for both relationship types, :firstChapter and :nextChapter (not just :nextChapter). Your chapter data for node(543) likely contains some relationships to chapter nodes not in the 543 chain and the query is returning them.
Consider adding an extra relationship using type :nextChapter to connect the start node to the first chapter, and check the relationships that exist on your chapters.
Then run:
start n=node(543)
match n-[:nextChapter*]->m
return m;
and see if you still get extra results. If so, you could run the following, bumping up n each time until you find the node that has the extra relationship(s) - (though I'm sure there are other ways!)
start n=node(543)
match n-[:nextChapter*1..n]->m
return m;

Resources