简体   繁体   中英

Neo4j Cypher Query: Finding all nodes, that are connected to a node, that has more than 3 other relationships

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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