Neo4j 中的标记子图

labelling subgraph in Neo4j

我想为每个子图分配唯一的 ID。

LOAD CSV WITH HEADERS FROM 'file:///test.csv' AS row
MERGE (a: Name { name: row.col1 })
MERGE (b: desc { name: row.col2 })
MERGE (a)-[:count {cnt: row.count}]->(b)

RETURN a, b

期望的输出:

 uniqueid    |   group
----------------------------------

  123        |   B, flower, fruit

  234        |   A, tree

  345        |   D, milk

有人可以帮我吗?

您想使用 Graph Data Science Library and specifically the Weakly Connected components 算法。它完全符合您的要求:

The WCC algorithm finds sets of connected nodes in an undirected graph, where all nodes in the same set form a connected component.

要写回您可以使用的结果:

CALL gds.wcc.write({
    nodeProjection:'*',
    relationshipProjection:'*',
    writeProperty: 'componentId'
})
YIELD nodePropertiesWritten, componentCount;

然后检查结果:

MATCH (node)
RETURN node.componentId as uniqueId, 
       collect(node.name) as members

在 Neo4j v4.x 中有一个新功能可以实现您的目标。

LOAD CSV FROM 'your file' AS line
RETURN linenumber() AS number,{other items}

Neo4j v4 documentation