Allow parameters for depth in Cypher query

☆樱花仙子☆ 提交于 2019-12-11 15:17:18

问题


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)

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)"
                                          ^

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).


回答1:


Parameters are not allowed to set Node Labels, Relationship Labels, Relationship depths.

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.

Keep other parameters(here go) as it is in the query.




回答2:


What about

MATCH tree = (n:Class)-[r:SUBCLASSOF*..10]->(parent)
WHERE LENGTH(tree)<=$depth



回答3:


You can use parameters for the minLevel and maxLevel arguments to the APOC function apoc.path.expand.

For example:

MATCH (n:Class)
WHERE n.obo_id = $go
CALL apoc.path.expand(n, "SUBCLASSOF>", "", 1, $depth) YIELD path
RETURN [n IN NODES(path) | n.obo_id] AS GOID



回答4:


Thanks to @Raj answer, the simplest solution I found was to use .format()

The query becomes:

query =  '''
    MATCH tree = (n:Class)-[r:SUBCLASSOF*{depth}]->(parent)
    WHERE n.obo_id = "{go}"
    RETURN [n in nodes(tree) | n.obo_id] as GOID
'''

Then constructed the query and executed db.run()

full_query = query .format(go=goid, depth="..2")
for record in db.run(full_query):
    ...


来源:https://stackoverflow.com/questions/55006763/allow-parameters-for-depth-in-cypher-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!