> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 查询结构

> Attack Surface Query 的组成部分：批量查询、from、select、关联行、include 以及响应结构

## 批量查询

一个请求是由多个命名查询组成的 **批量查询**：

```json theme={null}
{
  "window": { "since": "now-7d" },
  "queries": {
    "<name>": { "from": "<resource>", "...": "..." },
    "<another name>": { "from": "<resource>", "...": "..." }
  },
  "options": { "consistent": false, "mode": "interactive", "timezone": "UTC" }
}
```

**queries** `object` *required*

一到十个命名查询。名称以字母开头，包含字母、数字和 `_`（最多 64 个字符）。结果以相同的名称返回。

**window** `object`

所有未设置自身时间窗口的查询所使用的默认时间窗口：`{ "since": "now-7d", "until": "now" }`。参见 [时间窗口](https://docs.oasm.io/zh/attack-surface-query/filtering.md#time-windows)。

**options.consistent** `boolean` (default: false)

在同一个数据库快照上依次运行批量查询中的每个查询。当多个查询的数字必须完全吻合时（例如报告）使用它；否则查询并行运行。

**options.mode** `string` (default: interactive)

`interactive` 立即运行批量查询。`report` 将其作为后台任务加入队列，限制要宽松得多。参见 [已保存的查询和报告](https://docs.oasm.io/zh/attack-surface-query/saved-queries-and-reports.md#report-mode)。

**options.timezone** `string` (default: UTC)

划分时间桶（`day`、`week`…）所依据的 IANA 时区，例如 `Asia/Ho_Chi_Minh`。参见 [时区](https://docs.oasm.io/zh/attack-surface-query/aggregations.md#time-zones)。

> [!WARNING]
>
> 所有对象都是严格的：任何位置出现未知键（例如拼写错误 `"selct"`，或 `workspaceId`）都会报错，而不会被忽略。工作区始终来自你的会话，而不是请求体。

## 单个查询

**from** `string` *required*

要读取的资源：`targets`、`hosts`、`services`、`technologies`、`tls`、`urls`、`ips`、`dnsRecords`、`vulnerabilities` 或 `rollups`。

**select** `string[]`

要返回的字段。省略时返回一小组默认字段（对于 `services`：`port`、`host.value`、`firstSeen`）。

**include** `object`

附加到每一行的关联行列表。参见 [include](#include-lists-of-related-rows)。

**where** `object`

筛选条件。参见 [筛选](https://docs.oasm.io/zh/attack-surface-query/filtering.md)。

**window** `object | null`

此查询的时间窗口；会覆盖批量查询的窗口。`null` 表示全部时间范围。

**windowField** `string`

窗口作用的时间字段。默认：资源的 `firstSeen`。

**groupBy / aggregate** `array / object`

将查询变为计数查询。参见 [计数和分组](https://docs.oasm.io/zh/attack-surface-query/aggregations.md)。

**having** `object`

按分组键或聚合名称筛选聚合查询的分组。参见 [having](https://docs.oasm.io/zh/attack-surface-query/aggregations.md#having-filter-the-groups)。

**fill** `boolean` (default: false)

返回窗口内的每个时间桶，包括空桶。参见 [填充空桶](https://docs.oasm.io/zh/attack-surface-query/aggregations.md#filling-empty-buckets)。

**orderBy** `object[]`

最多三个 `{ "field", "dir": "asc" | "desc" }`。默认：最新的在前。

**limit** `number` (default: 50)

每页行数。最大值参见 [分页、限制和开销](https://docs.oasm.io/zh/attack-surface-query/paging-and-limits.md)。

**cursor** `string`

上一页的 `nextCursor`。

**withTotal** `boolean` (default: false)

同时返回 `total`，即匹配的行数（上限为 10 000）。

## select：字段和关联行

字段可以是资源自身的字段（`port`），也可以是 **单个关联行** 的字段，以点路径表示。一个服务有一个主机、一个目标和一个最新的 HTTP 响应，因此以下字段在 `services` 上都有效：

```json theme={null}
{ "from": "services", "select": ["port", "host.value", "target.value", "http.title", "http.statusCode"] }
```

结果会嵌套关联字段：

```json theme={null}
{
  "id": "53c80e50-…",
  "port": 443,
  "host": { "value": "api.example.com" },
  "target": { "value": "example.com" },
  "http": { "title": "API Gateway", "statusCode": 200 }
}
```

路径可以串联两个关联行：`technologies` → `service` → `host` 得到 `service.host.value`。

> [!TIP]
>
> 不存在的关联行会返回 `null`，而不是一个字段全为 null 的对象：`"http": null` 表示该服务没有 HTTP 响应。

## include：关联行列表

一个主机有多个服务；一个服务有多个技术和 URL。列表不能放在 `select` 中，而要用 `include` 附加。每个 include 本身就是一个小查询：

```json theme={null}
{
  "from": "hosts",
  "select": ["value"],
  "limit": 20,
  "include": {
    "services": {
      "select": ["port", "http.title"],
      "limit": 10,
      "include": {
        "technologies": { "select": ["name", "version"], "limit": 5 }
      }
    }
  }
}
```

```json theme={null}
{
  "id": "1396ad91-…",
  "value": "api.example.com",
  "services": [
    { "id": "…", "port": 443, "http": { "title": "API Gateway" },
      "technologies": [ { "id": "…", "name": "nginx", "version": "1.25" } ] }
  ]
}
```

**include.\<list>.select** `string[]`

子行（及其关联行）的字段。

**include.\<list>.where** `object`

针对子行的筛选：只附加匹配的子行。父行不受其筛选（筛选父行请使用父级的 `where`）。

**include.\<list>.orderBy** `object[]`

子行的顺序。只有部分字段可以用于列表排序（例如 `firstSeen`，以及主机服务的 `port`）；其他字段保持自然顺序。

**include.\<list>.limit** `number` (default: 10)

每个父行的子行数（最多 50；AI 智能体为 20）。

**include.\<list>.include** `object`

嵌套列表，最多两层。

> [!WARNING]
>
> 每个 include 会 **为每一行** 获取最多其自身限制数量的子行：200 个主机 × 20 个服务就是 4 000 个子行。请保持 `limit × include limit` 较小，或者直接带筛选条件查询子资源（参见 [组合查询](https://docs.oasm.io/zh/attack-surface-query/composing-queries.md)）。

### 各资源的列表

| 资源 | 单个关联行（在 `select` 中用点路径） | 列表（`include` 或 `where`） |
| - | - | - |
| `targets` | – | `hosts` |
| `hosts` | `target` | `services`, `ips`, `dnsRecords`, `vulnerabilities` |
| `services` | `host`, `target`, `http` | `technologies`, `tls`, `statusCodes`, `urls`, `tags` |
| `technologies`, `tls`, `urls` | `service` | – |
| `ips`, `dnsRecords` | `host` | – |
| `vulnerabilities` | `host`, `tool` | – |

## 默认字段和顺序

| 资源 | 默认 `select` | 默认顺序 |
| - | - | - |
| `targets` | `value`, `type`, `firstSeen` | 最新的在前 |
| `hosts` | `value`, `kind`, `firstSeen`, `target.value` | 最新的在前 |
| `services` | `port`, `host.value`, `firstSeen` | 最新的在前 |
| `technologies` | `name`, `version`, `service.host.value`, `service.port` | 最新的在前 |
| `tls` | `host`, `port`, `notAfter`, `issuerCn` | `notAfter` 升序（即将到期的在前） |
| `urls` | `url`, `firstSeen` | 最新的在前 |
| `ips` | `ip`, `host.value` | 最新的在前 |
| `dnsRecords` | `recordType`, `value`, `host.value` | 最新的在前 |
| `vulnerabilities` | `name`, `severity`, `host.value`, `firstSeen` | 最新的在前 |
| `rollups` | `metric`, `key`, `value` | `value` 降序 |

你无权读取的默认字段（例如没有 `target.read` 时的 `target.value`）会被省略，而不会导致查询失败。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.