Skip to content

Indexed nodes created in batch can't be used in set_labels or set_properties #221

cod3monk3y opened this Issue Dec 12, 2013 · 3 comments

3 participants


Original issue on SO:

The objects returned by WriteBatch.get_or_create_indexed_node are not usable in calls to WriteBatch.set_labels or .set_properties, as the objects returned from WriteBatch.create are. The URIs returned for the abstracts are clearly different, and it seems that the set_ methods aren't properly handling the indexed URIs.

I don't know if this is by design (as I'm new to py2neo and Node4j), but it makes updating a unique node in a batch difficult (and for me, impossible). Solutions on SO have pointed to splitting the batch into phases (create phase and property-set phase) or to create the node and then add it to the index (which works only if you know uniqueness a priori).

Here is the test code that fails:

def test_batch_simple():
    graph.get_or_create_index(neo4j.Node, 'node_index')

    batch = neo4j.WriteBatch(graph)
    bob = batch.get_or_create_indexed_node('node_index', 'name', 'Bob', { 'name':'Bob' })
    batch.set_labels(bob, 'Male')

    results = batch.submit()

And the failure

Traceback (most recent call last):
  File "D:\<my path>\test\", line 308, in <module>
  File "D:\<my path>\test\", line 282, in test_batch_simple
    results = batch.submit()
  File "build\\egg\py2neo\", line 2594, in submit
  File "build\\egg\py2neo\", line 2565, in _execute

Perhaps I'm doing this wrong? Is there a more correct way of updating labels/properties on unique indexed nodes within the batch that creates them?

(note: 50 rep bounty offered on the SO question!)



There is a server limitation whereby different types of URIs are returned for some batch methods than for others. The indexing methods are in particular affected by this and return index entry URIs which cannot be used in other calls. This is unfortunately one of the disadvantages of batches.

If you're using Neo4j 2.0 (and I'd recommend you upgrade to py2neo 1.6.1) then have a look at the new labels and schema indexing mechanisms. These can be used from Cypher and for more complex interactions, that's probably the best way to go.


In Neo4j 2.0, you can do the following using py2neo 1.6.1

def test_batch_simple():
    batch = neo4j.WriteBatch(graph)
    batch.append_cypher(neo4j.CypherQuery(graph, "MERGE (alice:Person {name:'alice'} RETURN alice") )
    batch.append_cypher(neo4j.CypherQuery(graph, "MERGE (bob:Person {name:'bob'} RETURN bob") )

    return batch.submit()

The server limitation mentioned by @nigelsmall still stands, you cannot use the BatchRequest returned from the cypher query in subsequent requests of the same batch transaction. Properties can be set in the same cypher request using MERGE's ON CREATE and ON MATCH.

Are there plans to implement the new MERGE features, as Issue #213 is requesting?


Closing as original issue is a reflection of a server limitation. Please continue subsequent MERGE discussion in issue #213.

@nigelsmall nigelsmall closed this Dec 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.