Skip to main content

Filters

In this section we'll learn how to filter the root query and its nested edges.
You'll also find all the available definitions of dqlx functions

Root Filter#

The first constraint we need to provide to our query is a single filter to help Dgraph minimise the amount of data we want to query against. We can achieve data using any functions in dqlx that ends in Fn

db.Query(dqlx.HasFn("name")) // rootQuery(func: has(name))
db.Query(dqlx.TypeFn("Animal")) // rootQuery(func: type(Animal))
db.Query(dqlx.EqFn("name", "Ollie")) // rootQuery(func: eq(name, $0))
// Aliases
db.QueryType("Animal") // rootQuery(func: type(Animal))
db.QueryHas("name") // rootQuery(func: has(name))

Query Filters#

Subsequently, we can apply other filters to our query, in order to narrow down the exact data we are after. We do that using the Filter() function

db.Query(dqlx.HasFn("name")).
Filter(
dqlx.EqFn("name", "Ollie"),
dqlx.GtFn("age", 3),
).
Filter(dqlx.EqFn("animal", "Cat"))

You can either add filters within a single Filter() call, or chain multiple Filter calls, the result is the same. All the above filters will be concatenated together with an AND condition

Sugars on Filters#

Instead of using the Fn functions within our Filter we can use the equivalent of that function as a Map.

db.Query(dqlx.HasFn("name")).
Filter(
dqlx.Eq{"name": "Ollie", "animal": "Cat"},
dqlx.Gt{"age": 2},
)

This makes it more elegant to express the same filter type for different fields

Filtering Connections#

So far the filters we've seen till now are always concatenated with an AND condition. In order to conjunct the filters together with different conditions such as OR we do the following.

db.Query(dqlx.HasFn("name")).
Filter(
dql.Or{
dql.And{
dqlx.Eq{"name": "Ollie", "animal": "Cat"},
dqlx.Gt{"age": 2},
},
dql.And{
dqlx.Eq{"name": "Leo", "animal": "Cat"},
dqlx.Gt{"age": 3},
},
},
)

which will translate in:

query RootQuery($0: string,$1: string,$3: int, $4: string, $5: string, $6: int) {
rootQuery(func: has(name)) @filter( (eq(name,$0) AND eq(animal,$1) AND gt(age, $3)) OR (eq(name,$4) AND eq(animal,$5) AND gt(age, $6))) {
}
}

Filtering Edges#

Filters on edges works the same way, just add the filter functions to your edge

db.Query(dqlx.HasFn("name")).
Filter(
dqlx.Eq{"name": "Ollie", "animal": "Cat"},
dqlx.Gt{"age": 2},
).
Edge("favorite_food", dqlx.Eq{"brand": "Wishcask"})

Functions#

Has#

Has function: HasFn(predicate)
Dgraph Doc

dqlx.HasFn("name") // as Function
dqlx.Has("name") // as Function (alias)

Type#

Has function: HasFn(predicate)
Dgraph Doc

dqlx.TypeFn("name") // as Function
dqlx.Type("name") // as Function (alias)

Between#

Between function: BetweenFn(predicate, from, to)
Dgraph Doc

dqlx.BetweenFn("age", 1, 30) // as Function
dqlx.Between("age", 1, 30) // as Function (alias)

UIDIn#

UID function: UIDFn(predicate)
Dgraph Doc

dqlx.UIDIn{"name": []string{"Ollie", "Leo"}} // as Map
dqlx.UIDInFn("name", []string{"Ollie", "Leo"}) // as Function

UID#

UID function: UID(predicate, vlaues)
Dgraph Doc

dqlx.UIDFn("name") // as Function
dqlx.UID("name") // as Function (alias)

Regexp#

Regexp function: RegexpFn(predicate, pattern)
Dgraph Doc

dqlx.Regexp{"name": "/pattern/"} // as Map
dqlx.RegexpFn("name", "/pattern/") // as Function

Eq#

Eq function: EqFn(predicate, value)
Dgraph Doc

dqlx.Eq{ "name": "value" } // as Map
dqlx.EqFn("name", "value") // as Function

Ge#

Ge function: GeFn(predicate, value)
Dgraph Doc

dqlx.Ge{ "name": "value" } // as Map
dqlx.GeFn("name", "value") // as Function

Gt#

Gt function: GtFn(predicate, value)
Dgraph Doc

dqlx.Gt{ "name": "value" } // as Map
dqlx.GtFn("name", "value") // as Function

Le#

Le function: LeFn(predicate, value)
Dgraph Doc

dqlx.Le{ "name": "value" } // as Map
dqlx.LeFn("name", "value") // as Function

Lt#

Lt function: LtFn(predicate, value)
Dgraph Doc

dqlx.Lt{ "name": "value" } // as Map
dqlx.LtFn("name", "value") // as Function

Allofterms#

Allofterms function: AlloftermsFn(predicate, value)
Dgraph Doc

dqlx.Allofterms{ "name": "value" } // as Map
dqlx.AlloftermsFn("name", "value") // as Function

Anyofterms#

Anyofterms function: Anyofterms(predicate, value)
Dgraph Doc

dqlx.Anyofterms{ "name": "value" } // as Map
dqlx.AnyoftermsFn("name", "value") // as Function

Alloftext#

Alloftext function: Alloftext(predicate, value)
Dgraph Doc

dqlx.Alloftext{ "name": "value" } // as Map
dqlx.AlloftextFn("name", "value") // as Function

Anyoftext#

Anyoftext function: Anyoftext(predicate, value)
Dgraph Doc

dqlx.Anyoftext{ "name": "value" } // as Map
dqlx.AnyoftextFn("name", "value") // as Function

Match#

Match function: Match(predicate, value)
Dgraph Doc

dqlx.Match{ "name": "value" } // as Map
dqlx.MatchFn("name", "value") // as Function

Term#

Term function: Term(predicate, value)
Dgraph Doc

dqlx.Term{ "name": "value" } // as Map
dqlx.TermFn("name", "value") // as Function

Exact#

Exact function: Exact(predicate, value)
Dgraph Doc

dqlx.Exact{ "name": "value" } // as Map
dqlx.ExactFn("name", "value") // as Function

Fulltext#

Fulltext function: Fulltext(predicate, value)
Dgraph Doc

dqlx.Fulltext{ "name": "value" } // as Map
dqlx.FulltextFn("name", "value") // as Function

Expr#

Expr function: Expr(predicate)

The Expr allows you to write Raw statement as the value, the variable will not be escaped.

dqlx.Eq{
"name": dqlx.Expr("count(animals)"),
}