標籤:func query UNC like can put stars 操作 where
這部分主要是查詢塊、查詢變數、彙總操作
多名稱查詢
實際上就是類似多個查詢資料的拼接
格式:{ caro(func: allofterms([email protected], "Marc Caro")) { [email protected] director.film { [email protected] } } jeunet(func: allofterms([email protected], "Jean-Pierre Jeunet")) { [email protected] director.film { [email protected] } }}
查詢變數
類似graphql 總的input 變數,但是查詢變數更方便,可以理解為sql 的預存程序,或者編程中的函數
var_name as some_block { ... }
查詢變數資料的引用
查詢變數的資料可以傳遞給子查詢block 使用,這點相比graphql 的方式有很大的方便
參考格式:{ coactors(func:allofterms([email protected], "Jane Campion")) @cascade { JC_films as director.film { # JC_films = all Jane Campion‘s films starting_movie: [email protected] starring { JC_actors as performance.actor { # JC_actors = all actors in all JC films actor : [email protected] actor.film { performance.film @filter(not uid(JC_films)) { film_together : [email protected] starring { # find a coactor who has been in some JC film performance.actor @filter(uid(JC_actors)) { coactor_name: [email protected] } } } } } } } }}
指變數(min max)
使用min max 可以擷取變數的最大或者最小指
參考格式:{ q(func: allofterms([email protected], "Ang Lee")) { director.film { uid [email protected] # 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([email protected], "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 : [email protected] # get the average # also count the movies }}
指變數 filter order
指變數可以應用filter以及order 操作
參考格式:{ ID as var(func: allofterms([email protected], "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 : [email protected] average_actors : val(average) num_films : count(director.film) }}
指變數 math
dgraph 內建math 函數操作,可以進行一些常見的+ / - 以及sin 。。。。 操作
參考格式:{ var(func:allofterms([email protected], "Jean-Pierre Jeunet")) { [email protected] 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)){ [email protected] stars_per_director : val(ratio) num_stars : val(stars) }}
groupby 操作
類似sql 的groupby 操作,groupby 代碼塊內部操作只能應用到彙總函式上,同時count 只能
應用到uid 上,同時可以方便的通過變數應用到其他查詢中
參考格式:{ var(func:allofterms([email protected], "Steven Spielberg")) { director.film @groupby(genre) { a as count(uid) } } byGenre(func: uid(a), orderdesc: val(a)) { [email protected] num_movies : val(a) }}
參考資料
https://tour.dgraph.io/blocksvars/1/
https://github.com/rongfengliang/dgraph-docker-compose-deploy
dgraph 基本查詢文法 三