Question

I would like to create relationships with properties using batch insertion, so I tried the following example from the py2neo documenation:

batch = neo4j.WriteBatch(graph_db)
batch.create(node(name="Alice"))
batch.create(node(name="Bob"))
#batch.create(rel(0, "KNOWS", 1, since=2006, reason="unknown")) # This would work
property_dictionary = {"since": 2006, "reason": "unknown"}
batch.create(rel(0, "KNOWS", 1, property_dictionary)) # Does NOT work
batch.submit()

Why isn't it possible to specify a dictionary to assign the properties to the relationship? How can I batch-create a relationship with properties where the properties are stored in SOME_PROPERTIES_VARIABLE, e.g. something like that:

batch.create(rel(0, "KNOWS", 1, SOME_PROPERTIES_VARIABLE))

How would SOME_PROPERTIES_VARIABLE have to look like?

For the batch function get_or_create_indexed_relationship I could simply use a dictionary for the properties, e.g.:

batch.get_or_create_indexed_relationship(my_rel_index, my_rel_key, my_rel_value, 0, "KNOWS", 1, properties=property_dictionary)

This is exactly what I am looking for, except that I don't want to index the relationship.

I'm using Neo4j 1.9.2 and py2neo 1.5.1 - Thank you!

Additional question: Is a batch after a batch.submit() empty or do I additionally have to execute batch.clear() ?

Was it helpful?

Solution

You can use the ** syntax to auto-expand the dictionary, e.g.:

batch.create(rel(0, "KNOWS", 1, **property_dictionary))

Alternatively, specify the relationship type as a tuple, e.g.:

batch.create(rel(0, ("KNOWS", property_dictionary), 1))

For your additional question, the batch is automatically emptied after a successful submission. However, this changes in version 1.6 and will have to be done manually.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top