When is the @count index actually needed in Dgraph?

I’m trying to better understand the use case for the @count index in Dgraph.

From what I gather, it’s only necessary when we need to filter or sort nodes based on the number of values they have for a specific predicate — for example, how many friends a node has. In that case, we’d write something like:

{
  q(func: has(friends)) @filter(ge(count(friends), 3)) {
    uid
  }
}

But in a query like this:

{
  var(func: type(someType)) @filter(has(sth)) {
    someuid as uid  
  }

  count_num(func: uid(someuid)) @filter(type(someType)) {
    count_num: count(uid)
  }
}

We’re simply counting the number of nodes matching a condition, not counting predicate values. So I assume the @count index is not required here.

Is this understanding correct?

Thanks!

That’s correct. Note that the @count directive would also be needed for “root functions” that use count, e.g.:

		user(func:eq(count(friend),4)) {
			name
		}

In your second example, the set of UIDs is already collected in the search so no @count predicate is required.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.