If the number of properties is greater than n, ret

2019-02-25 18:27发布

问题:

So I have a graph database that looks like this:

The only really interesting thing about it is that SomeProperty can be 'Yes' or 'No'.

In the top row, 1 of the 3 nodes have a 'Yes' for this property.

On the bottom row, all 3 nodes have a 'Yes' for this property.

How do I write a Cypher query that returns only the bottom row, by asking the question: Which subgraph has 2 or more values for SomeProperty = 'Yes'?

Here is my code:

CREATE (person:Person {gender: 'Male', name: 'Albert', SomeProperty: 'Yes'})
CREATE (person:Person {gender: 'Female', name: 'Annie', SomeProperty: 'Yes'})
CREATE (person:Person {gender: 'Female', name: 'Adrian', SomeProperty: 'No'})

MATCH (a1:Person),(a2:Person)
WHERE a1.name = 'Albert' AND a2.name = 'Annie'
CREATE (a1)-[r:RELATED_TO]->(a2)
SET r.relationship='related'

MATCH (a1:Person),(a2:Person)
WHERE a1.name = 'Annie' AND a2.name = 'Adrian'
CREATE (a1)-[r:RELATED_TO]->(a2)
SET r.relationship='related'

CREATE (person:Person {gender: 'Male', name: 'Bill', SomeProperty: 'Yes'})
CREATE (person:Person {gender: 'Female', name: 'Barb', SomeProperty: 'Yes'})
CREATE (person:Person {gender: 'Male', name: 'Barry', SomeProperty: 'Yes'})

MATCH (a1:Person),(a2:Person)
WHERE a1.name = 'Bill' AND a2.name = 'Barb'
CREATE (a1)-[r:RELATED_TO]->(a2)
SET r.relationship='related'

MATCH (a1:Person),(a2:Person)
WHERE a1.name = 'Barb' AND a2.name = 'Barry'
CREATE (a1)-[r:RELATED_TO]->(a2)
SET r.relationship='related'

回答1:

To return all paths that have more than 2 'Yes' nodes:

MATCH p=(:Person)-[:RELATED_TO*]->(:Person)
WHERE 2 < REDUCE(s = 0, x IN NODES(p) | CASE WHEN x. SomeProperty = 'Yes' THEN s + 1 ELSE s END)
RETURN p;

The REDUCE function is used to calculate the number of nodes with a SomeProperty value of 'Yes'.



回答2:

I think I am a bit confused, because I am not sure how long a chain can be, if it's always only three then I guess something like this could return them maybe

MATCH (p1)-[r1]-(p2)-[r2]-(p3) 
WHERE (a1.SomeProperty ='Yes' AND a2.SomeProperty ='Yes') 
OR (a1.SomeProperty ='Yes' AND a3.SomeProperty ='Yes') 
RETURN a1,a2,a3 

And if it can be longer then maybe starting finding the 2 nodes

MATCH (a1)-[..]-(a2) 
WHERE (a1.SomeProperty ='Yes' AND a2.SomeProperty ='Yes')
RETURN a1,a2

and then extract the subgraph starting from those nodes as here: Extract subgraph in neo4j

Granted I haven't played with Neo4J all that much, so I am sure someone will pop by and give a better tip



标签: neo4j cypher