dgraph 基本查询语法 一
dgraph 的查询语法是在graphql 上的扩展,添加了新的支持,同时官方提供了一个
学习的网站 https://tour.dgraph.io/
基本环境(cluster 模式的)
参考 github 项目 https://github.com/rongfengliang/dgraph-docker-compose-deploy
git clone https://github.com/rongfengliang/dgraph-docker-compose-deploy
cd dgraph-docker-compose-deploy/one-zero
docker-compose up -d
基本使用
- 加载schema
name: string @index(term) @lang .
age: int @index(int) .
friend: uid @count .
- 加载数据
{
set {
_:michael <name> "Michael" .
_:michael <age> "39" .
_:michael <friend> _:amit .
_:michael <friend> _:sarah .
_:michael <friend> _:sang .
_:michael <friend> _:catalina .
_:michael <friend> _:artyom .
_:michael <owns_pet> _:rammy .
_:amit <name> "अमित"@hi .
_:amit <name> "অমিত"@bn .
_:amit <name> "Amit"@en .
_:amit <age> "35" .
_:amit <friend> _:michael .
_:amit <friend> _:sang .
_:amit <friend> _:artyom .
_:luke <name> "Luke"@en .
_:luke <name> "Łukasz"@pl .
_:luke <age> "77" .
_:artyom <name> "Артём"@ru .
_:artyom <name> "Artyom"@en .
_:artyom <age> "35" .
_:sarah <name> "Sarah" .
_:sarah <age> "55" .
_:sang <name> "상현"@ko .
_:sang <name> "Sang Hyun"@en .
_:sang <age> "24" .
_:sang <friend> _:amit .
_:sang <friend> _:catalina .
_:sang <friend> _:hyung .
_:sang <owns_pet> _:goldie .
_:hyung <name> "형신"@ko .
_:hyung <name> "Hyung Sin"@en .
_:hyung <friend> _:sang .
_:catalina <name> "Catalina" .
_:catalina <age> "19" .
_:catalina <friend> _:sang .
_:catalina <owns_pet> _:perro .
_:rammy <name> "Rammy the sheep" .
_:goldie <name> "Goldie" .
_:perro <name> "Perro" .
}
}
- 基本查询
{
everyone(func: anyofterms(name, "Michael Amit")) {
name
friend {
name@ru:ko:en
friend { expand(_all_) { expand(_all_) } }
}
}
}
- 数据类型查询
schema(pred: [name, age, friend, owns_pet]) {
type
index
}
- 语言支持
<name>@<lan>
参考:
{
language_support(func: allofterms(name@hi, "अमित")) {
name@bn:hi:en
age
friend {
name@ko:ru
age
}
}
}
- 函数&&过滤
内容查询
- allOfTerms(edge_name, "term1 ... termN")
- anyOfTerms(edge_name, "term1 ... termN")
比较查询
- eq(edge_name, value):
- ge(edge_name, value):
- le(edge_name, value):
- gt(edge_name, value):
- lt(edge_name, value):
- AND OR NOT 查询
参考:
{
michael_friends_and(func: allofterms(name, "Michael")) {
name
age
friend @filter(ge(age, 27) AND le(age, 48)) {
name@.
age
}
}
}
- 排序
orderasc or orderdesc
{
michael_friends_sorted(func: allofterms(name, "Michael")) {
name
age
friend (orderasc: age) {
name@.
age
}
}
}
- 分页
first: N
offset: N
after: uid
参考:
{
michael_friends_first(func: allofterms(name, "Michael")) {
name
age
friend (orderasc: name@., offset: 1, first: 2) {
name@.
}
}
}
- count
{
michael_number_friends(func: allofterms(name, "Michael")) {
name
age
count(friend)
}
}
- has
{
have_friends(func: has(friend)) {
name@.
age
number_of_friends : count(friend)
}
}
- 别名(和graphql 一致)
{
michael_number_friends(func: allofterms(name, "Michael")) {
persons_name : name
age
number_of_friends : count(friend)
}
}