Dgraph GraphQL Tour

Basic

How Dgraph Search Works

Given what you’ve seen so far, you’ve probably already understood this, but it’s worth going over.

The graphs in Dgraph can be huge, so starting your search from all nodes isn’t always efficient. Dgraph needs a place to start searching, and you choose this by starting your search from a root query function.

For example, you can use the root query functions with names like query<type> or get<type> to find an initial set of nodes. So far, you have mainly used the getPerson root query function in this tour operations, but you can also search through all of the nodes having the type ‘Person’ by using queryPerson.

Dgraph relies on the @search directive in the schema to generate filter arguments and build an index on values that are to be searched. Without these @search directives, Dgraph would not know to generate the filter arguments or create an index to make the search more efficient. Without an index, Dgraph would have to trawl through all of the nodes of a type to find matching values, which would impact query efficiency.

From the set of nodes matched in the root query function, Dgraph then follows predicates and edges to satisfy the remainder of the query. The filters on blocks inside the root are only applied to the nodes reached by following listed edges to them.

In the example we have provided, the query searches for all people in their 30’s, and then finds their friends that have an xid equal to ‘bob’. Given our small dataset and the simple search parameters, it may not be very evident, but if the dataset was very large we would see the efficiency of only searching for friends of people in their 30’s as the first step, instead of searching for all people in the dataset with both steps.

2.10 How Dgraph Search Works