1

I am trying to return a set of nodes where there is more than n outgoing relationships of the same kind. 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

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. Two actors in the list have contributed to both titles, and I am interested in returning just those two.

enter image description here

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
HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133

2 Answers2

0

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). 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)

You can use the following query which COUNTS CreativeWorks for each Person (This query returns a list of 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).

Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
0

@Raj's last query can be simplified:

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
cybersam
  • 63,203
  • 6
  • 53
  • 76