在Cypher查询中允许深度参数 [英] Allow parameters for depth in Cypher query
问题描述
我正在使用用于Python的Neo4j Bolt驱动程序1.7 来提取特定数据库的路径,这是导致问题的代码示例.
I'm using Neo4j Bolt Driver 1.7 for Python to extract paths from a specific database, here's a sample of code that causes the problem.
from neo4j import GraphDatabase
uri = "bolt://0.0.0.0:7878"
driver = GraphDatabase.driver(uri, auth=("neo4j", "neo4j"))
db = driver.session()
query = '''
MATCH tree = (n:Class)-[r:SUBCLASSOF*{depth}]->(parent) # <---- ERROR here
WHERE n.obo_id = {go}
RETURN [n in nodes(tree) | n.obo_id] as GOID
'''
results = []
goid = "GO:0051838"
for record in db.run(query, go=goid, depth="..2"):
results.append(record["GOID"])
print(results)
当我使用{depth}
参数时,出现以下错误:
When I use {depth}
parameter I get the following error:
Traceback (most recent call last):
File "neoEnrich.py", line 16, in <module>
for record in db.run(query, go=goid, depth="..2"):
File "/usr/local/lib/python3.6/site-packages/neo4j/__init__.py", line 499, in run
self._connection.fetch()
File "/usr/local/lib/python3.6/site-packages/neobolt/direct.py", line 414, in fetch
return self._fetch()
File "/usr/local/lib/python3.6/site-packages/neobolt/direct.py", line 454, in _fetch
response.on_failure(summary_metadata or {})
File "/usr/local/lib/python3.6/site-packages/neobolt/direct.py", line 738, in on_failure
raise CypherError.hydrate(**metadata)
neobolt.exceptions.CypherSyntaxError: Parameter maps cannot be used in MATCH patterns (use a literal map instead, eg. "{id: {param}.id}") (line 2, column 38 (offset: 42))
" MATCH tree = (n:Class)-[r:SUBCLASSOF*{depth}]->(parent)"
^
当用..2
替换{depth}
时,我得到了预期的结果:
when replacing {depth}
by ..2
, I got the desired result:
[['GO:0051838', 'GO:0051801'],
['GO:0051838', 'GO:0051801', 'GO:0051883'],
['GO:0051838', 'GO:0051801', 'GO:0051715'],
['GO:0051838', 'GO:0051873'],
['GO:0051838', 'GO:0051873', 'GO:0051883'],
['GO:0051838', 'GO:0051873', 'GO:0051852']]
这里是否有允许深度参数的方法?由于用户将指定深度(将是功能参数).
Is there a way to allow parameters for depth here ? Since a user will specify the depth (will be a function parameter).
推荐答案
不允许使用参数设置节点标签,关系标签,关系深度.
Parameters are not allowed to set Node Labels, Relationship Labels, Relationship depths.
如果您确实需要此深度作为参数,请在python中将查询创建为字符串,并将关系深度作为参数传递给它.
If you really need this depth as a parameter then create a query as a string in python and pass relationship depths to it as a parameter.
保留查询中的其他参数(此处为go
).
Keep other parameters(here go
) as it is in the query.
这篇关于在Cypher查询中允许深度参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!