0
votes

Unsure of how best to word this - in the below diagram

neo

I am trying to return all nodes where the 2nd tier node is related to 2 or more of the 1st tier nodes, but only where the first tier nodes are different. So in the example picture I want to return grey node 660082, green node 110258, and all the pink nodes & relationships that are related to it. I do not want to see, for example, green nodes where pink nodes have 2 relationships to the same green node. so far my cypher looks like this

MATCH (i:IFB_Flagged)-[r]->(m:Matters)<-[r2]-(ie:Indirect_Entity)
WITH i, ie,r, collect(m) AS overlap 
WHERE size(overlap) > 1 and i.id = '660082'
RETURN i, ie, overlap 

This does appear to work as it filters out pink nodes that have only one link to a green node, however I can't figure out how to only return pink nodes where the related green nodes are not the same node

UPDATE

When adding a distinct to the collect & removing the where clause entirely I get a completely different graph, however it is actually closer to what I want to show neo2

I still want to see all pink nodes that relate to more than one green node - however I do not want to see the pink nodes that only relate to one green node (the 6 pink nodes at the bottom left of the screen)

Here is my Cypher - I am not sure why collect(distinct m) doesn't work in this case?

MATCH (i:IFB_Flagged)-[r]->(m:Matters)<-[r2]-(ie:Indirect_Entity) 
WITH i, ie,r, collect(distinct m) AS overlap 
WHERE i.id = '385886' 
RETURN i, ie, overlap limit 20
1
But in your example the green node is the same for all red nodes and the same for both relations of each red node... Anyway, try putting a distinct clause in your collect, this way: collect(distinct m) - Bruno Peres
The question's wording does not match what you actually seem to want. - cybersam
Red nodes in the question are actually more like pink. Let's wait for the OP to get this straight. - Gabor Szarnyas
edited my original question for clarity - Shaun Parker
"I am not sure why collect(distinct m) doesn't work in this case?" it doesn't work because you left variable r in the WITH clause, so it will aggregate on each r separately. - Gabor Szarnyas

1 Answers

2
votes

Use the DISTINCT keyword for collect (docs):

The DISTINCT operator works in conjunction with aggregation. It is used to make all values unique before running them through an aggregate function.

Also, you can move the check for id inside the MATCH clause for conciseness.

Update. Try this.

MATCH (i:IFB_Flagged)-[r]->(m:Matters)<-[r2]-(ie:Indirect_Entity) 
WHERE i.id = '385886'
WITH i, ie, collect(distinct m) AS overlap 
WHERE size(overlap) > 1
RETURN i, ie, overlap
LIMIT 20