When using the count index at the query root, I regularly need to get values lower than a threshold, including zero.
For instance, suppose I have products connected to suppliers and I’d like to known when there are not enough suppliers. I could use the following query:
But I will miss the products with 0 suppliers: the most important ones to me.
I could use a second query block with a filter not has(supplier) but it is no efficient as not using any index.
What about adding an option on @count index to also track 0 counts?
It could be tracked only for the types containing the predicates with those indexes.
For example :
type Product {
supplier
}
supplier: [uid] @count(zero) .
As you can see we don’t support/track negative counts (nonsensical) or zero counts (not tracked).
{
q(func: eq(count(friend), 0)) {
count(uid)
name
}
}
Result
Error Name: t
Message: : count(predicate) cannot be used to search for negative counts (nonsensical) or zero counts (not tracked).
URL: http://localhost:8080/query?timeout=60s&debug=true
Raw Error:
${
"name": "t",
"url": "http://localhost:8080/query?timeout=60s&debug=true",
"errors": [
{
"message": ": count(predicate) cannot be used to search for negative counts (nonsensical) or zero counts (not tracked).",
"extensions": {
"code": "ErrorInvalidRequest"
}
}
]
}
Other way
You can have a Type and then filter it by count. It works.
{
q(func: type(Person)) @filter(eq(count(friend), 0)) {
count(uid)
name
}
}
{
q(func: has(name)) @filter(eq(count(friend), 0)) {
count(uid)
name
}
}
In terms of performances, does it mean that dgraph will first retrieve all persons and then intersect with those without friends?
Is it really efficient?
You can try to cheat this. Create a “fake” entity (as in my example, “a fake friend”) and then you will have all your nodes with at least 1 value. Hence you disregard this value and treat it as “Zero”. And in every query you have, you use “math” func to subtract that extra entity.
Hey guys, does similar filter functionality exist on the GraphQL endpoint yet? I need to retrieve all values that have no connections on a specific predicate.