简体   繁体   English

Neo4j 统计相关节点个数

[英]Neo4j count the number of related nodes

I'm starting studying the Neo4j and the Cypher Queries, and I need some help to understand and get some results.我开始研究 Neo4j 和 Cypher 查询,我需要一些帮助来理解和获得一些结果。

I have this MATCH query:我有这个 MATCH 查询:

MATCH (p:EntidadePessoa {id: 168750})-[rd:SE_RELACIONA]->(d:Documento)<--(p2) 
 where p2:EntidadePessoa or p2:EntidadeOrganizacao 
 return p,d,p2

That results this:结果是:

图形图像

Commencing with EntidadePessoa id:168750, I want the count of EntidadeDocumento directcly linked to EntidadePessoa id:168750, is this case 2, and the count of each Entidade* that is linked to EntidadeDocumento, in this case 4 for each EntidadeDocumento.从 EntidadePessoa id:168750 开始,我希望 EntidadeDocumento 的计数直接链接到 EntidadePessoa id:168750,在这种情况下是 2,以及链接到 EntidadeDocumento 的每个 Entidade* 的计数,在这种情况下,每个 EntidadeDocumento 是 4。

I tried some queries, but none give me the results I wanted, the count number is never the numbers I wanted.我尝试了一些查询,但没有一个给我想要的结果,计数永远不是我想要的数字。

Could you help with that?你能帮忙吗?

  1. A query to get the count of d nodes connected to p (id: 168750).获取连接到 p (id: 168750) 的 d 个节点数的查询。
    MATCH (p:EntidadePessoa {id: 168750})-[rd:SE_RELACIONA]->(d:Documento)
    RETURN p.id as `nodeId`, count(d) as `connectedCount`

output: output:

 nodeId connectedCount 168750 2
  1. A query with a subquery to look at just d:Documento, incoming links from p2, omitting (p:EntidadePessoa {id: 168750}) and counting p2s for each d node:带有子查询的查询仅查看 d:Documento、来自 p2 的传入链接、省略 (p:EntidadePessoa {id: 168750}) 并计算每个 d 节点的 p2:
    CALL
    {MATCH (p)-[rd:SE_RELACIONA]->(d:Documento)
    WHERE p.id=168750
    RETURN  d,p.id as `topid`}
    MATCH (p2)-->(d)
    WHERE (p2:EntidadePessoa or p2:EntidadeOrganizacao) AND p2.id<>topid
    //p2.id<>topid ensures p is not included in count(p2) 
    RETURN d.id as `nodeId`, count(p2) as `connectedCount`

output: output:

 nodeId connectedCount 164532 4 164552 4
  1. Combine both these results with a UNION:将这两个结果与 UNION 结合起来:
    MATCH (p:EntidadePessoa {id: 168750})-[rd:SE_RELACIONA]->(d:Documento)
    RETURN p.id as `nodeId`, count(d) as `connectedCount`UNION
    CALL
    {MATCH (p)-[rd:SE_RELACIONA]->(d:Documento)
    WHERE p.id=168750
    RETURN  d,p.id as `topid`}
    MATCH (p2)-->(d)
    WHERE (p2:EntidadePessoa or p2:EntidadeOrganizacao) AND p2.id<>topid
    //p2.id<>topid ensures p is not included in count(p2) 
    RETURN d.id as `nodeId`, count(p2) as `connectedCount`

output: output:

 nodeId connectedCount 168750 2 164552 4 164552 4

This will give you the result.这会给你结果。 This is similar to count and group_by in sql.这个类似于sql中的count和group_by。

 MATCH (p:EntidadePessoa {id: 168750})-[rd:SE_RELACIONA]->(d:Documento)<--(p2) 
  where p2:EntidadePessoa or p2:EntidadeOrganizacao 
 With p, count(distinct d) as cnt_d, count(p2) as cnt_p2
  return p, cnt_d, cnt_p2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM