如果属性数大于n,则返回Neo4j中的路径 [英] If the number of properties is greater than n, return the paths in Neo4j
问题描述
所以我有一个如下图数据库:
So I have a graph database that looks like this:
唯一有趣的是SomeProperty
可以是是"或否".
The only really interesting thing about it is that SomeProperty
can be 'Yes' or 'No'.
在第一行中,这3个节点中的1个对此属性具有是".
In the top row, 1 of the 3 nodes have a 'Yes' for this property.
在底行上,所有3个节点的此属性都为是".
On the bottom row, all 3 nodes have a 'Yes' for this property.
如何通过询问以下问题来编写仅返回最底行的Cypher查询:哪个子图的SomeProperty
='是'具有2个或更多值?
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'?
这是我的代码:
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'
推荐答案
要返回具有超过2个是"节点的所有路径:
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;
REDUCE
函数用于计算SomeProperty
值为是"的节点数.
The REDUCE
function is used to calculate the number of nodes with a SomeProperty
value of 'Yes'.
这篇关于如果属性数大于n,则返回Neo4j中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!