neo4j批处理操作get_or_create标志覆盖具有不同id的现有节点

时间:2013-12-09 16:52:58

标签: neo4j batch-updates

我正在使用neo4j批处理操作端点。 我想将属性索引到现有节点,如果它们之前没有被索引,即。我正在使用此endpoint,但使用?uniqueness=get_or_create标志除外。

当我为两个不同的节点连续发出两个这样的批量请求时,第二个请求永远不会被索引

以下是两个批处理请求的有效负载:

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'
            }
        }
    ]

关于为什么会发生这种情况的任何想法?如果我删除uniqueness=get_or_create它可以正常工作,但这正是我想要实现的目标!

1 个答案:

答案 0 :(得分:1)

请参阅:http://docs.neo4j.org/chunked/stable/rest-api-batch-ops.html#rest-api-refer-to-items-created-earlier-in-the-same-batch-job并向下滚动到索引关系的有效内容中的示例。

就您的REST API请求有效负载而言,您似乎错过了JSON中的id属性。此外,您无需将uri显式设置为Neo4j实例的完整URI,只需使用节点的相对路径即可。

您的请求应如下所示:

[
    {
        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
    }
]

这应解决您的问题。

值得注意的是,这不会创建user索引。您需要确保在运行这些批处理操作之前创建索引。此外,如果节点上的属性发生更改,则需要使用相同的过程更新索引。请在评论中告诉我这是否可以解决您的问题。

干杯,

肯尼