Skip to content Skip to sidebar Skip to footer

Allow Parameters For Depth In Cypher Query

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:

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

Solution 2:

What about

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

Solution 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

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

Post a Comment for "Allow Parameters For Depth In Cypher Query"