NEO4J计算连接数少于X的过滤器节点数

iezvtpos  于 2023-05-17  发布在  其他
关注(0)|答案(2)|浏览(147)

我有以下查询:

MATCH(cw:commonWords)-[theEdge]->(story:theStory) 
WHERE cw.text IN ['tools','sell'] RETURN cw,story

我想过滤掉所有少于2个传入连接的story节点(蓝色)。

ercv8c1e

ercv8c1e1#

一种选择是:

MATCH(cw:commonWords)-[theEdge]->(story:theStory)
WHERE cw.text IN ['tools','sell']
WITH collect(cw) AS cws, story
WHERE size(cws) > 1
UNWIND cws AS cw
RETURN cw, story

其中,使用此示例数据:

MERGE (a1:commonWords {text: 'tools'})  
MERGE (a2:commonWords {text: 'sell'}) 
MERGE (b1:theStory {key: 4756})
MERGE (b2:theStory {key: 4753}) 
MERGE (b3:theStory {key: 4752}) 
MERGE (b4:theStory {key: 4755}) 

MERGE (a1)-[:commonWordsInStory]-(b1)
MERGE (a2)-[:commonWordsInStory]-(b1)
MERGE (a1)-[:commonWordsInStory]-(b2)
MERGE (a2)-[:commonWordsInStory]-(b2)
MERGE (a1)-[:commonWordsInStory]-(b3)
MERGE (a2)-[:commonWordsInStory]-(b3)
MERGE (a1)-[:commonWordsInStory]-(b4)

退货:

╒════════════════╤════════════╕
│"cw"            │"story"     │
╞════════════════╪════════════╡
│{"text":"tools"}│{"key":4756}│
├────────────────┼────────────┤
│{"text":"sell"} │{"key":4756}│
├────────────────┼────────────┤
│{"text":"tools"}│{"key":4753}│
├────────────────┼────────────┤
│{"text":"sell"} │{"key":4753}│
├────────────────┼────────────┤
│{"text":"tools"}│{"key":4752}│
├────────────────┼────────────┤
│{"text":"sell"} │{"key":4752}│
└────────────────┴────────────┘
js5cn81o

js5cn81o2#

要过滤掉传入commonWordsInStory关系少于2个的story节点(并返回每个结果story沿着常用词列表):

MATCH (cw:commonWords)-[:commonWordsInStory]->(story:theStory) 
WHERE cw.text IN ['tools', 'sell']
WITH story, COLLECT(cw) AS commonWords
WHERE SIZE(commonWords) > 1
RETURN story, commonWords

如果你想在单独的一行中返回每个常用单词,只需将上面的RETURN子句替换为:

...
UNWIND commonWords AS commonWord
RETURN story, commonWord

相关问题