amik
amik

Reputation: 5919

Count node depth in neo4j

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.

Upvotes: 2

Views: 3009

Answers (3)

hetal
hetal

Reputation: 399

In version 4.x, U should use SIZE function

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

https://neo4j.com/docs/cypher-manual/current/functions/scalar/#functions-size

Upvotes: 1

Satish Shinde
Satish Shinde

Reputation: 2996

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

Upvotes: 4

amik
amik

Reputation: 5919

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

Upvotes: 0

Related Questions