Is it possible to combine using data-driven node or relationship shortcuts?

I am working with prepared statements using the Neo4J JDBC driver and you need to create shortcuts for node and relationships whose names are driven by the data we will receive.

For example, I would like to create a prepared statement on the following lines:

MERGE (test:{1} {id: {2}) ON CREATE SET test.id = {2}

OR

MERGE (test:Test)-[:{1}]->(test2:Test)

They do not currently work as it seems that Neo4J does not interpret the placeholder {1}

as a placeholder, instead treating it as an invalid placeholder name.

Another possibility I'm looking into is that we can extend Cypher with a stored procedure, although I suspect we might run into the same limitation.

Hoping that someone can provide some insight as to if there is a way to accomplish this with Cypher.

Thank!

UPDATE:

Below is an example using the APOC procedure apoc.create.node

, but I need to merge a dynamic label. Updated title to reflect this.

+3


source to share


3 answers


I ended up using a different procedure from APOC - apoc.cypher.doIt

as it turns out there is no way for APOC to merge with dynamic shortcuts. See Feature Request: https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/271

Below I finished doing. Note that the requirement was to iterate (using UNWIND in this case) over the build and merge nodes with dynamic shortcuts pulled from that collection, and then merge the relationship between the pre-existing node and this new node:



WITH myNode, myList
UNWIND categories AS catArray
WITH myNode, 'MERGE (cat:' + catArray[0] + ' {value: "' + catArray[1] + '" }) ON CREATE SET cat.value = \"' + catArray[1] + '\" RETURN cat' AS cypher
CALL apoc.cypher.doIt(cypher, {}) YIELD value
WITH myNode, value.cat as cat
MERGE (myNode)-[:IN_CATEGORY]->(cat)

      

+2


source


You can use APOC to create dynamic relationships. Similar APOC procedures exist for creating dynamically labeled nodes or adding dynamic labels to nodes.



MERGE (test:Test {name: 'Test'})
WITH test
MERGE (test2:Test {name: 'Test 2'})
WITH test, test2
CALL apoc.create.relationship(test, {new_rel_type}, {}, test2) YIELD rel
RETURN test, test2, rel

      

+1


source


After trying for almost half a day, I finally find out this method:

UNWIND {batch} as row MERGE (n { id: row.id }) SET n += row.properties WITH n CALL apoc.create.addLabels(id(n), [n.label]) YIELD node RETURN node

and the performance is almost the same as pure MERGE.

Thanks a lot for this: SET label: pass label name as parameter

0


source







All Articles