我试图在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中有更好的方法吗?或者是cypher最好的方法吗?
编辑:
我还尝试了here中的以下内容,但也出现了同样的错误。
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
答案 0 :(得分:2)
好的,我明白了。我使用了教程[这里](基于@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以便于操作。我会到那里发布那些代码。
答案 1 :(得分:0)
您提供的错误消息特定于neomodel并且看起来已经被提出,因为还没有任何支持在neomodel中对py2neo Path对象进行充气。
然而,这应该在原始py2neo中正常工作,因为完全支持路径,因此可能值得再试一次。 Py2neo肯定不会在neomodel代码中引发错误。我自己尝试了shortestPath
查询,并按预期返回了一个值。