2
votes

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.

2
Can you describe your Neo4j data? so that we can figure it out whats wrong there. - Satish Shinde
See my own answer below. The data is just a forest of sentence trees as I said, I needed to calculate distance of a node (word in sentence) from root. The solution is LENGTH(r), not COUNT(r). - amik
thanks for your comment. - Satish Shinde

2 Answers

3
votes

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

0
votes

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