Sign in to Hypermode and create your workspace
Openhypermode.com/login
in your web browser and sign in to Hypermode to create your account.After signing in you’ll be prompted to choose a name for your workspace.Enter a name for your workspace and then select Create workspace.
Create your graph
After creating your Hypermode workspace you’ll be prompted to create your first graph.Enter a name for your graph and choose the hosting location, then select Create graph.View your graph details
Once your graph is created you’ll see the graph details including its status, connection string, and API key.We’ll use the Dgraph connection string and the API key to connect to our graph via Dgraph clients such as Ratel or language SDKs.Click on the copy icon next to the connection string to copy the Dgraph connection string.Connect the Ratel graph client
Add data to the graph with a mutationGraph databases like Dgraph use a data model called the property graph, which consists of nodes, relationships that connect nodes, and key-value pair properties that describe nodes and relationships. With Dgraph, we use triples to describe each piece of our graph, which when combined together make up our property graph. Triples are composed of a subject, predicate, and object.<subject> <predicate> <object> .
The subject always refers to a node, predicates can be a relationship or property, and the object can be a node or property value. You can read more about triples in the RDF section of the docs, but for now let’s move on to creating data in Dgraph using triples. Let’s create data about movies, characters, and their genres. Here’s the property graph representation of the data we’ll create:
Add mutation in Ratel
The create, update, and delete operations in Dgraph are called mutations.In the Ratel Console page, select the Mutate tab, then paste the following mutation:{
set {
_:scifi <dgraph.type> "Genre" .
_:scifi <Genre.name> "Sci-Fi" .
_:starwars <dgraph.type> "Movie" .
_:starwars <Movie.title> "Star Wars: Episode IV - A New Hope" .
_:starwars <Movie.release_date> "1977-05-25"^^<xs:dateTime> .
_:startrek <dgraph.type> "Movie" .
_:startrek <Movie.title> "Star Trek: The Motion Picture" .
_:startrek <Movie.release_date> "1979-12-07"^^<xs:dateTime> .
_:george <dgraph.type> "Person" .
_:george <Person.name> "George Lucas" .
_:luke <dgraph.type> "Character" .
_:luke <Character.name> "Luke Skywalker" .
_:leia <dgraph.type> "Character" .
_:leia <Character.name> "Princess Leia" .
_:han <dgraph.type> "Character" .
_:han <Character.name> "Han Solo" .
_:starwars <Movie.genre> _:scifi .
_:startrek <Movie.genre> _:scifi .
_:starwars <Movie.director> _:george .
_:starwars <Movie.character> _:luke .
_:starwars <Movie.character> _:leia .
_:starwars <Movie.character> _:han .
}
}
The preceding DQL mutation uses N-Quad RDF format to define the triples that make up the property graph we want to create.
View mutation results
Select Run to execute the mutation. In the JSON tab we can see the result of this mutation.{
"data": {
"code": "Success",
"message": "Done",
"queries": null,
"uids": {
"george": "0x4",
"han": "0x7",
"leia": "0x6",
"luke": "0x5",
"scifi": "0x1",
"startrek": "0x3",
"starwars": "0x2"
}
}
}
Dgraph displays the universal identifiers (UID) of the nodes that were created. Query the graph
Query for all movies
In the Console page, select the Query tab and run this query:{
movies(func: type(Movie)) {
Movie.title
Movie.genre {
Genre.name
}
Movie.director {
Person.name
}
Movie.character {
Character.name
}
}
}
This query searches for all Movie
nodes as the start of the traversal using the type(Movie)
function to define the starting point of our query traversal, then finds any genres, directors, and characters connected to each movie.
View results in JSON and graph visualization
In Ratel’s JSON tab we can view the results of this query as JSON:{
"data": {
"movies": [
{
"Movie.title": "Star Wars: Episode IV - A New Hope",
"Movie.genre": [
{
"Genre.name": "Sci-Fi"
}
],
"Movie.director": [
{
"Person.name": "George Lucas"
}
],
"Movie.character": [
{
"Character.name": "Luke Skywalker"
},
{
"Character.name": "Princess Leia"
},
{
"Character.name": "Han Solo"
}
]
},
{
"Movie.title": "Star Trek: The Motion Picture",
"Movie.genre": [
{
"Genre.name": "Sci-Fi"
}
]
}
]
}
}
In the response panel, Select Graph to view a graph visualization of the results of our query: Update the graph schema and query using an indexThe previous query used the type()
function to find the starting point of our graph traversal. We can use more complex functions to filter by string comparison operator, and others, however to use these function we must first update the graph schema to create an index on the predicates we want to use in these functions. The function documentation specifies which kind of index is needed for each function. We’ll use Ratel to alter the schema to add indexes on some of the data so queries can use term matching, filtering, and sorting.
Create an index for movie title
In Ratel’s Schema page, select Predicates. Here we can see all the predicates used in the graph. A predicate is Dgraph’s internal representation of a node, property, or relationship.Select theMovie.title
predicate. Ratel displays details about the predicate type and indexes.Change the type to string then select index and select term for the Movie.title
predicate, then select Update to apply the index.
Create an index for movie release date
Next, we’ll create an index for theMovie.release_date
predicate.Select the Movie.release_date
predicate. Change the type to dateTime. Select index and choose year for the index tokenizer. Click Update to apply the index on the release-date
predicate.
Query using indexes
Now let’s find all movies with the term “Star” in their title and released before 1979.In the Console page select the Query tab and run this query:{
movieSearch(func: allofterms(Movie.title, "Star"), orderasc: Movie.release_date) @filter(lt(Movie.release_date, "1979")) {
Movie.title
Movie.release_date
Movie.director {
Person.name
}
Movie.character (orderasc: Character.name) {
Character.name
}
}
}
We can see the JSON result in the JSON tab:
{
"data": {
"movieSearch": [
{
"Movie.title": "Star Wars: Episode IV - A New Hope",
"Movie.release_date": "1977-05-25T00:00:00Z",
"Movie.director": [
{
"Person.name": "George Lucas"
}
],
"Movie.character": [
{
"Character.name": "Han Solo"
},
{
"Character.name": "Luke Skywalker"
},
{
"Character.name": "Princess Leia"
}
]
}
]
}
}
And also view the graph visualization of the result in the Graph tab:Try changing the release date and the search terms conditions to see Dgraph search and filtering in action. Reverse relationship query
Add reverse relationship
In the previous queries we traversed from the movie node to its connected genre node, but what if we want to find all movies connected to a genre node? In order to traverse from a genre node to a movie node we need to explicitly define theMovie.genre
predicate as a reverse relationship.To define a reverse relationship for the Movie.genre
predicate we’ll return to the Schema page in Ratel, select the Movie.genre
predicate and toggle the reverse checkbox. Then select Update to apply this schema change.
Query using the reverse relationship
In a DQL query the~
operator is used to specify a reverse relationship. To traverse from a genre node to a movie node we use the syntax ~Movie.genre
.In this query we find all movies connected to the “Sci-Fi” genre:
{
genreSearch(func: type(Genre)) {
Genre.name
movies: ~Movie.genre {
Movie.title
}
}
}
Note that we can also alias the field name to “movies” in our result JSON using the syntax movies: ~Movie.genre
.
{
"data": {
"genreSearch": [
{
"Genre.name": "Sci-Fi",
"movies": [
{
"Movie.title": "Star Wars: Episode IV - A New Hope"
},
{
"Movie.title": "Star Trek: The Motion Picture"
}
]
}
]
}
In this quick start we created a new graph instance using Dgraph on Hypermode, added data, queried the graph, visualized the results, and updated the schema of our graph. Where to go from here
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4