Dgraph DQL Tour
Basic
Facets : Edge attributes
Dgraph supports facets — key value pairs on edges — as an extension to RDF
triples. That is, facets add properties to edges, rather than to nodes. For
example, a friend edge between two nodes may have a boolean property of close
friendship. Facets can also be used as weights for edges.
More details about Facets you can find in our Docs:
https://dgraph.io/docs/query-language/#facets-edge-attributes
Reading all the documentation about Facets you will have examples of:
- Facets on scalar predicates
- Alias with facets
- Facets on UID predicates
- Filtering on facets
- Sorting using facets
- Assigning Facet values to a variable
- Facets and Variable Propagation
- Facets and Aggregation
For JSON mutation with Facets see: https://dgraph.io/docs/mutations/#facets
Run this mutation on you terminal, before running the query:
Show answer
curl -sH "Content-Type: application/rdf" "localhost:8080/mutate?commitNow=true" -XPOST -d $'
{
set {
# -- Facets on scalar predicates
_:alice <name> "Alice" .
_:alice <mobile> "040123456" (since=2006-01-02T15:04:05) .
_:alice <car> "MA0123" (since=2006-02-02T13:01:09, first=true) .
_:bob <name> "Bob" .
_:bob <car> "MA0134" (since=2006-02-02T13:01:09) .
_:charlie <name> "Charlie" .
_:dave <name> "Dave" .
# -- Facets on UID predicates
_:alice <friend> _:bob (close=true, relative=false) .
_:alice <friend> _:charlie (close=false, relative=true) .
_:alice <friend> _:dave (close=true, relative=true) .
# -- Facets for variable propagation
_:movie1 <name> "Movie 1" .
_:movie2 <name> "Movie 2" .
_:movie3 <name> "Movie 3" .
_:alice <rated> _:movie1 (rating=3) .
_:alice <rated> _:movie2 (rating=2) .
_:alice <rated> _:movie3 (rating=5) .
_:bob <rated> _:movie1 (rating=5) .
_:bob <rated> _:movie2 (rating=5) .
_:bob <rated> _:movie3 (rating=5) .
_:charlie <rated> _:movie1 (rating=2) .
_:charlie <rated> _:movie2 (rating=5) .
_:charlie <rated> _:movie3 (rating=1) .
}
}' | python -m json.tool
Add multiple attributes to a third single node
Sometimes you need two Edges with distinct predicates pointing to the same node.
This feature can be used to “explain the relationship” of nodes.
Let’s say you have a existing Node that’s your target his UID is “0x0f43”. So
you need to setup your RDF like this.
_:MyNode <predicate1> <0x0f43> ( foo=bar, and=1 ) .
_:MyNode <predicate2> <0x0f43> ( bar=bar, and=2 ) .
_:MyNode <predicate3> <0x0f43> ( bar=bar, and=3 ) .
more examples:
Show answer
_:MyNode <Morning> <0x0f43> ( foo=true, and=1 , since=2006-02-02T13:01:09 ) .
_:MyNode <Evening> <0x0f43> ( bar=false, and=2, since=2006-02-02T13:01:09 ) .
_:MyNode <Night> <0x22> ( bar=true, and=3, since=2006-02-02T13:01:09 ) .
If you do the same mutation above but using the same predicates instead of
unique one. You will override the Edge attribute. Line by line. So you must to
use distinct predicates.