【问题标题】:Neo4j, py2neo, Neomodel - Cypher Shortest Path giving error - TypeError: 'NotImplementedType' object is not callableNeo4j、py2neo、Neomodel - Cypher 最短路径给出错误 - TypeError:'NotImplementedType' 对象不可调用
【发布时间】:2014-10-02 03:29:21
【问题描述】:

我正在尝试在 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

【问题讨论】:

    标签: python neo4j py2neo neomodel


    【解决方案1】:

    好的,我想通了。我使用了 [here] 教程(基于 @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')),))]]
    

    从这里开始,我希望我会将每个值膨胀回我的新模型对象和 django 中,以便于操作。当我到达那里时将发布该代码。

    【讨论】:

      【解决方案2】:

      您提供的错误消息是特定于 neomodel 的,并且似乎已经提出,因为目前还没有任何支持在 neomodel 中膨胀 py2neo Path 对象。

      这在原始 py2neo 中应该可以正常工作,因为完全支持路径,因此可能值得再次尝试。 Py2neo 当然不会在 neomodel 代码中引发错误。我自己尝试了一个shortestPath 查询,它按预期返回了一个值。

      【讨论】:

      • 谢谢你奈杰尔。你能发布你的示例代码吗?我很好奇如何在 python 中运行原始 py2neo 密码查询,因为我认为这就是我上面所做的......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-06
      • 1970-01-01
      相关资源
      最近更新 更多