Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
2c810e9
fix(docs): update version (#331)
skrdgraph Oct 20, 2022
4ba1c95
[Docs] Access Control Lists : add the command and random keys (#333)
Rajakavitha1 Oct 27, 2022
4096ae4
[Docs] Encryption at Rest: added a note for macOS (#334)
Rajakavitha1 Oct 27, 2022
d01e2ed
docs(release): Dgraph v22.0.0 release (#335)
MichelDiz Oct 31, 2022
b05f66c
Cherry pick/v22.0/glossary (#364)
rderbier Dec 14, 2022
004bba5
Cherry pick/v22.0/glossary update (#387)
rderbier Jan 13, 2023
5e9f635
Merge branch 'release/v22.0'
rderbier Jan 17, 2023
3fa1a02
Revamp(install) : merge to v22.0 (#409)
rderbier Jan 18, 2023
ce08046
change build script for branches (#425)
rderbier Jan 27, 2023
84c8c49
Algolia/v22.0 (#427)
rderbier Jan 30, 2023
3804b5c
Algolia/v22.0 (#429)
rderbier Jan 31, 2023
b91145a
Algolia/v22.0 (#430)
rderbier Jan 31, 2023
fae5fb3
Algolia/v22.0 (#431)
rderbier Jan 31, 2023
43b6bbc
remove graphql overview and references (#433)
rderbier Feb 1, 2023
6cda032
remove graphql overview and references (#434)
rderbier Feb 1, 2023
b95fd77
add v22.0.2 (#435)
rderbier Feb 1, 2023
97665b4
Revamp/import data (#437)
rderbier Feb 1, 2023
d43480f
Dgraph overview diagram (#438)
rderbier Feb 1, 2023
034c80b
upgrade hugo version to 0.91 (#443)
rderbier Feb 2, 2023
568aaa9
Docs(Lambda): Fix incorrect environment variable example (#449)
matthewmcneely Feb 10, 2023
15c3c6f
move type-system (#452)
rderbier Feb 13, 2023
68de9f6
[REVAMP]: move predicate types (ex schema) to DQL section (#454)
rderbier Feb 14, 2023
5a3f415
Revamp/howto drop data (#451)
rderbier Feb 14, 2023
a9ee580
Revamp/mutation syntax (#459)
rderbier Feb 14, 2023
399de45
revamp doc home (#463)
rderbier Feb 14, 2023
84068c6
Delete .hugo_build.lock
rderbier Feb 14, 2023
e992f39
ignore lock files
rderbier Feb 14, 2023
86d1133
graphql quickstart (#465)
rderbier Feb 15, 2023
4717578
avoid conflicts with cloud doc urls
rderbier Feb 15, 2023
a9c8f4c
re-organize graphql section (#466)
rderbier Feb 15, 2023
bc84b61
correct api ref
rderbier Feb 15, 2023
67a437d
move cloud pages in this repo (#468)
rderbier Feb 16, 2023
e8dbca7
move cloud folder
rderbier Feb 16, 2023
4071053
gqlschema section update (#471)
rderbier Feb 16, 2023
2a60b83
Split info from the predicate page
rderbier Feb 16, 2023
9429b2f
Merge branch 'main' into revamp/predicates
rderbier Feb 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions content/dql/predicate-indexing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
+++
date = "2017-03-20T22:25:17+11:00"
title = "Predicate indexing"
weight = 4
[menu.main]
parent = "dql"
+++

Filtering on a predicate by applying a [function]({{< relref "query-language/functions.md" >}}) requires an index.

When filtering by applying a function, Dgraph uses the index to make the search through a potentially large dataset efficient.

All scalar types can be indexed.

Types `int`, `float`, `bool` and `geo` have only a default index each: with tokenizers named `int`, `float`, `bool` and `geo`.

Types `string` and `dateTime` have a number of indices.

## String Indices
The indices available for strings are as follows.

| Dgraph function | Required index / tokenizer | Notes |
| :----------------------- | :------------ | :--- |
| `eq` | `hash`, `exact`, `term`, or `fulltext` | The most performant index for `eq` is `hash`. Only use `term` or `fulltext` if you also require term or full-text search. If you're already using `term`, there is no need to use `hash` or `exact` as well. |
| `le`, `ge`, `lt`, `gt` | `exact` | Allows faster sorting. |
| `allofterms`, `anyofterms` | `term` | Allows searching by a term in a sentence. |
| `alloftext`, `anyoftext` | `fulltext` | Matching with language specific stemming and stopwords. |
| `regexp` | `trigram` | Regular expression matching. Can also be used for equality checking. |

{{% notice "warning" %}}
Incorrect index choice can impose performance penalties and an increased
transaction conflict rate. Use only the minimum number of and simplest indexes
that your application needs.
{{% /notice %}}

## DateTime Indices

The indices available for `dateTime` are as follows.

| Index name / Tokenizer | Part of date indexed |
| :----------- | :------------------------------------------------------------------ |
| `year` | index on year (default) |
| `month` | index on year and month |
| `day` | index on year, month and day |
| `hour` | index on year, month, day and hour |

The choices of `dateTime` index allow selecting the precision of the index. Applications, such as the movies examples in these docs, that require searching over dates but have relatively few nodes per year may prefer the `year` tokenizer; applications that are dependent on fine grained date searches, such as real-time sensor readings, may prefer the `hour` index.


All the `dateTime` indices are sortable.


## Sortable Indices

Not all the indices establish a total order among the values that they index. Sortable indices allow inequality functions and sorting.

* Indexes `int` and `float` are sortable.
* `string` index `exact` is sortable.
* All `dateTime` indices are sortable.

For example, given an edge `name` of `string` type, to sort by `name` or perform inequality filtering on names, the `exact` index must have been specified. In which case a schema query would return at least the following tokenizers.

```
{
"predicate": "name",
"type": "string",
"index": true,
"tokenizer": [
"exact"
]
}
```

## Count index

For predicates with the `@count` Dgraph indexes the number of edges out of each node. This enables fast queries of the form:
```
{
q(func: gt(count(pred), threshold)) {
...
}
}
```

## List Type

Predicate with scalar types can also store a list of values if specified in the schema. The scalar
type needs to be enclosed within `[]` to indicate that its a list type.

```
occupations: [string] .
score: [int] .
```

* A set operation adds to the list of values. The order of the stored values is non-deterministic.
* A delete operation deletes the value from the list.
* Querying for these predicates would return the list in an array.
* Indexes can be applied on predicates which have a list type and you can use [Functions]({{<relref "query-language/functions.md">}}) on them.
* Sorting is not allowed using these predicates.
* These lists are like an unordered set. For example: `["e1", "e1", "e2"]` may get stored as `["e2", "e1"]`, i.e., duplicate values will not be stored and order may not be preserved.

## Filtering on list

Dgraph supports filtering based on the list.
Filtering works similarly to how it works on edges and has the same available functions.

For example, `@filter(eq(occupations, "Teacher"))` at the root of the query or the
parent edge will display all the occupations from a list of each node in an array but
will only include nodes which have `Teacher` as one of the occupations. However, filtering
on value edge is not supported.

## Reverse Edges

A graph edge is unidirectional. For node-node edges, sometimes modeling requires reverse edges. If only some subject-predicate-object triples have a reverse, these must be manually added. But if a predicate always has a reverse, Dgraph computes the reverse edges if `@reverse` is specified in the schema.

The reverse edge of `anEdge` is `~anEdge`.

For existing data, Dgraph computes all reverse edges. For data added after the schema mutation, Dgraph computes and stores the reverse edge for each added triple.

```
type Person {
name string
}
type Car {
regnbr string
owner Person
}
owner uid @reverse .
regnbr string @index(exact) .
name string @index(exact) .
```

This makes it possible to query Persons and their cars by using:
```
q(func type(Person)) {
name
~owner { name }
}
```
To get a different key than `~owner` in the result, the query can be written with the wanted label
(`cars` in this case):

```
q(func type(Person)) {
name
cars: ~owner { name }
}
```

This also works if there are multiple "owners" of a `car`:
```
owner [uid] @reverse .
```

In both cases the `owner` edge should be set on the `Car`:
```
_:p1 <name> "Mary" .
_:p1 <dgraph.type> "Person" .
_:c1 <regnbr> "ABC123" .
_:c1 <dgraph.type> "Car" .
_:c1 <owner> _:p1
```

## Querying Schema

A schema query queries for the whole schema:

```
schema {}
```

{{% notice "note" %}} Unlike regular queries, the schema query is not surrounded
by curly braces. Also, schema queries and regular queries cannot be combined.
{{% /notice %}}

You can query for particular schema fields in the query body.

```
schema {
type
index
reverse
tokenizer
list
count
upsert
lang
}
```

You can also query for particular predicates:

```
schema(pred: [name, friend]) {
type
index
reverse
tokenizer
list
count
upsert
lang
}
```

{{% notice "note" %}} If ACL is enabled, then the schema query returns only the
predicates for which the logged-in ACL user has read access. {{% /notice %}}

Types can also be queried. Below are some example queries.

```
schema(type: Movie) {}
schema(type: [Person, Animal]) {}
```

Note that type queries do not contain anything between the curly braces. The
output will be the entire definition of the requested types.
Loading