Referring to this image , I have two categories of neo4j graph structure that I need to differentiate.
I have this logic to differentiate:
category 1 =>
Match (a:A)-[:A1]->(B)-[l1:B1]->(n1:C)
With n1, count(n1) as cnt, a Where cnt = 1 AND n1.id = "c"
Return a
category 2 =>
Match (a:A)-[:A1]->(B)-[l1:B1]->(n1:C)
With n1,count(n1) as cnt,a Where cnt>1 AND n1.id="c"
Return a
But this is not working for second category. Can somebody help with this?
I think you are missing the point about aggregation in Neo4j, you can not have a count(n1), n1 ... would obviously always return "1" as the count.
Try this and you'll see the difference, the aggregation should be on a only ...
Match (a:A)-[:A1]->(B)-[l1:B1]->(n1:c)
WHERE n1.name = "c"
With count(n1) as cnt,a Where cnt>1
Return a
Hope this helps.
Regards, Tom
PS Your first query is also wrong for the same reason ...
UPDATE after you updated the image ...
MATCH (a:A)-[:A1]->(B)-[l1:B1]->(n1:C {id: "c"})
WITH a, count(*) AS cnt
WHERE cnt > 1
RETURN a, cnt;
That should do it ...
You can use this as your math query, this matches any relationship with any nodes between them.
Match (a:A)-[:A1*]->(n1:C)
I hope this helps.
You can use the TYPE(<relationship>)
function to check a relationships relation type. So MATCH (a)-[r1]-(b)-[r2]-(c) WHERE TYPE(r1) = TYPE(r2) RETURN *
Will match all tuples where r1 and r2 are the same kind of relation.
You can then specify the relation direction as incoming ( (a)-[r1]->(b)<-[r2]-(c)
) or outgoing ( (a)<-[r1]-(b)-[r2]->(c)
)
Note: With Cypher, it insures that no node is revisited in a single pattern match, so you don't need to check that a is not c ( a <> c
)
// category 1 =>
MATCH ()-[r1]->(b:C)<-[r2]-()
WHERE TYPE(r1) = TYPE(r2)
WITH DISTINCT b
MATCH (a:A)-[*..25]->(b)
RETURN DISTINCT a
// category 1, known relation =>
MATCH ()-[r1:B1]->(b:C)<-[r2:B1]-()
WITH DISTINCT b
MATCH (a:A)-->()-->(b)
RETURN DISTINCT a
// category 2 =>
MATCH ()<-[r1]-(b:B)-[r2]->()
WHERE TYPE(r1) = TYPE(r2)
WITH DISTINCT b
MATCH (a:A)-[*..25]->(b)
RETURN DISTINCT a
// category 2, known relation =>
MATCH ()<-[r1:B1]-(b:B)-[r2:B1]->()
WITH DISTINCT b
MATCH (a:A)-->(b)
RETURN DISTINCT a
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.