Cypher -Tally report on the depth of each relationship type stemming from parent node

岁酱吖の 提交于 2019-12-12 02:48:05

问题


I am trying to create tally report on the depth of each relationship type stemming from parent node but I running into issues with the following error:

"Type mismatch: r already defined with conflicting type Relationship (expected Collection<Relationship>)

Here is the output I am attempting to achieve:

[
   {
      reltype    : "123A_RelationshipTitleOne",
      depthcount : 5
   }, {
      reltype    : "123A_RelationshipTitleTwo",
      depthcount : 9
   }, {
      reltype    : "123A_RelationshipTitleThree",
      depthcount : 42
   }
]

Here is my cypher query attempt that generates the error mentioned above. In place of '123A' I use a a variable but I used '123A' as a legible working example here:

MATCH (n {id: '123A'})
OPTIONAL MATCH (n)-[r]-()
WHERE left( type(r), LENGTH( '123A' )) = '123A'
OPTIONAL MATCH p=(n)-[r*]->(c)
WITH n, COLLECT({
    id    : type(r),
    count : MAX(length(p))
}) AS leafreport
RETURN n, leafreport

I am very grateful for help you can offer.


回答1:


The type mismatch error is being caused by using the bound variable r to represent first a relationship and then (in the second OPTIONAL MATCH) a Collection of relationships.

In the pattern (n)-[r*]->(c), r* indicates a variable length path so r is bound to a Collection of relationships that match the variable length pattern. However r was previously bound to a relationship in OPTIONAL MATCH (n)-[r]-(), thus the error. If you want to check that r is in the path, p, use a WHERE clause. Something like this:

MATCH (n {id: '123A'})
MATCH (n)-[r]-()
WHERE left( type(r), LENGTH( '123A' )) = '123A'
MATCH p=(n)-[rs*]->(c) WHERE r IN rs
WITH n, {
    id    : type(r),
    count : MAX(length(p))
} AS leafreport
RETURN n, leafreport

I don't think you want the second OPTIONAL MATCH to be optional, so I changed it to a MATCH clause



来源:https://stackoverflow.com/questions/33635241/cypher-tally-report-on-the-depth-of-each-relationship-type-stemming-from-parent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!