问题
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