I want to count the number of results possibly returned by fulltext search, but return only a limited subset of these results. This query will return only 10 nodes and the number of all results:
CALL db.index.fulltext.queryNodes($indexName, $searchTerm) YIELD node, score
WITH count(*) as countResults, collect({node: node, score: score})[0..10] as nodes
RETURN nodes, countResults
I have two questions regarding this query:
count(*) as countResults
consume all rows from db.index.fulltext.queryNodes()
and therefore, cause a build up in memory of all results?collect()[0..10]
?TO answer your questions; 1) yes, it will count the number of rows in the full text search; depending on the number of rows to be returned, it may or may not cause an issue on your memory. 2) yes, apoc.agg.slice() is better than collect()[0..10] because slice will only accumulate n results that you put rather than collecting ALL rows then getting the first 10 items.