hash
index.exact
index.term
index.@hackintoshrao
.
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
#GraphQL
and #GraphDB
.
@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.
User
, Tweet,
and Hashtag
.
Tweet
and a User
node.
authored
.
An authored
edge points from a User
node to a Tweet
node.
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.
@hackintoshrao
, @francesc
, @dgraphlabs
, and
@gopherpalooza
.
Now, letโs find their tweets and hashtags too.
hackintoshrao
.
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.
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.
term
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 |