neo4j-apoc-procedures icon indicating copy to clipboard operation
neo4j-apoc-procedures copied to clipboard

apoc.merge.node just for Match-only

Open zirkelc opened this issue 2 years ago • 0 comments

Feature description (Mandatory)

apoc.merge.node apoc.merge.relationship

apoc.merge.node allows to dynamically MERGE a node with labels and properties that can be provided as parameters instead of building a static Cyper query beforehand. This is useful in cases where we unwind on multiple rows and the MERGE clause would be different for each row:

UNWIND $rows AS row
CALL apoc.merge.node(row.labels, row.match, row.onCreateProps, row.onMatchProps) YIELD node
RETURN collect(id(node)) as nodeIds

In the same way, we can use apoc.merge.relationship to dynamically MERGE a relationship with type and properties provided as parameters. However, we must also provide the start and end node of the relationship. Both nodes can be matched with apoc.merge.node without providing properties to be merge (last two params):

UNWIND $rows AS row
CALL apoc.merge.node(row.start.labels, row.start.match) YIELD node as startNode
CALL apoc.merge.node(row.end.labels, row.end.match) YIELD node as endNode
CALL apoc.merge.relationship(startNode, row.relType, row.match, row.onCreateProps, endNode, row.onMatchProps) YIELD rel
RETURN collect(id(rel)) as relationshipIds

In the second example, I'm using apoc.merge.node to MATCH the start and end nodes without actually creating or changing them. My assumption is though, that Neo4j still write-locks these nodes which affects the performance and would hinder optimisations such as parallelisation via apoc.periodic.iterate due to deadlocks on the same node.

I would like to suggest a new procedure apoc.match.node or apoc.nodes.match to dynamically MATCH nodes. The implementation would be fairly easy as we could copy apoc.merge.node and change the Cypher query in line 77 from MERGE to MATCH: https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/c56b754bf41d16a78ace806e42e293bdb7d13e13/core/src/main/java/apoc/merge/Merge.java#L67-L79

Considered alternatives

The default values of apoc.merge.node for onCreateProps and onMatchProps is {}. This means no change to the properties on CREATE or MATCH. Therefore, we could alternatively add condition in Java to check if properties have been provided. If not, we could switch the Cypher query from MERGE to MATCH in this case.


If you find this request useful and would be wiling to add it to APOC, then I would go ahead and submit a PR for this! Let me know what you think! :-)

zirkelc avatar Mar 31 '22 14:03 zirkelc