简体   繁体   English

Neo4J查找节点之间的关系数(SIZE)

[英]Neo4J find the the number(SIZE) of relationships between nodes

I am trying to return a set of nodes where there is more than n outgoing relationships of the same kind. 我试图返回一组节点,其中有超过n种相同类型的传出关系。 The specific use case is given a set of movies which actors have contributed to more than one of those movies. 为特定的用例提供了一组电影,其中演员为其中一部电影做出了贡献。

I have tried multiple methods of COUNTing and SIZE , but cannot figure out whether this is even possible in Neo4J 我尝试了COUNTingSIZE多种方法,但无法弄清楚在Neo4J中这是否可能

MATCH (cw:CreativeWork) WHERE cw.officialTitle IN ['Antz', 'The Specialist ']
MATCH (p:Person) WHERE SIZE((p)-[:contributedTo]-(cw)) > 1
RETURN p, cw

This will return the two Creative Works specified and all the people who have contributed to the title, based on the relationship :contributedTo . 这将根据:contributedTo关系,返回指定的两个Creative Works以及为该标题做出贡献的所有人员。 Two actors in the list have contributed to both titles, and I am interested in returning just those two. 名单中的两个演员都为这两个奖项做出了贡献,而我有兴趣归还这两个。

在此处输入图片说明

This query for example returns no results: 例如,此查询不返回任何结果:

MATCH (cw:CreativeWork) WHERE cw.officialTitle IN ['Antz', 'The Specialist ']
MATCH (p:Person) WHERE SIZE((p)-[:contributedTo]-(cw)) > 1
RETURN p, cw

In case of your query value of SIZE() will be always 0 or 1 (Except if there are two or more relationships between any pair of Person and CreativeWork ). 如果您的查询值SIZE()始终为01 (除非在PersonCreativeWork任何对之间存在两个或多个关系)。 The reason for this is your query is aggregating the SIZE (or Count ) over Person and CreativeWork .(Same as Group by on both of these) 这样做的原因是您的查询汇总了PersonCreativeWorkSIZE (或Count )。(两者均与Group by相同)

You can use the following query which COUNTS CreativeWorks for each Person (This query returns a list of CreativeWork for Person. ): 您可以使用以下查询,该查询为每个Person COUNTS CreativeWorks (此查询返回CreativeWork for Person的列表。):

MATCH (cw:CreativeWork) WHERE cw.officialTitle IN ['Antz', 'The Specialist']
MATCH (p:Person)-[:contributedTo]-(cw)
WITH p,count(cw) as rels, collect(cw) as cws
WHERE rels > 1
RETURN p,cws

You can modify the above query to look simple as follows(Both performs the same): 您可以修改上面的查询,使其看起来很简单,如下所示(两者执行相同):

MATCH (p:Person)-[:contributedTo]-(cw:CreativeWork)
WHERE cw.officialTitle IN ['Antz', 'The Specialist']
WITH p,count(cw) as rels, collect(cw) as cws
WHERE rels > 1
RETURN p,cws

NOTE: There is a space at the end of the last title in the list (in your query). 注意:列表中最后一个标题的末尾有一个空格(在您的查询中)。

@Raj's last query can be simplified: @Raj的最后一个查询可以简化:

MATCH (p:Person)-[:contributedTo]-(cw:CreativeWork)
WHERE cw.officialTitle IN ['Antz', 'The Specialist']
WITH p, collect(cw) as cws
WHERE SIZE(cws) > 1
RETURN p, cws

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM