Skip to content

查询、筛选与排序

数据墙DBW 的 REST 和 GraphQL API 都提供了丰富的筛选和排序能力。两者功能对等,语法形式不同——REST 使用 URL 查询参数,GraphQL 使用查询对象。

筛选(REST)

REST 通过 $filter 参数实现条件筛选,语法遵循 OData 规范。

比较运算符

?$filter={field} {op} {value}
运算符含义示例
eq等于title eq 'Dune'
ne不等于year ne 2000
gt大于pages gt 300
ge大于等于year ge 2000
lt小于pages lt 100
le小于等于year le 2020
notnot year lt 2000

字符串值用单引号包裹,数字直接书写:

bash
curl "http://localhost:5000/api/Book?\$filter=year ge 2000 and pages gt 300"

逻辑组合

and  — 且
or   — 或
not  — 非
bash
# 年份大于 2000 且 页数大于 300
?$filter=year ge 2000 and pages gt 300

# 标题等于 Dune 或 Foundation
?$filter=title eq 'Dune' or title eq 'Foundation'

# 年份不小于 2000
?$filter=not year lt 2000

括号控制优先级:

bash
?$filter=(year ge 2000 or title eq 'Dune') and pages gt 300

字符串函数

函数说明示例
contains包含子字符串contains(title,'Foundation')
startswith以某字符串开头startswith(title,'The')
endswith以某字符串结尾endswith(title,'Wakes')
bash
?$filter=contains(title,'Dune')
?$filter=startswith(title,'The')

null 值判断

bash
?$filter=description eq null
?$filter=description ne null

完整示例

bash
# 综合查询:2000 年后出版、页数大于 200、标题包含 "Dune" 的书
curl "http://localhost:5000/api/Book?\$filter=year%20ge%202000%20and%20pages%20gt%20200%20and%20contains(title,%27Dune%27)"

NOTE

URL 中空格需编码为 %20,单引号编码为 %27。大多数 HTTP 客户端会自动处理编码。

筛选(GraphQL)

GraphQL 通过 filter 参数筛选,使用对象嵌套语法而非 URL 查询字符串。

比较运算符

graphql
{
  books(filter: { year: { ge: 2000 }, pages: { gt: 300 } }) {
    items { id title year pages }
  }
}
运算符含义
eq等于
neq不等于
gt大于
gte大于等于
lt小于
lte小于等于
contains字符串包含
startswith字符串开头
endswith字符串结尾
isNull是否为 null(true/false

逻辑组合

graphql
{
  books(
    filter: {
      and: [
        { year: { gte: 2000 } }
        { pages: { gt: 300 } }
      ]
    }
  ) { items { id title } }
}

多个条件用 andor 包裹:

graphql
{
  books(
    filter: {
      or: [
        { title: { contains: "Dune" } }
        { title: { contains: "Foundation" } }
      ]
    }
  ) { items { id title } }
}

嵌套逻辑:

graphql
{
  books(
    filter: {
      and: [
        { year: { gte: 2000 } }
        {
          or: [
            { title: { contains: "Dune" } }
            { title: { contains: "Foundation" } }
          ]
        }
      ]
    }
  ) { items { id title year } }
}

null 判断

graphql
{ books(filter: { description: { isNull: true } }) { items { id } } }
{ books(filter: { description: { isNull: false } }) { items { id } } }

排序(REST)

$orderby 参数定义排序规则,多字段用逗号分隔:

bash
# 单字段升序(默认)
?$orderby=title

# 单字段降序
?$orderby=year desc

# 多字段:先按年份降序,再按标题升序
?$orderby=year desc,title asc
bash
curl "http://localhost:5000/api/Book?\$orderby=year%20desc,title%20asc"

asc(升序)是默认方向,可以省略。desc 必须显式写出。

排序(GraphQL)

orderBy 参数使用键值对指定字段和方向:

graphql
{
  books(orderBy: { year: DESC, title: ASC }) {
    items { id title year }
  }
}

方向为 ASCDESC(大写)。省略方向默认为 ASC

graphql
# 等同于 orderBy: { year: ASC }
{
  books(orderBy: { year: ASC }) {
    items { id title year }
  }
}

筛选 + 排序 + 分页组合

三个参数可以同时使用:

REST

bash
curl "http://localhost:5000/api/Book?\$filter=year%20ge%202000&\$orderby=pages%20desc&\$first=10"

GraphQL

graphql
{
  books(
    filter: { year: { gte: 2000 } }
    orderBy: { pages: DESC }
    first: 10
  ) {
    items { id title year pages }
  }
}

REST vs GraphQL 对照

操作RESTGraphQL
等于$filter=title eq 'Dune'filter: { title: { eq: "Dune" } }
大于$filter=year gt 2000filter: { year: { gt: 2000 } }
包含$filter=contains(title,'Dune')filter: { title: { contains: "Dune" } }
逻辑与andand: [...]
逻辑或oror: [...]
nulleq nullisNull: true
排序$orderby=year descorderBy: { year: DESC }

下一步

数据墙DBW 产品文档与开发指南。