We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
- Modeling tweets in Dgraph.
- Using String indices in Dgraph
- Querying twitter users using the
hash
index. - Comparing strings using the
exact
index. - Searching for tweets based on keywords using the
term
index.
- Querying twitter users using the
Modeling a tweet in Dgraph
Here’s a sample tweet.tweet for the fifth episode of getting started series with @dgraphlabs.
-
The Author
The author of the tweet is the user
@hackintoshrao
. -
The Body
This component is the content of the tweet.
Test tweet for the fifth episode of getting started series with @dgraphlabs. Wait for the video of the fourth one by @francesc the coming Wednesday! #GraphDB #GraphQL
-
The Hashtags
Here are the hashtags in the tweet:
#GraphQL
and#GraphDB
. -
The Mentions
A tweet can mention other twitter users.
Here are the mentions in the tweet above:
@dgraphlabs
and@francesc
.
With the above design principles in mind, let’s go through components of a tweet and see how we could fit them into Dgraph. The Author The Author of a tweet is a twitter user. We should use a node to represent this. The Body We should represent every tweet as a node. The Hashtags It is advantageous to represent a hashtag as a node of its own. It gives us better flexibility while querying. Though you can search for hashtags from the body of a tweet, it’s not efficient to do so. Creating unique nodes to represent a hashtag, allows you to write performant queries like the following: Hey Dgraph, give me all the tweets with hashtag #graphql The Mentions A mention represents a twitter user, and we’ve already modeled a user as a node. Therefore, we represent a mention as an edge between a tweet and the users mentioned.Nodes
andEdges
are the building blocks of a graph model. May it be a sale, a tweet, user info, any concept or an entity is represented as a node. If any two nodes are related, represent that by creating an edge between them.
The Relationships
We have three types of nodes:User
, Tweet,
and Hashtag
.

Tweet
and a User
node.
- Every tweet is authored by a user, and a user can author many tweets.
authored
.
An authored
edge points from a User
node to a Tweet
node.
- A tweet can mention many users, and users can be mentioned in many tweets.
mentioned
.
A mentioned
edge points from a Tweet
node to a User
node. These users are
the ones who are mentioned in the tweet.

tagged_with
.
A tagged_with
edge points from a Tweet
node to a Hashtag
node. These
hashtag nodes correspond to the hashtags in the tweets.



If you’re new to Dgraph, and yet to figure out how to run the database and use
Ratel, we highly recommend reading the first article of the
series

- Five blue twitter user nodes.
- The green nodes are the tweets.
- The blue ones are the hashtags.

@hackintoshrao
, @francesc
, @dgraphlabs
, and
@gopherpalooza
.
Now, let’s find their tweets and hashtags too.

What are indices?
Indexing is a way to optimize the performance of a database by minimizing the number of disk accesses required when a query is processed. Consider a “Book” of 600 pages, divided into 30 sections. Let’s say each section has a different number of pages in it. Now, without an index page, to find a particular section that starts with the letter “F”, you have no other option than scanning through the entire book. i.e: 600 pages. But with an index page at the beginning makes it easier to access the intended information. You just need to look over the index page, after finding the matching index, you can efficiently jump to the section by skipping other sections. But remember that the index page also takes disk space! Use them only when necessary. In our next section,let’s learn some interesting queries on our twitter graph.String indices and querying
Hash index
Let’s compose a query which says: Hey Dgraph, find me the tweets of user with twitter handle equals tohackintoshrao
.
Before we do so, we need first to add an index has to the user_handle
predicate. We know that there are 5 types of string indices: hash
, exact
,
term
, full-text
, and trigram
.
The type of string index to be used depends on the kind of queries you want to
run on the string predicate.
In this case, we want to search for a node based on the exact string value of a
predicate. For a use case like this one, the hash
index is recommended.
Let’s first add the hash
index to the user_handle
predicate.

eq
comparator to find all the tweets of hackintoshrao
.
Go to the query tab, type in the query, and click Run.

eq
in detail.
Let’s extend the last query also to fetch the hashtags and the mentions.

equals to (eq)
on the string predicates.
Exact Index
We discussed in the third tutorial that there five comparator functions in Dgraph. Here’s a quick recap:comparator function name | Full form |
---|---|
eq | equals to |
lt | less than |
le | less than or equal to |
gt | greater than |
ge | greater than or equal to |
eq
operator. The other four are useful for
operations, which depend on the alphabetical ordering of the strings.
Let’s learn about it with a simple example.
Let’s find the twitter accounts which come after dgraphlabs
in alphabetically
sorted order.

hash
index on the user_handle
predicate doesn’t support the gt
function.
To be able to do string comparison operations like the one above, you need first
set the exact
index on the string predicate.
The exact
index is the only string index that allows you to use the ge
,
gt
, le
, lt
comparators on the string predicates.
Remind you that the exact
index also allows you to use equals to (eq)
comparator. But, if you want to just use the equals to (eq)
comparator on
string predicates, using the exact
index would be an overkill. The hash
index would be a better option, as it’s, in general, much more space-efficient.
Let’s see the exact
index in action.

hash
and the exact
indices.
The user_handle
predicate already has the hash
index, so trying to set the
exact
index gives you an error.
Let’s uncheck the hash
index for the user_handle
predicate, select the
exact
index, and click update.


francesc
, gopherpalooza
, and
hackintoshrao
.
In the alphabetically sorted order, these twitter handles are greater than
dgraphlabs
.
Some tweets appeal to us better than others. For instance, I love Graphs
and
Go
. Hence, I would surely enjoy tweets that are related to these topics. A
keyword-based search is a useful way to find relevant information.
Can we search for tweets based on one or more keywords related to your
interests?
Yes, we can! Let’s do that in our next section.
The Term index
Theterm
index lets you search string predicates based on one or more
keywords. These keywords are called terms.
To be able to search tweets with specific keywords or terms, we need to first
set the term
index on the tweets.
Adding the term
index is similar to adding any other string index.

allofterms
and anyofterms
.
Apart from these two functions, the term
index only supports the eq
comparator. This means any other query functions (like lt, gt, le…) fails when
run on string predicates with the term
index.
We’ll soon take a look at the table containing the string indices and their
supporting query functions. But first, let’s learn how to use anyofterms
and
allofterms
query functions. Let’s write a query to find all tweets with terms
or keywords Go
or Graph
in them.
Go the query tab, paste the query, and click Run.

anyofterms
function returns tweets which have either of Go
or Graph
keyword.
In this case, we’ve used only two terms to search for (Go
and Graph
), but
you can extend for any number of terms to be searched or matched.
The result has one of the three tweets in the database. The other two tweets
don’t make it to the result since they don’t have either of the terms Go
or
Graph
.
It is also important to notice that the term search functions (anyofterms
and
allofterms
) are insensitive to case and special characters.
This means, if you search for the term GraphQL
, the query returns a positive
match for all of the following terms found in the tweets: graphql
, graphQL
,
#graphql
, #GraphQL
.
Now, let’s find tweets that have either of the terms Go
or GraphQL
in them.

Go
or GraphQL
.
Now, how about finding tweets that contain both the terms Go
and GraphQL
in
them. We can do it by using the allofterms
function.

Go
and
GraphQL
in them.
Besides Go
and Graph
, I’m also a big fan of GraphQL
and GraphDB
.
Let’s find out tweets that contain both the keywords GraphQL
and GraphDB
in
them.

GraphQL
and GraphDB
.
Index | Valid query functions |
---|---|
hash | eq |
exact | eq, lt, gt, le, ge |
term | eq, allofterms, anyofterms |
Summary
In this tutorial, we modeled a series of tweets and set up the exact, term, and hash indices in order to query them. Did you know that Dgraph also offers more powerful search capabilities like full-text search and regular expressions based search? In the next tutorial, we’ll explore these features and learn about more powerful ways of searching for your favorite tweets! Sounds interesting? Then see you all soon in the next tutorial. Till then, happy Graphing! Check out our next tutorial of the getting started series here.Need Help
- Please use discuss.hypermode.com for questions, feature requests, bugs, and discussions.