Count node depth in neo4j

浪子不回头ぞ 提交于 2019-12-24 03:06:22

问题


I have this query in Neo4j:

MATCH (sentence:Sentence)-[r*]->(n:Word )
WITH n, COUNT(r) AS c
RETURN n, c

My graph is a linguistic database containing words and dependency relations between them. This query should return depth of nodes, however the COUNT(r) always returns 1. When I ommit the COUNT function and write just

WITH n, r AS c

instead (trying in web browser neo4j interface), neo4j returns multiple relations for each word node "n" as expected. Can you please help me what am I doing wrong, how to count the length of path between sentence node and word node? thanks.


回答1:


I think it query return n and c and there are multiple record of n so count(r) return 1.

Try this -

MATCH (sentence:Sentence)-[r*]->(n:Word )
WITH n, LENGTH(r) AS depth
RETURN n, depth

You will get depth like this.

Or Try this

   MATCH p= (sentence:Sentence)-->(n:Word)    
   RETURN n, length(p) as depth

http://docs.neo4j.org/chunked/stable/query-functions-scalar.html#functions-length




回答2:


Finally found the solution myself - it is cypher's LENGTH function:

MATCH (sentence:Sentence)-[r*]->(n:Word )
WITH n, LENGTH(r) AS c
RETURN n, c

found in this useful cheat sheet: http://assets.neo4j.org/download/Neo4j_CheatSheet_v3.pdf



来源:https://stackoverflow.com/questions/26089984/count-node-depth-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!