Cypher returns the final node score, as well as a limited set

Is it possible to extract a limited set of nodes and the total number of nodes in a single cypher request?

match (n:Molecule) with n, count(*) as nb limit 10 return {N: nb, nodes: collect(n)} 

The above query returns nodes correctly, but returns 1 as the number of nodes. Of course, I understand why it returns 1, since there is no grouping, but cannot figure out how to fix it.

+5
source share
2 answers

The following query returns a counter for an integer number of rows (which, I think, is what was needed). Then it matches again and limits your search, but the original counter is still available as it is carried over via WITH -statement.

 MATCH (n:Molecule) WITH count(*) AS cnt MATCH (n:Molecule) WITH n, cnt LIMIT 10 RETURN { N: cnt, nodes:collect(n) } AS molecules 
+8
source

Here is an alternative solution:

 match (n:Molecule) return {nodes: collect(n)[0..5], n: length(collect(n))} 

84 ms for 30k nodes, shorter, but not as effective as the ones suggested bysgren above.

+1
source

Source: https://habr.com/ru/post/1210501/


All Articles