[英]Neo4j, py2neo, Neomodel - Cypher Shortest Path giving error - TypeError: 'NotImplementedType' object is not callable
我正在嘗試在neomodel中運行以下Cypher查詢:
MATCH (b1:Bal { text:'flame' }), (b2:Bal { text:'candle' }),
p = shortestPath((b1)-[*..15]-(b2))
RETURN p
通過服務器控制台在neo4j上非常有效。 它返回具有兩個關系連接的3個節點。 但是,當我在python中嘗試以下操作時:
# Py2Neo version of cypher query in python
from py2neo import neo4j
graph_db = neo4j.GraphDatabaseService()
shortest_path_text = "MATCH (b1:Bal { text:'flame' }), (b2:Bal { text:'candle' }), p = shortestPath((b1)-[*..15]-(b2)) RETURN p"
results = neo4j.CypherQuery(graph_db, shortest_path_text).execute()
要么
# neomodel version of cypher query in python
from neomodel import db
shortest_path_text = "MATCH (b1:Bal { text:'flame' }), (b2:Bal { text:'candle' }), p = shortestPath((b1)-[*..15]-(b2)) RETURN p"
results, meta = db.cypher_query(shortest_path_text)
都給出以下錯誤:
/Library/Python/2.7/site-packages/neomodel-1.0.1-py2.7.egg/neomodel/util.py in _hydrated(data)
73 elif obj_type == 'relationship':
74 return Rel(data)
---> 75 raise NotImplemented("Don't know how to inflate: " + repr(data))
76 elif neo4j.is_collection(data):
77 return type(data)([_hydrated(datum) for datum in data])
TypeError: 'NotImplementedType' object is not callable
考慮到neomodel基於py2neo,這是有道理的。
主要問題是如何通過這兩個方法使shortestPath查詢起作用? python中是否有更好的方法? 還是密碼是最好的方法?
編輯:
我也從這里嘗試了以下給出相同錯誤的方法。
graph_db = neo4j.GraphDatabaseService()
query_string = "START beginning=node(1), end=node(4) \
MATCH p = shortestPath(beginning-[*..500]-end) \
RETURN p"
result = neo4j.CypherQuery(graph_db, query_string).execute()
for r in result:
print type(r) # r is a py2neo.util.Record object
print type(r.p) # p is a py2neo.neo4j.Path object
好的,我知道了。 我使用了[這里](基於@ nigel-small的答案的教程。
from py2neo import cypher
session = cypher.Session("http://localhost:7474")
tx = session.create_transaction()
tx.append("START beginning=node(3), end=node(16) MATCH p = shortestPath(beginning-[*..500]-end) RETURN p")
tx.execute()
返回:
[[Record(columns=(u'p',), values=(Path(Node('http://localhost:7474/db/data/node/3'), ('threads', {}), Node('http://localhost:7474/db/data/node/1'), ('threads', {}), Node('http://localhost:7474/db/data/node/2'), ('threads', {}), Node('http://localhost:7474/db/data/node/16')),))]]
從這里開始,我希望我將每個值都膨脹回我的neomodel對象和django中,以便於操作。 我到那里時將發布該代碼。
您提供的錯誤消息是特定於neomodel的,並且由於沒有任何支持在neomodel中膨脹py2neo Path對象而引起的錯誤消息。
但是,由於完全支持路徑,因此在原始py2neo中應該可以正常工作,因此值得再次嘗試。 Py2neo當然不會在新模型代碼中引發錯誤。 我剛剛嘗試了一個shortestPath
查詢,它返回了預期的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.