目前我得到了以下Cypher查询:
MATCH (topic:TopicNode)-[r]-(article:ArticleNode)-[b]->(author:AuthorNode)
MATCH (topic)-[v]->(source:SourceNode)
WHERE r.relevancy > 0.002
RETURN topic, count(DISTINCT article), count(DISTINCT author), count(DISTINCT source)
工作良好,并为每个主题提供预期的结果。
现在我想获得以下附加信息:每个主题具有积极情绪(例如article.sentiment > 0
)和消极情绪的文章的数量。
是否有方法将条件语句添加到计数操作符?
例如:
RETURN count(article.sentiment > 0)
因为这似乎不适合我。
您可以尝试使用列表推导式:
MATCH (topic:TopicNode)-[r]-(article:ArticleNode)-[b]->(author:AuthorNode)
MATCH (topic)-[v]->(source:SourceNode)
WHERE r.relevancy > 0.002
WITH topic, count(DISTINCT article) AS articlesCount, count(DISTINCT author) AS authorsCount, count(DISTINCT source) AS sourceCount, collect(DISTINCT article) AS articles
RETURN topic, articlesCount, authorsCount, sourceCount, size([x IN articles WHERE x.sentiment > 0 | x ]) AS positiveSentiment, size([x IN articles WHERE x.sentiment < 0 | x ]) AS negativeSentiment
在这里,我们收集列表中的文章,然后使用列表推导,过滤出具有积极和消极情绪的文章,并计算它们的计数。试一试。