1

如何在 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()
4

1 回答 1

1
  1. 我们需要返回两个节点犯罪和犯罪。如果犯罪节点丢失,我会收到错误消息
  2. 参数的语法应包含 {}。例如:{参数:{ node_ids:ext_param }}。另请注意使用不同的名称,例如 ext_param。
  3. 然后在 Python 中,使用 replace 函数替换单词 ext_param,但将列表转换为字符串
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 节:节点标签

于 2021-03-22T15:35:33.697 回答