2014-03-26 61 views
1

我使用py2neo 1.6.4和neo4j 2.0.1,我發現訪問索引節點的一些奇怪之處。特別是,索引訪問的索引節點不返回與id訪問的節點相同的對象。由id訪問py2neo節點返回不同對象比索引訪問節點

例如:

>>> graph_db.get_or_create_indexed_node('index','key',1) 
Node('http://localhost:7474/db/data/node/1') 
>>> graph_db.get_indexed_node('index','key',1) 
Node('http://localhost:7474/db/data/node/1') #get works fine after create 
>>> graph_db.get_indexed_node('index','key',1).exists 
True         #the node exists in the db 
>>> graph_db.get_indexed_node('index','key',1)._id 
1         #the id for the node 
>>> graph_db.node(1) 
Node('http://localhost:7474/db/node/ #note that this is different than the query on the index 
>>> graph_db.node(1).exists 
False        #node does not exist in db when accessed by id 

所以當通過ID訪問實際上並不存在於數據庫中,即使ID返回返回的節點正是分配給索引節點。

我對neo4j和py2neo都很陌生,對索引的理解也沒有絕對的深刻理解,所以如果有一個答案可以幫助教育我和其他人,那將是非常棒的,如果這代表了一個錯誤將很高興知道以及:)

謝謝!

回答

2

我並不完全熟悉py2neo如何確定數據庫中是否存在節點,但您可能想嘗試使用Neo4j 2.0.0中引入的新索引。您在這裏使用的索引是遺留索引,這些索引需要您手動更新它們,並且在其操作過程中有幾個注意事項。新的索引會自動保持最新狀態,並以更好的方式作爲查詢的優化工作,同樣可以在關係數據庫中進行索引。

我不知道如何或如果py2neo直接暴露這些索引,但你可以通過py2neos密碼API訪問它們。在使用neo4j服務器時,使用密碼查詢語言通常是一個更好的主意,因爲它允許您發送更大的域工作塊來在數據庫中完成,而不是一次將數據從一個http調用中拉出來並執行在客戶端工作。

例如:

from py2neo import cypher 

session = cypher.Session("http://localhost:7474") 
tx = session.create_transaction() 

# Create an index 
tx.append("CREATE INDEX ON :User(name)") 
tx.commit() 

# Query that will use the index for lookup 
tx = session.create_transaction() 
tx.append("MATCH (n:User) WHERE n.name='Cat Stevens' RETURN n") 
results = tx.execute() 
+0

而這並不完全解決問題的行爲,但它建議不共享同樣的問題一個更好的選擇 - 在我的書的聲音正確的! – Parker