简体   繁体   English

使用cypher在neo4j中查找具有很多指向它的关系的节点

[英]Finding Nodes with lots of relations pointing to it in neo4j using cypher

i have a neo4j database with the nodes following this structure 我有一个neo4j数据库,其节点遵循此结构

[a:article_id] -[r:about_place]-> [l:location]

now i want to find article_id,location pairs where location has lots of incoming relationships (say > 4) 现在我想找到article_id,位置对,其中位置具有很多传入关系(例如> 4)

MATCH ()-[r:about_place]->(n)
WITH n,count(r) as rel_cnt
where rel_cnt > 4
RETURN n.name,rel_cnt; 

this works, i get the list of locations as i need. 这有效,我可以根据需要获取位置列表。

查询答案列表

but i now want all the incomings articles from the relation also, like what the 5 article ids that china has pointing to it are. 但是我现在也希望从该关系中获取所有传入的文章,例如中国所指的5个文章ID。

something like this, 像这样

MATCH (a)-[r:about_place]->(n)
WITH a,n,count(r) as rel_cnt
where rel_cnt > 4
RETURN a.title,n.name,rel_cnt;

but this returns 0 rows. 但这将返回0行。 im guessing because now the (a,n) combo is used in the group clause which makes count(r) always be 1 in each row. 我猜是因为现在(a,n)组合在group子句中使用,这使得count(r)在每一行中始终为1。 i saw in a talk that this was the way the count(*) clause works by default. 我在一次谈话中看到,这是count(*)子句默认工作的方式。

i think a solution would be to chain these results and make a new query but for the life of me i cant figure out how. 我认为一种解决方案是将这些结果链接起来并进行新的查询,但是对于我来说,我不知道该如何做。 any ideas or links would help too. 任何想法或链接也会有所帮助。

I'm not sure if there's a better way than this: 我不确定是否有比这更好的方法:

MATCH ()-[r:about_place]->(n)
WITH n, count(r) as rel_cnt
WHERE rel_cnt > 4
MATCH (a)-[r:about_place]->(n)
RETURN a.title,n.name,rel_cnt;

Also, unsolicited notes: 另外,请注意:

  • You might want to use the label in your query (like MATCH ()-[r:about_place]->(n:location) ) for better performance 您可能想在查询中使用标签(例如MATCH ()-[r:about_place]->(n:location) )以获得更好的性能
  • Neo4j convention has labels in CamelCase Neo4j Convention在CamelCase中带有标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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