pythonneo4jcypherneo4j-apoc

Neo4j: how to pass parameters to relationships?


The official website of neo4j point out that we can not pass parameters to the type of relationships. However, I need to pass parameters to relationships when adding embeddings in batch.

Neo4j adds batch vectors for nodes by

    driver.execute_query('''
    UNWIND $movies as movie
    MATCH (m:Movie {title: movie.title, plot: movie.plot})
    CALL db.create.setNodeVectorProperty(m, 'embedding', movie.embedding)
    ''', movies=nodes_with_embeddings, database_=DB_NAME)

add embedding property for nodes

This code use $movies to pass parameters and pass movie.title as property value. Each movie is a dict. However, I can not pass relationship types with $datas like MATCH (h:Entity {id: data.id})-[r: data.r]->(t:Entity {id: data.id})

I've tried to use common concatenation for strings to pass parameters, and add embedding property one by one. For example, "MATCH (h:Entity {id: data.h_id})-[r:"+data['r']+"]->(t:Entity {id: data.t_id}) However, I want to use batch operation to speed up.

Is there a way to pass parameters to the relationships for batch operation? Thanks for your time.


Solution

  • You can use the APOC procedure apoc.create.relationship to create relationships with dynamic types. For example:

    UNWIND $data AS d
    MATCH (h:Entity {id: d.h_id})
    MATCH (t:Entity {id: d.t_id})
    CALL apoc.create.relationship(h, d.r, NULL, t) YIELD rel
    RETURN rel