Search code examples
neo4jcypherfull-text-searchneo4j-apoc

Neo4j fulltext search: count number of results and return limited subset


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:

  1. Will the aggregation count(*) as countResults consume all rows from db.index.fulltext.queryNodes() and therefore, cause a build up in memory of all results?
  2. Would it be better to use the APOC function apoc.agg.slice() instead of collect()[0..10]?

Solution

  • 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.