dgraph 基本查询语法 三
这部分主要是查询块、查询变量、聚合操作
多名称查询
实际上就是类似多个查询数据的拼接
格式:
{
caro(func: allofterms(name@en, "Marc Caro")) {
name@en
director.film {
name@en
}
}
jeunet(func: allofterms(name@en, "Jean-Pierre Jeunet")) {
name@en
director.film {
name@en
}
}
}
查询变量
类似graphql 总的input 变量,但是查询变量更方便,可以理解为sql 的存储过程,或者编程中的函数
var_name as some_block { ... }
查询变量数据的引用
查询变量的数据可以传递给子查询block 使用,这点相比graphql 的方式有很大的方便
参考格式:
{
coactors(func:allofterms(name@en, "Jane Campion")) @cascade {
JC_films as director.film { # JC_films = all Jane Campion's films
starting_movie: name@en
starring {
JC_actors as performance.actor { # JC_actors = all actors in all JC films
actor : name@en
actor.film {
performance.film @filter(not uid(JC_films)) {
film_together : name@en
starring {
# find a coactor who has been in some JC film
performance.actor @filter(uid(JC_actors)) {
coactor_name: name@en
}
}
}
}
}
}
}
}
}
指变量(min max)
使用min max 可以获取变量的最大或者最小指
参考格式:
{
q(func: allofterms(name@en, "Ang Lee")) {
director.film {
uid
name@en
# Count the number of starring edges for each film
num_actors as count(starring)
# In this block, num_actors is the value calculated for this film.
# The film with uid and name
}
# Here num_actors is a map of film uid to value for all
# of Ang Lee's films
#
# It can't be used directly, but aggregations like min and max
# work over all the values in the map
most_actors : max(val(num_actors))
}
# to use num_actors in another query, make sure it's done in a context
# where the film uid to value map makes sense.
}
指变量(sum avg)
可以获取变量的sum 以及avg
参考格式:
{
ID as var(func: allofterms(name@en, "Steven Spielberg")) {
# count the actors and save to a variable
# average as ...
}
# average is a map from uid to value so it must be used in a context
# where the map makes sense. Because query block avs works over the UID
# of Steven Spielberg, the value variable has the value we expect.
avs(func: uid(ID)) @normalize {
name : name@en
# get the average
# also count the movies
}
}
指变量 filter order
指变量可以应用filter以及order 操作
参考格式:
{
ID as var(func: allofterms(name@en, "Steven")) {
director.film {
num_actors as count(starring)
}
average as avg(val(num_actors))
}
avs(func: uid(ID), orderdesc: val(average)) @filter(ge(val(average), 40)) @normalize {
name : name@en
average_actors : val(average)
num_films : count(director.film)
}
}
指变量 math
dgraph 内置math 函数操作,可以进行一些常见的+ / - 以及sin 。。。。 操作
参考格式:
{
var(func:allofterms(name@en, "Jean-Pierre Jeunet")) {
name@en
films as director.film {
stars as count(starring)
directors as count(~director.film)
ratio as math(stars / directors)
}
}
best_ratio(func: uid(films), orderdesc: val(ratio)){
name@en
stars_per_director : val(ratio)
num_stars : val(stars)
}
}
groupby 操作
类似sql 的groupby 操作,groupby 代码块内部操作只能应用到聚合函数上,同时count 只能
应用到uid 上,同时可以方便的通过变量应用到其他查询中
参考格式:
{
var(func:allofterms(name@en, "Steven Spielberg")) {
director.film @groupby(genre) {
a as count(uid)
}
}
byGenre(func: uid(a), orderdesc: val(a)) {
name@en
num_movies : val(a)
}
}
参考资料
https://tour.dgraph.io/blocksvars/1/
https://github.com/rongfengliang/dgraph-docker-compose-deploy
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)