9

我正在尝试找到解决以下问题的方法。我在这个SO question中看到了它的准描述,但没有真正回答。

以下代码失败,从一个新的图表开始:

from py2neo import neo4j

def add_test_nodes():
    # Add a test node manually
    alice = g.get_or_create_indexed_node("Users", "user_id", 12345, {"user_id":12345})

def do_batch(graph):
    # Begin batch write transaction
    batch = neo4j.WriteBatch(graph)

    # get some updated node properties to add
    new_node_data = {"user_id":12345, "name": "Alice"}

    # batch requests
    a = batch.get_or_create_in_index(neo4j.Node, "Users", "user_id", 12345, {})
    batch.set_properties(a, new_node_data)  #<-- I'm the problem

    # execute batch requests and clear
    batch.run()
    batch.clear()

if __name__ == '__main__':
    # Initialize Graph DB service and create a Users node index
    g = neo4j.GraphDatabaseService()
    users_idx = g.get_or_create_index(neo4j.Node, "Users")

    # run the test functions
    add_test_nodes()
    alice = g.get_or_create_indexed_node("Users", "user_id", 12345)
    print alice

    do_batch(g)

    # get alice back and assert additional properties were added
    alice = g.get_or_create_indexed_node("Users", "user_id", 12345)
    assert "name" in alice

简而言之,我希望在一批事务中更新现有的索引节点属性。该batch.set_properties行发生故障,这是因为BatchRequest上一行返回的对象未被解释为有效节点。虽然不完全相同,但感觉就像我正在尝试类似此处发布的答案

一些细节

>>> import py2neo
>>> py2neo.__version__
'1.6.0'
>>> g = py2neo.neo4j.GraphDatabaseService()
>>> g.neo4j_version
(2, 0, 0, u'M06') 

更新

如果我将问题分成不同的批次,那么它可以正常运行:

def do_batch(graph):
    # Begin batch write transaction
    batch = neo4j.WriteBatch(graph)

    # get some updated node properties to add
    new_node_data = {"user_id":12345, "name": "Alice"}

    # batch request 1
    batch.get_or_create_in_index(neo4j.Node, "Users", "user_id", 12345, {})

    # execute batch request and clear
    alice = batch.submit()
    batch.clear()

    # batch request 2
    batch.set_properties(a, new_node_data)

    # execute batch request and clear
    batch.run()
    batch.clear()

这也适用于许多节点。虽然我不喜欢拆分批次的想法,但这可能是目前唯一的方法。有人对此有什么意见吗?

4

2 回答 2

5

在阅读了 Neo4j 2.0.0-M06 的所有新特性之后,似乎节点和关系索引的旧工作流程正在被取代。目前,neo 在编制索引的方式上存在一些分歧。即,标签模式索引

标签

标签可以任意附加到节点上,并且可以作为索引的参考。

索引

可以通过引用标签(此处为User)和节点属性键(screen_name)在 Cypher 中创建索引:

CREATE INDEX ON :User(screen_name)

暗号MERGE

此外,索引get_or_create方法现在可以通过新的 cypherMERGE函数实现,该函数非常简洁地结合了标签及其索引:

MERGE (me:User{screen_name:"SunPowered"}) RETURN me

py2neo可以通过将 CypherQuery 实例附加到批处理对象来批处理排序查询:

from py2neo import neo4j

graph_db = neo4j.GraphDatabaseService()
cypher_merge_user = neo4j.CypherQuery(graph_db, 
    "MERGE (user:User {screen_name:{name}}) RETURN user")

def get_or_create_user(screen_name):
    """Return the user if exists, create one if not"""
    return cypher_merge_user.execute_one(name=screen_name)

def get_or_create_users(screen_names):
    """Apply the get or create user cypher query to many usernames in a 
    batch transaction"""
    
    batch = neo4j.WriteBatch(graph_db)
    
    for screen_name in screen_names:
        batch.append_cypher(cypher_merge_user, params=dict(name=screen_name))

    return batch.submit()

root = get_or_create_user("Root")
users = get_or_create_users(["alice", "bob", "charlie"])

局限性

但是,有一个限制,因为批处理事务中的密码查询的结果不能稍后在同一事务中引用。最初的问题是关于在一批事务中更新索引用户属性的集合。据我所知,这仍然是不可能的。例如,以下代码段会引发错误:

batch = neo4j.WriteBatch(graph_db)
b1 = batch.append_cypher(cypher_merge_user, params=dict(name="Alice"))
batch.set_properties(b1, dict(last_name="Smith")})
resp = batch.submit()

因此,似乎虽然由于不再需要遗留索引而实现get_or_createover a label 节点的开销要少一些,但最初的问题仍然需要 2 个单独的批处理事务来完成。py2neo

于 2013-12-15T20:29:54.817 回答
1

您的问题似乎batch.set_properties()不在batch.get_or_create_in_index(). 如果添加带有 的节点batch.create(),它可以工作:

db = neo4j.GraphDatabaseService()

batch = neo4j.WriteBatch(db)
# create a node instead of getting it from index
test_node = batch.create({'key': 'value'})
# set new properties on the node
batch.set_properties(test_node, {'key': 'foo'})

batch.submit()

如果您查看返回的 BatchRequest 对象的属性,batch.create()并且batch.get_or_create_in_index()URI 存在差异,因为这些方法使用 neo4j REST API 的不同部分:

test_node = batch.create({'key': 'value'})
print test_node.uri # node
print test_node.body # {'key': 'value'}
print test_node.method # POST

index_node = batch.get_or_create_in_index(neo4j.Node, "Users", "user_id", 12345, {})
print index_node.uri # index/node/Users?uniqueness=get_or_create
print index_node.body # {u'value': 12345, u'key': 'user_id', u'properties': {}}
print index_node.method # POST

batch.submit()

所以我想batch.set_properties()不知何故无法处理索引节点的URI?即它并没有真正得到节点的正确 URI?

不能解决问题,但可能是其他人的指针;)?

于 2013-11-16T12:39:35.867 回答