@filter
The @filter directive allows you to apply additional filtering conditions to nodes in a query block. Filters use functions to test node attributes or relationships and can be applied to both root query blocks and nested blocks.
Using @filter
In Query Blocks
A query block may have a combination of filters to apply to the root nodes. The @filter directive appears after the func: criteria and before the opening curly bracket:
{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film {
name@en
}
}
}
In Nested Blocks
For relationships to fetch, nested blocks may specify filters to apply on the related nodes:
{
director(func: eq(name@en, "Steven Spielberg")) {
name@en
director.film @filter(allofterms(name@en, "indiana jones")) {
uid
name@en
}
}
}
Nested blocks may also specify criteria on the relationships attributes using filtering on facets.
Filter Functions
Filters use the same functions that are available for root criteria. These functions can test:
- String attributes: term matching, regular expressions, fuzzy matching, full-text search
- Attribute values: equality, inequalities, ranges
- Node properties: predicate existence, UID, relationships, node type
- Relationship counts: equality and inequality comparisons
- Geolocation attributes: proximity, containment, intersection
Common functions include:
- String matching: allofterms, anyofterms, regexp, match, alloftext
- Value comparisons: eq, le, lt, ge, gt, between
- Node tests: has, uid, uid_in,
type() - Geolocation: near, within, contains, intersects
Variables may be used as function parameters in filters. See query variables and value variables for more information.
Filters can also be combined with directives like @cascade to create pattern matching queries where only nodes matching the complete query structure are returned.
Connecting Filters
Within @filter multiple functions can be used with boolean operators AND, OR, and NOT.
AND, OR and NOT
Connectives AND, OR and NOT join filters and can be built into arbitrarily complex filters, such as (NOT A OR B) AND (C AND NOT (D OR E)). Note that, NOT binds more tightly than AND which binds more tightly than OR.
Query Example: All Steven Spielberg movies that contain either both "indiana" and "jones" OR both "jurassic" and "park".
- Query
- Go
- Java
- Python
- JavaScript (gRPC)
- JavaScript (HTTP)
- Curl
{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"google.golang.org/grpc"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
)
func main() {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer txn.Discard(ctx)
query := `{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}`
resp, err := txn.Query(ctx, query)
if err != nil {
log.Fatal(err)
}
var result map[string]interface{}
json.Unmarshal(resp.Json, &result)
fmt.Printf("%+v\n", result)
}
import io.dgraph.DgraphClient;
import io.dgraph.DgraphGrpc;
import io.dgraph.DgraphProto;
import io.dgraph.Transaction;
import java.util.Map;
public class App {
public static void main(String[] args) {
ManagedChannel channel = ManagedChannelBuilder
.forAddress("localhost", 9080)
.usePlaintext()
.build();
DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
DgraphClient dgraphClient = new DgraphClient(stub);
String query = "{
me(func: eq(name@en, \"Steven Spielberg\")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, \"jones indiana\") OR allofterms(name@en, \"jurassic park\")) {
uid
name@en
}
}
}";
Transaction txn = dgraphClient.newTransaction();
try {
DgraphProto.Response response = txn.query(query);
System.out.println(response.getJson().toStringUtf8());
} finally {
txn.discard();
}
}
}
import grpc
from dgraph import DgraphClient, Txn
def main():
client_stub = DgraphClient("localhost:9080")
client = DgraphClient(client_stub)
query = """{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}"""
txn = client.txn()
try:
response = txn.query(query)
print(response.json)
finally:
txn.discard()
if __name__ == "__main__":
main()
const dgraph = require("dgraph-js");
const grpc = require("grpc");
async function main() {
const clientStub = new dgraph.DgraphClientStub(
"localhost:9080",
grpc.credentials.createInsecure()
);
const dgraphClient = new dgraph.DgraphClient(clientStub);
const query = `{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}`;
const txn = dgraphClient.newTxn();
try {
const res = await txn.query(query);
const json = res.getJson();
console.log(JSON.stringify(JSON.parse(json), null, 2));
} finally {
await txn.discard();
}
}
main().catch((e) => {
console.error(e);
});
const fetch = require("node-fetch");
async function main() {
const query = `{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}`;
const response = await fetch("http://localhost:8080/query", {
method: "POST",
headers: {
"Content-Type": "application/dql"
},
body: query
});
const result = await response.json();
console.log(JSON.stringify(result, null, 2));
}
main().catch((e) => {
console.error(e);
});
curl -X POST http://localhost:8080/query \
-H "Content-Type: application/dql" \
-d '{
me(func: eq(name@en, "Steven Spielberg")) @filter(has(director.film)) {
name@en
director.film @filter(allofterms(name@en, "jones indiana") OR allofterms(name@en, "jurassic park")) {
uid
name@en
}
}
}'