2
votes

I have a problem with my Cypher query. I have some nodes called :SENTENCE and some other called :WORD. :SENTENCE nodes have relationships :CONTAINS to :WORD nodes.

I want to find :SENTENCE nodes, that are connected to :WORD nodes, that are used from more than 3 other :SENTENCE nodes. All :WORD nodes have to comply this criterion.

I tried something like this:

MATCH p=(s1:SENTENCE)-[:CONTAINS]-(w:WORD)-[:CONTAINS]-(s2:SENTENCE)
WITH s1,w, COUNT(s2) as num 
WHERE num > 3  
RETURN s1
LIMIT 25

But the result contains :SENTENCE nodes, where one and not all :WORD nodes have a degree of minimum 3.

Some other try:

MATCH p=(s1:SENTENCE)-[:CONTAINS]-(w:WORD)-[:CONTAINS]-(s2:SENTENCE)
WHERE SIZE((:SENTENCE)-[:CONTAINS]-(w:WORD)) > 3 
RETURN s1
LIMIT 25

But this does not hold for any :WORD nodes that is contained in an Sentence. It only holds for 1.

So my question is: How can I make a query that the condition hold for all nodes and not only for one.

1

1 Answers

2
votes

This kind of requirement usually requires collecting nodes and using the all() function to ensure some predicate holds true for all elements of the collection:

MATCH (s1:SENTENCE)-[:CONTAINS]-(w:WORD)
WITH s1, collect(w) as words
WHERE all(word in words WHERE size((word)-[:CONTAINS]-()) > 3)
RETURN s1
LIMIT 25