简体   繁体   中英

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

i have a neo4j database with the nodes following this structure

[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)

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.

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. im guessing because now the (a,n) combo is used in the group clause which makes count(r) always be 1 in each row. i saw in a talk that this was the way the count(*) clause works by default.

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
  • Neo4j convention has labels in CamelCase

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