如果属性数大于n,则在Neo4j中返回一个子图 [英] If the number of properties is greater than n, return a subgraph in Neo4j
问题描述
此问题是说我有一个图形数据库,如下所示:
Say I have a graph database that looks like this:
就像我问的上一个问题一样,关于此的唯一真正有趣的事情是SomeProperty
可以是是"或否".
Just like the previous question I asked, the only really interesting thing about this 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个节点的此属性为是".其他2个为否".
On the bottom row, there are 3 nodes have a 'Yes' for this property. The other 2 have a 'No'.
如何编写一个仅返回以下一行的Cypher查询,方法是询问以下问题:这些不相交的子图中的一个是否都具有2个或多个SomeProperty ='Yes'的值?
How do I write a Cypher query that returns only THE WHOLE the bottom row, by asking the question: Do either of these disjoint subgraphs have 2 or more values for SomeProperty = 'Yes'?
以前,出色的@cybersam建议使用类似以下内容的内容:
Previously, the brilliant @cybersam recommended using something like:
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;
...这将返回匹配的路径.在这种情况下,它将返回
...which would return the matching paths. In this case, this would return
但是我试图对此进行归纳,以返回5个"B"节点的整个集合.
but I am trying to generalize this to return the whole set of 5 "B" nodes.
在苦苦挣扎之后,我意识到我正在尝试创建3节点子图,但没有返回它.我正在尝试使用它的存在作为返回我的比赛的超图,而忽略任何未连接的子图的方法!这变得越来越复杂,我很困惑.
After struggling with this, I realize that I'm trying to create the 3 node subgraph, but not return it. I'm trying to use its existence as a way to return the supergraph of my match, and ignore any non-connected subgraphs! This is getting pretty complicated and I'm stumped.
这是我的代码:
CREATE (albert:person {gender: 'Male', name: 'Albert', SomeProperty: 'Yes'})
CREATE (annie:person {gender: 'Female', name: 'Annie', SomeProperty: 'No'})
CREATE (adrian:person {gender: 'Female', name: 'Adrian', SomeProperty: 'No'})
CREATE (albert)-[r1:RELATED_TO]->(annie)
SET r1.relationship='related'
CREATE (annie)-[r2:RELATED_TO]->(adrian)
SET r2.relationship='related'
CREATE (bill:person {gender: 'Male', name: 'Bill', SomeProperty: 'Yes'})
CREATE (barb:person {gender: 'Female', name: 'Barb', SomeProperty: 'Yes'})
CREATE (barry:person {gender: 'Male', name: 'Barry', SomeProperty: 'Yes'})
CREATE (bart:person {gender: 'Male', name: 'Bart', SomeProperty: 'No'})
CREATE (bartholemu:person {gender: 'Male', name: 'Bartholemu', SomeProperty: 'No'})
CREATE (bill)-[r4:RELATED_TO]->(barb)
SET r4.relationship='related'
CREATE (barb)-[r5:RELATED_TO]->(barry)
SET r5.relationship='related'
CREATE (barry)-[r6:RELATED_TO]->(bart)
SET r6.relationship='related'
CREATE (bart)-[r7:RELATED_TO]->(bartholemu)
SET r7.relationship='related'
推荐答案
此查询将从结果中过滤出所有子路径:
This query will filter out all sub-paths from the results:
MATCH p=(a:person)-[:RELATED_TO*]->(b:person)
WHERE
NOT ()-[:RELATED_TO]->(a) AND
NOT (b)-[:RELATED_TO]->() AND
2 < REDUCE(s = 0, x IN NODES(p) | CASE WHEN x. SomeProperty = 'Yes' THEN s + 1 ELSE s END)
RETURN p;
说明:
-
如果
-
NOT ()-[:RELATED_TO]->(a)
为true.
如果 -
NOT (b)-[:RELATED_TO]->()
为true.
a
节点不在RELATED_TO
关系的末尾,则b
节点不在RELATED_TO
关系的开头,则NOT ()-[:RELATED_TO]->(a)
is true if thea
node is not the at the end of aRELATED_TO
relationship.NOT (b)-[:RELATED_TO]->()
is true if theb
node is not at the start of aRELATED_TO
relationship.
因此,匹配路径不能是子路径.
Therefore, a matching path cannot be a sub-path.
这篇关于如果属性数大于n,则在Neo4j中返回一个子图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!