0

I'm using the neo4j batch operations endpoint. I want to index properties to an existing node if they have not been indexed before, ie. I'm using this endpoint, with the exception of using ?uniqueness=get_or_create flag.

It works, when when I'm issuing two such batch requests in a row for two different nodes, the second one never gets indexed!

Here is the payload for the the two batch requests:

FIRST ONE:
[
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'registeredInShop',
            value: '52a5f4e19e3fc8406a000006'
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'idInShop',
            value: '1'
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'email',
            value: 'me@shop.com'
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'createdOn',
            value: 1386607841880
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'attributes_isSpam',
            value: false
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'attributes_isHardBounced',
            value: false
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'attributes_isSubscribedAlerts',
            value: true
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'attributes_isSubscribed',
            value: true
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'attributes_isCustomer',
            value: false
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2388',
            key: 'id',
            value: '52a5f4e19e3fc8406a000008'
        }
    }
]





// SECOND ONE


[
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: 'http: //localhost: 7474/db/data/node/2390',
            key: 'registeredInShop',
            value: '52a5f4e19e3fc8406a000006'
        }
    },
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'email',
                value: 'me2@shop.com'
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'createdOn',
                value: 1386607842460
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'attributes_isSpam',
                value: false
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'attributes_isHardBounced',
                value: false
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'attributes_isSubscribedAlerts',
                value: true
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'attributes_isSubscribed',
                value: true
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'attributes_isCustomer',
                value: false
            }
        },
        {
            method: 'POST',
            to: '/index/node/users?uniqueness=get_or_create',
            body: {
                uri: 'http: //localhost: 7474/db/data/node/2390',
                key: 'id',
                value: '52a5f4e29e3fc8406a000016'
            }
        }
    ]

Any ideea as to why this is happening? If I remove uniqueness=get_or_create it works, but it's exactly what I want to achieve!

alexandru.topliceanu
  • 2,364
  • 2
  • 27
  • 38

1 Answers1

1

See: http://docs.neo4j.org/chunked/stable/rest-api-batch-ops.html#rest-api-refer-to-items-created-earlier-in-the-same-batch-job and scroll down to the example in the payload that indexes a relationship.

As far as your REST API request payloads go, it looks like you're missing the id property in your JSON. Also, you do not need to explicitly set the uri to the full URI of your Neo4j instance, just use the relative path to your nodes.

Your request should look like this:

[
    {
        method: 'POST',
        to: '/index/node/users?uniqueness=get_or_create',
        body: {
            uri: '/node/2388',
            key: 'registeredInShop',
            value: '52a5f4e19e3fc8406a000006'
        },
        id: 2388
    },
    {
        method: 'POST',
        to: '/index/node/users',
        body: {
            uri: '/node/2388',
            key: 'idInShop',
            value: '1'
        },
        id: 2388
    }
]

That should resolve your issues.

It's worth noting that this will not create the user index. You will need to ensure your index is created in advance to running these batch operations. Also, you'll need to update the index using this same process if your property on the node changes. Let me know in the comments whether or not this resolves your issue.

Cheers,

Kenny

Kenny Bastani
  • 3,268
  • 15
  • 20
  • AFAIK the `id` property is used to refer to the result of a previous operation in the same batch. I don't need this, I'm referring to an existing node, whose id/URL I already know and I want add it's properties to an existing index. I think you're confusing the node id with the batch operations {[JOB ID]} feature which **are not the same**! – alexandru.topliceanu Dec 11 '13 at 09:19
  • Thanks for pointing this out. How many items do you have in an average batch? I'm wondering if it makes more sense to upgrade to 2.0 and then use Cypher. Also, Michael Hunger may be able to help more with your current issue. – Kenny Bastani Dec 11 '13 at 20:27
  • It varies, anywhere between 5 to 50 operationsn in a batch. – alexandru.topliceanu Dec 12 '13 at 10:39