I'm using Neo4j 3.2 with the Neo4j Spatial plugin 0.24-neo4j-3.1.1 After adding layers, I tried to create node and add it to the index with spatial.addNode
CALL spatial.addPointLayer('geom');
CREATE (n:Node {latitude:60.1,longitude:15.2})
WITH n
CALL spatial.addNode('geom',n) YIELD node
RETURN node;
I also tried create a new one and add to the index later
CREATE (n:Node {latitude:60.1,longitude:55.2});
MATCH (n:Node {latitude:60.1,longitude:55.2})
WITH n
CALL spatial.addNode('geom',n) YIELD node
RETURN node;
Later on when I tried to call
CALL spatial.removeLayer("geom");
The procedure delete all nodes including those created by
CREATE (n:Node {latitude:60.1,longitude:55.2});
Is it a by-design behavior?
If it is, can you suggest any other way to update/delete the indexes without deleting the location node? There is a proposed solution here but it seems hacky and error-prone when it comes to updating the index information when the location node change the lat/long value neo4j-spatial: What is the official way to delete a node from a spatial index?
I have looked into the source code of the neo4j spatial package to see how the addNode and removeLayer procedures implemented.
When you call addNode, the node itself will be directly added into the RTree, which means it will serve as a leaf node in the RTree.
When you call removeLayer, everything in the RTree will be removed from neo4j, including all the nodes and all the edges in this RTree.
Since I have not found one method to remove the RTree information without touching the leaf nodes, I would suggest the following way:
When you want to add a node into the RTree, you can create a new node with the same location (replicate the original node) and create an edge from the original node to this new one. By doing so, when you call removeLayer, only the nodes replicated by you will be deleted. Your original nodes will not be influenced. The cost is you need to spend more storage.
ps. When you want to call removeLayer in this case, you need to delete the edges you create before calling removeLayer. The reason is a node cannot be deleted if there is any edge connected to it.