2

How do I pass parameters to gds.create.graph in Neo4j? For instance, what is wrong with this query? (I use py2neo)

query = """
CALL gds.graph.create.cypher(
    'betweenness',
    'MATCH (n) WHERE n:Criminal AND id(n) in $nodes_in_component 
    RETURN id(n) AS id, labels(n) AS labels',
    'MATCH (m:Crime)<-[r:INVOLVED_IN]-(n:Criminal) 
    WHERE  id(m) in $nodes_in_component
    RETURN id(m) AS source, id(n) AS target, type(r) AS type',
    parameters:{nodes_in_component: nodes_in_component}
    )
    YIELD graphName, nodeCount, relationshipCount, createMillis
    """
graph.run(query, parameters= {"nodes_in_component":nodes_in_component}).data()
Sanjay Singh
  • 367
  • 1
  • 11
  • what is the error you are getting? Looks like you are using python, is it correct? – jose_bacoy Mar 22 '21 at 12:14
  • Invalid input '{': expected whitespace or a label name (line 9, column 16 (offset: 332)) " parameters:{nodes_in_component: nodes_in_component}" Basically, not accepting the parameters passed to gds.graph.create.cypher() – Sanjay Singh Mar 24 '21 at 03:34

1 Answers1

3
  1. We need to return both nodes Criminal and Crime. Im getting an error if Crime nodes are missing
  2. The syntax for parameters should include {}. For example: {parameters: { node_ids: ext_param }}. Please also note to use a different name like ext_param.
  3. Then in Python, replace the word ext_param using the replace function but convert the list as a string
query = """
CALL gds.graph.create.cypher(
    'betweenness',
    'MATCH (n) WHERE n:Criminal OR n:Crime RETURN id(n) AS id, labels(n) AS labels',
    'MATCH (a:Criminal)-[r:INVOLVED_IN]->(c:Crime) WHERE id(a) in $node_ids RETURN id(a) AS source, id(c) AS target, type(r) AS type',
    {parameters: { node_ids: ext_param }}
)
YIELD graphName, nodeCount, relationshipCount, createMillis;
"""

ext_param = [5, 6, 7, 8, 31]
graph.run(query.replace('ext_param', str(ext_param))).data()

Result:
 [{'graphName': 'betweenness',
  'nodeCount': 10,
  'relationshipCount': 12,
  'createMillis': 22}]

Reference: Look for section 4: Node labels in here

jose_bacoy
  • 12,227
  • 1
  • 20
  • 38