Dgraph QL 入門

0x01 HelloWorld(簡單查詢)

{
    find_someone(func: eq(name, "SomeOne's name")) {
        uid
        name
        age
        sex
    }
}
複製代碼

上面的查詢語句表示, 新建一個名爲 find_someone 的query, 查詢條件爲集合內name等於 SomeOne's name的實體, 並返回 uid, name, age, sex 字段。 其中 uid 是 dgraph 內部的用來標識entity的惟一identify, 是一個16進制的數. 也能夠經過uid 來查詢某個entity:express

{
    find_someone(func: uid(0x6)) {
        name
        age
        sex
        uid
        someFieldNotFound # 若是字段不存在,則不顯示,不會報錯
    }
}
複製代碼

0x02 關係查詢

能夠查詢某entity和另外的entity的關係(edge)json

{
    find_someone(func: eq(name, "Michael")) {
        name
        age
        uid
        owner_pet {  # 查詢Michael擁有的寵物
            name
        }
        friend { # 查詢Michael的朋友
            name
            age
            uid
            friend {
                name # 查詢Michael朋友的朋友
            }
        }
    }
}
複製代碼

Response:ide

{
  "data": {
    "find_someone": [
      {
        "name": "Michael",
        "age": 39,
        "uid": "0x3",
        "friend": [
          {
            "age": 35,
            "uid": "0x4",
            "friend": [
              {
                "name": "Michael"
              }
            ]
          },
          {
            "age": 24,
            "uid": "0x5",
            "friend": [
              {
                "name": "Catalina"
              }
            ]
          },
          {
            "name": "Catalina",
            "age": 19,
            "uid": "0x6"
          },
          {
            "age": 35,
            "uid": "0x7"
          },
          {
            "name": "Sarah",
            "age": 55,
            "uid": "0xa"
          }
        ]
      }
    ]
  }
複製代碼

0x03 數據類型和節點

Dgraph中可用的數據類型有:函數

  • int signed 64 bit integer
  • float double precision floating point number
  • string string
  • bool boolean
  • id ID’s stored as strings
  • dateTime RFC3339 time format with optional timezone eg: 2006-01-02T15:04:05.999999999+10:00 or 2006-01-02T15:04:05.999999999
  • geo geometries stored using go-geom
  • uid uid

0x04 多語言支持

Dgraph支持UTF-8編碼的字符串文本查詢.ui

字符串值謂詞能夠用language tag進行註釋.編碼

好比:spa

"Lily"@en # 表示以英文存儲
    "अमित"@hi # 表示以菲律賓語存儲
    "상현"@ko  # 表示以韓文存儲
    "張三"@ch  # 表示中文存儲
複製代碼

Query能夠經過以哪一種語言搜索以及以哪一種語言返回來搜索帶Tag語言的文本. 若是是以此種格式要求返回: @lang1:...:langN, 則需遵循如下規則:code

  • 至少返回一個結果
  • 若是結果存在於首選語言中,則返回最左邊(在首選項列表中)的結果
  • 若是結果不存在於首選語言中,則不返回結果,除非首選項列表以.結尾,在這種狀況下,將返回沒有指定語言的值.

0x05 函數和過濾

節點根據應用於節點edge的函數進行過濾.orm

過濾不只能夠應用在查詢的頂級節點上, 事實上, 查詢過濾能夠應用在任何節點上.排序

下列是一些經常使用的過濾函數:

  • allOfTerms(edge_name, "term1 ... termN"): 以任何順序匹配符合全部指定模式的字符串;不區分大小寫

  • anyOfTerms(edge_name, "term1 ... termN"): 以任何順序匹配符合任何指定模式的字符串;不區分大小寫

  • 等值或不等值查詢過濾, 能夠用來比較的類型有: int, float, stringdate.

    1. eq(edge_name, value): 等於
    2. ge(edge_name, value): 大於等於
    3. le(edge_name, value): 小於等於
    4. gt(edge_name, value): 大於
    5. lt(edge_name, value): 小於
  • 還有其餘過濾函數諸如: regular expression, full text search, geo search

eg.

{
    filter_friend(func: allofterms(name, "Michael")) {
        name
        age
        friend @filter(le(age, 27)) { # 子過濾, 過濾出年齡小於27的朋友..
          name@. # tag 爲 . , 表示顯示全部語言類別的name
          age
        }
    }
}
複製代碼

Response:

{
  "data": {
    "filter_friend": [
      {
        "name": "Michael",
        "age": 39,
        "friend": [
          {
            "name@.": "Sang Hyun",
            "age": 24
          },
          {
            "name@.": "Catalina",
            "age": 19
          }
        ]
      }
    ]
  }
}
複製代碼

0x06 邏輯運算(AND, OR and NOT)

邏輯操做符 AND, ORNOT 能夠在一個filter中組合多個function.

eg.

{
  filter_test(func: anyofterms(name, "Michael")) {
    age
    name
    friend @filter(lt(age, 40) AND gt(age, 20)) { # 過濾大於20且小於40的friend.
      name@.
      age
    }
  }
}

{
  filter_test(func: anyofterms(name, "Michael")) {
    age
    name
    friend @filter(NOT ge(age, 20)) { # 過濾小於20的friend.
      name@.
      age
    }
  }
}
複製代碼

注意: 邏輯操做符不能直接用於func關鍵字以後, 好比:

{
  filter_xxx(func: AND allofterms()) ... # 報錯
}
複製代碼

0x07 排序

查詢結果可使用 orderascorderdesc 來升降序排序.

排序結果只會在JSON Response中才會體現.

eg.

{
  filter_test(func: allofterms(name, "Michael")) {
    age
    name
    friend (orderdesc: age) { # 以age倒序排列
      name@.
      age
    }
  }
}
複製代碼

0x08 分頁(offset, first, after)

一般來講,一個查詢返回上萬的結果的狀況並不罕見.

有些場景下, 咱們可能不須要這麼多的數據,或者只是須要 top-K 的數據, 對結果進行分頁顯示,或者limit一些結果.

GraphQL+- 語法中, 關鍵字: offset, first, after 能夠和排序功能組合使用.

  • first: N 返回前N個結果
  • offset: N 跳過前N個結果
  • after: uid 返回該uid以後的數據

這裏的N必須是無符號整型

默認的, 查詢結果經過uid進行排序.

eq.

{
  filter_test(func: allofterms(name, "Michael")) {
    age
    name
    friend (orderdesc: name@., first: 2, offset: 1) { 
      name@.
      age
    }
  }
}
複製代碼

0x09 計數(Count)

使用 count 函數能夠統計結果集中的edges個數.

eg.

{
  filter_test(func: anyofterms(name, "Michael")) {
    age
    name
    count(friend)
  }
}
複製代碼

Response:

"data": {
    "filter_test": [
      {
        "age": 39,
        "name": "Michael",
        "count(friend)": 5
      }
    ]
  }
}
複製代碼
相關文章
相關標籤/搜索