在 Neo4j 中将参数传递给 gds.create.graph

Passing parameters to gds.create.graph in Neo4j

如何在 Neo4j 中将参数传递给 gds.create.graph?例如,这个查询有什么问题? (我用的是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()
  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}]

参考: 查找第 4 部分:here

中的节点标签