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

# 筛选数据集

> 对 900+ 个 Bright Data 市场数据集运行异步筛选任务。返回可下载的 snapshot_id，支持最大 200 MiB 的 CSV/JSON 上传。

Bright Data 市场数据集 API 的 Filter 端点可针对 900+ 个市场数据集中的任意一个运行大型或基于文件的筛选任务，并返回一个 `snapshot_id`，任务完成后即可用它下载数据。

> [!TIP]
>
> 将您的 API Key 粘贴到授权字段。要获取 API Key，请[创建账户](https://www.bright.cn/?hs_signup=1\&utm_source=docs\&utm_campaign=playground)并了解[如何生成 API Key](https://docs.brightdata.com/cn/api-reference/authentication.md#如何生成新的-api-key？)。

## 何时使用 Filter？

对于可接受异步处理的批量或基于文件的任务，使用 Filter：

* 按字段值筛选市场数据集，例如国家、价格或布尔标志。
* 查找文本字段包含某个值的记录。
* 按精确值列表匹配记录。
* 按 CSV 或 JSON 文件中的大型值列表筛选，例如排除 10 万以上的公司 ID。
* 超过 1,000 条记录的批量导出。
* Search 尚不支持的数据集。

如需对受支持数据集进行亚秒级实时查询，请改用 [Search](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/search-dataset.md)。

## Filter 如何工作？

* 调用 Filter 端点会启动一个异步任务，并在您的账户中创建一份筛选数据的快照。
* 端点返回 `snapshot_id`，而不是筛选后的记录。
* 任务最长运行时间为 5 分钟。运行超时的任务将被取消。
* 按快照中的每条记录计费，采用标准市场费率 \$2.5 CPM。
* Filter 适用于全部 900+ 个市场数据集。
* 筛选组的最大嵌套深度为 3 层。

## 如何进行身份验证？

Filter 使用 Bearer token 身份验证。在 `Authorization` 请求头中传入您的 API Key：

```bash theme={null}
Authorization: Bearer YOUR_API_KEY
```

从[账户设置](https://docs.brightdata.com/cn/api-reference/authentication.md#如何生成新的-api-key？)获取您的 Key。

## Filter 有哪些限制？

| 限制 | 值 | 说明 |
| - | - | - |
| **每个文件最大行数** | 10,000 | 每个上传的 CSV/JSON 文件最多可包含 10,000 行数据。表头行不计入。 |
| **每次请求最大文件数** | 无限制 | 一次 multipart 请求中可附加任意数量的文件，只要总大小不超过 200 MiB 上限。 |
| **最大请求大小** | 200 MiB | 所有上传文件和表单数据的总大小。超过 200 MiB 的请求会被拒绝。 |
| **任务超时** | 5 分钟 | 如果筛选在 5 分钟内未完成，任务将被取消。 |
| **筛选嵌套深度** | 3 层 | 使用 `and`/`or` 的嵌套筛选组的最大深度。 |
| **最大并行任务数** | 每个数据集 25 个 | 每个数据集最多可同时有 25 个 Filter 任务处于运行中（快照状态为 `building`）。在有运行中的任务完成之前，新请求将返回 `429`。 |
| **速率限制** | 5 次/分钟，120 次/小时 | Filter API 调用次数上限。两项限制同时适用。 |

如有超出上述限制的复杂筛选需求，请联系您的客户经理。

## 如何使用 JSON 请求进行筛选？

当筛选值可以直接放入请求时，使用 JSON 模式。将 `dataset_id`、`records_limit` 和 `filter` 放在 JSON 请求体中，并将 `Content-Type` 设为 `application/json`：

```bash theme={null}
curl -X POST "https://api.brightdata.com/datasets/filter" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "dataset_id": "gd_l1viktl72bvl7bjuj0",
    "records_limit": 100,
    "filter": {
      "name": "name",
      "operator": "includes",
      "value": "John"
    }
  }'
```

Filter 返回一个 `snapshot_id`：

```json theme={null}
{ "snapshot_id": "s_m123456789" }
```

### Filter 接受哪些请求字段？

| 字段 | 类型 | 必填 | 说明 |
| - | - | - | - |
| `dataset_id` | string | 是 | 要筛选的市场数据集 ID。在 multipart 模式下，作为查询参数发送。 |
| `records_limit` | integer | 否 | 快照中包含的最大记录数。在 multipart 模式下，作为查询参数发送。 |
| `filter` | object | 是 | 单字段筛选、筛选组或空值检查。 |
| `filter.name` | string | 是 | 用于筛选的数据集字段。使用[获取数据集元数据](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-dataset-metadata.md)列出字段名称。 |
| `filter.operator` | string | 是 | 字段与 `value` 的匹配方式，例如 `=`、`in`、`includes` 或 `>=`。 |
| `filter.value` | string、number、boolean、array 或文件名 | 是，`is_null` 和 `is_not_null` 除外 | 运算符用于比较的值。在 multipart 模式下，为上传文件的名称。 |

## 如何按字段类型筛选？

每个字段筛选由字段名称、运算符和值组成。以下示例按字段类型展示常用运算符。完整的运算符表、筛选组和嵌套规则，请参阅[筛选语法参考](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/filter-syntax.md)。

### 文本字段

使用 `includes` 匹配字段包含该值的记录：

```json theme={null}
{ "name": "name", "operator": "includes", "value": "John" }
```

此筛选可匹配 `John Smith`、`Johnson` 和 `Senior John Account Manager` 等值。使用 `not_includes` 可排除这些记录。

### 精确值列表

使用 `in` 匹配字段值恰好为列表中某个值的记录：

```json theme={null}
{ "name": "country", "operator": "in", "value": ["US", "GB", "DE"] }
```

使用 `not_in` 排除字段值在列表中的记录。

### 何时使用 includes 而不是 in？

| 运算符 | 适用场景 | 匹配方式 |
| - | - | - |
| `includes` | 需要匹配包含某个值的文本 | 部分匹配 |
| `in` | 已有明确的允许值列表 | 仅精确匹配 |

例如，`includes` 搭配 `engineer` 可匹配 `Sales Engineer` 和 `Engineering Manager`，而 `in` 搭配 `["Sales Engineer"]` 仅匹配 `Sales Engineer`。

### 布尔字段

使用 `=` 搭配 `true` 或 `false`：

```json theme={null}
{ "name": "verified", "operator": "=", "value": true }
```

### 数值字段

数值字段支持 `=`、`!=`、`<`、`<=`、`>`、`>=`、`in`、`not_in`、`is_null` 和 `is_not_null`：

```json theme={null}
{ "name": "price", "operator": ">=", "value": 100 }
```

### 日期范围

在 `and` 组中组合 `>=` 和 `<=` 以匹配日期范围：

```json theme={null}
{
  "operator": "and",
  "filters": [
    { "name": "created_at", "operator": ">=", "value": "2026-01-01" },
    { "name": "created_at", "operator": "<=", "value": "2026-01-31" }
  ]
}
```

### 数组字段

使用 `array_includes` 匹配多值字段中包含某个精确值的记录：

```json theme={null}
{ "name": "categories", "operator": "array_includes", "value": "Electronics" }
```

此筛选匹配 `categories` 数组包含 `Electronics` 的记录。使用 `not_array_includes` 可排除这些记录。

### 空字段

使用 `is_not_null` 仅保留字段有值的记录，或使用 `is_null` 匹配字段为空的记录。这两个运算符不需要 `value`：

```json theme={null}
{ "name": "reviews_count", "operator": "is_not_null" }
```

## 如何使用 CSV 或 JSON 文件上传进行筛选？

当您需要按大型值列表筛选时，使用文件上传。以 `multipart/form-data` 发送请求，将 `dataset_id` 和 `records_limit` 作为查询参数传入，并将每个筛选的 `value` 设为上传文件的名称：

```bash theme={null}
curl -X POST "https://api.brightdata.com/datasets/filter?dataset_id=gd_l1vijqt9jfj7olije&records_limit=100" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F 'filter={"operator":"and","filters":[{"name":"industries:value","operator":"includes","value":"industries.csv"}]}' \
  -F 'files[]=@/path/to/industries.csv'
```

响应与 JSON 模式返回的 `snapshot_id` 对象相同。

### 如何格式化 CSV 或 JSON 文件？

> [tab] CSV
>
> * 第一行必须是与筛选字段名称匹配的表头。
> * 后续每行包含单个值。
> * 文件只能包含一列。
> * 不支持空行和仅包含空格的行。
>
> ```csv Example: industries.csv theme={null}
> industries:value
> Accounting
> Ad Network
> Advertising
> ```

> [tab] JSON
>
> * 文件必须是对象数组，每个对象的键与筛选字段名称匹配。
>
> ```json Example: industries.json theme={null}
> [
>   {"industries:value": "Accounting"},
>   {"industries:value": "Ad Network"},
>   {"industries:value": "Advertising"}
> ]
> ```

文件中的值必须与数据集字段格式完全一致。

### 哪些运算符接受文件引用？

文件引用仅可与 `in`、`not_in`、`includes`、`not_includes`、`array_includes` 和 `not_array_includes` 配合使用。各运算符如何处理文件，请参阅[筛选语法参考](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/filter-syntax.md#哪些运算符可读取-csv-或-json-文件？)。

### 如何按超过 10,000 个值筛选？

将这些值拆分为每个最多 10,000 行的文件，并在一次请求中全部附加。以下示例排除分布在三个文件中的公司 ID：

```bash theme={null}
curl -X POST "https://api.brightdata.com/datasets/filter?dataset_id=gd_l1vijqt9jfj7olije&records_limit=5000" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F 'filter={"operator":"and","filters":[{"name":"company_id","operator":"not_in","value":"exclude1.csv"},{"name":"company_id","operator":"not_in","value":"exclude2.csv"},{"name":"company_id","operator":"not_in","value":"exclude3.csv"}]}' \
  -F 'files[]=@exclude1.csv' \
  -F 'files[]=@exclude2.csv' \
  -F 'files[]=@exclude3.csv'
```

一次请求中的多个文件也可用于不同字段。以下示例组合了行业文件和地区文件：

```bash theme={null}
curl -X POST "https://api.brightdata.com/datasets/filter?dataset_id=gd_l1vijqt9jfj7olije" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: multipart/form-data" \
  -F 'filter={"operator":"and","filters":[{"name":"industries:value","operator":"includes","value":"industries.csv"},{"name":"region","operator":"in","value":"regions.csv"}]}' \
  -F 'files[]=@/path/to/industries.csv' \
  -F 'files[]=@/path/to/regions.csv'
```

### 如何修复文件上传错误？

| 错误或问题 | 原因 | 解决方法 |
| - | - | - |
| `Only one column in header is allowed` | CSV 包含多于一列。 | 使用单列 CSV 文件。 |
| `One or more lines in the input file contain only space characters and cannot be used for filtering. Please remove any empty or whitespace-only line and try again.` | CSV 包含空行或仅含空格的行。 | 删除空行和仅含空格的行。 |
| `File exceeds allowed size` | 请求大于 200 MiB。 | 减小文件大小，或将值拆分到更小的文件中。 |
| `Too many rows in file` | 某个文件包含超过 10,000 行数据。 | 将值拆分到更小的文件中。 |
| `File not found` | 筛选中的文件名与上传的文件不匹配。 | 确保筛选中引用的文件名与上传文件名完全匹配。 |
| `Invalid file format` | CSV 表头或 JSON 键与筛选字段不匹配。 | 检查 CSV 表头是否与筛选字段名称匹配，或确保 JSON 是对象数组。 |
| `Field not found` | 数据集中不存在该字段。 | 使用[获取数据集元数据](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-dataset-metadata.md)核对字段名。 |

## Filter 返回什么？

Filter 返回 `snapshot_id`，即该请求创建的筛选快照的 ID：

```json theme={null}
{ "snapshot_id": "s_m123456789" }
```

任务完成后，将 `snapshot_id` 用于快照端点：

* [获取快照元数据](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-snapshot-meta.md)，检查任务状态。
* [获取快照部分](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-snapshot-parts.md)，用于大型或拆分的快照。
* [快照内容](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/download-the-file-by-snapshot_id.md)，下载记录。
* [交付快照](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/deliver-snapshot.md)，将记录发送到已配置的目标位置。

## Filter 的费用是多少？

Filter 的费用为 \$2.5 CPM（每返回 1,000 条记录），与市场价格相同。筛选返回 0 条记录时不收费。

## Filter 可能返回哪些错误？

| 状态码 | 含义 | 处理方式 |
| - | - | - |
| `400` | 筛选或参数有误 | 使用[获取数据集元数据](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-dataset-metadata.md)核对字段名。 |
| `401` | API Key 错误或缺失 | 在 `Authorization` 请求头中发送有效的 API Key。 |
| `402` | 余额不足 | 充值或减小 `records_limit`。 |
| `404` | 未知的 `dataset_id` | 确认数据集 ID。 |
| `422` | 筛选未匹配任何记录 | 放宽筛选条件或检查字段值。 |
| `429` | 并行任务过多（每个数据集最多 25 个运行中任务）或触发速率限制（5 次/分钟或 120 次/小时） | 等待运行中的任务完成，然后以退避方式重试。如需减少任务数量，可将兼容的筛选条件合并为一个范围更广的任务。 |

## 相关文档

* [数据集列表](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-dataset-list.md)，查找可用的市场数据集。
* [获取数据集元数据](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/get-dataset-metadata.md)，查看数据集字段和类型。
* [筛选语法参考](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/filter-syntax.md)，了解所有运算符、筛选组和嵌套。
* [Search 数据集（同步）](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/search-dataset.md)，用于实时查询。
* [Dataset API 概述](https://docs.brightdata.com/cn/api-reference/marketplace-dataset-api/overview.md)


## OpenAPI

````yaml cn-dca-api POST /datasets/filter
openapi: 3.1.0
info:
  title: Brightdata API
  description: 用于与数据集市场交互的 API
  version: 1.0.0
servers:
  - url: https://api.brightdata.com
security:
  - bearerAuth: []
paths:
  /datasets/filter:
    post:
      description: 基于提供的过滤条件创建数据集快照
      operationId: filterDataset
      parameters:
        - name: dataset_id
          in: query
          description: 要过滤的数据集 ID（在 multipart/form-data 模式下为必填）
          required: false
          schema:
            type: string
            example: gd_l1viktl72bvl7bjuj0
        - name: records_limit
          description: 限制快照中包含的记录数量
          in: query
          required: false
          schema:
            type: integer
            example: 1000
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - dataset_id
                - filter
              properties:
                dataset_id:
                  type: string
                  description: 要过滤的数据集 ID
                  example: gd_l1viktl72bvl7bjuj0
                records_limit:
                  type: integer
                  description: 限制快照中包含的记录数量
                  example: 1000
                filter:
                  $ref: '#/components/schemas/DatasetFilter'
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/FilterDatasetBody'
      responses:
        '200':
          description: 快照创建任务已成功启动
          content:
            application/json:
              schema:
                type: object
                properties:
                  snapshot_id:
                    type: string
                    description: 快照 ID
        '400':
          description: 错误请求
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidationErrorBody'
              example:
                validation_errors:
                  - '"filter.filters[0].invalid_prop" 不被允许'
                  - '"records_limit" 必须是正数'
        '402':
          description: 余额不足，无法创建快照
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorBody'
              example:
                error: 您当前的余额不足以处理此数据采集请求。请向账户添加资金或调整请求以继续。($1 缺失)
        '422':
          description: 提供的过滤条件未匹配到任何记录
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorBody'
              example:
                error: 提供的过滤条件未匹配到任何记录
        '429':
          description: 并行任务过多
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ErrorBody'
              example:
                error: 每个数据集的最大任务限制 25 已超出
components:
  schemas:
    DatasetFilter:
      anyOf:
        - $ref: '#/components/schemas/DatasetFilterItem'
          title: 单字段过滤器
        - $ref: '#/components/schemas/DatasetFilterGroup'
          title: 过滤器组
        - $ref: '#/components/schemas/DatasetFilterItemNoVal'
          title: 无值单字段过滤器
    FilterDatasetBody:
      type: object
      required:
        - filter
      properties:
        filter:
          $ref: '#/components/schemas/DatasetFilter'
    ValidationErrorBody:
      type: object
      properties:
        validation_errors:
          type: array
          items:
            type: string
    ErrorBody:
      type: object
      properties:
        error:
          type: string
    DatasetFilterItem:
      type: object
      required:
        - name
        - operator
        - value
      additionalProperties: false
      properties:
        name:
          type: string
          description: 要过滤的字段名称
        operator:
          type: string
          enum:
            - '='
            - '!='
            - '>'
            - <
            - '>='
            - <=
            - in
            - not_in
            - includes
            - not_includes
            - array_includes
            - not_array_includes
        value:
          description: 用于过滤的值
          oneOf:
            - type: string
            - type: number
            - type: boolean
            - type: object
            - type: array
              items:
                oneOf:
                  - type: string
                  - type: number
                  - type: boolean
      example:
        name: name
        operator: '='
        value: John
    DatasetFilterGroup:
      type: object
      required:
        - operator
        - filters
      additionalProperties: false
      properties:
        operator:
          type: string
          enum:
            - and
            - or
        combine_nested_fields:
          type: boolean
          description: 针对对象数组：如果为 true，则所有过滤器必须在单个对象内匹配
        filters:
          type: array
          items:
            $ref: '#/components/schemas/DatasetFilter'
      example:
        operator: and
        filters:
          - name: name
            operator: '='
            value: John
          - name: age
            operator: '>'
            value: '30'
    DatasetFilterItemNoVal:
      type: object
      required:
        - name
        - operator
      additionalProperties: false
      properties:
        name:
          type: string
          description: 要过滤的字段名称
        operator:
          type: string
          enum:
            - is_null
            - is_not_null
      example:
        name: reviews_count
        operator: is_not_null
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: >-
        在 Authorization 头中使用您的 Bright Data API Key 作为 Bearer token。


        **认证方法:**

        1. 从 Bright Data 账户设置获取您的 API Key:
        https://brightdata.com/cp/setting/users

        2. 在请求的 Authorization 头中包含 API Key

        3. 格式: `Authorization: Bearer YOUR_API_KEY`


        **示例:**

        ```

        Authorization: Bearer
        b5648e1096c6442f60a6c4bbbe73f8d2234d3d8324554bd6a7ec8f3f251f07df

        ```


        了解如何获取 Bright Data API Key:
        https://docs.brightdata.com/cn/api-reference/authentication#如何生成新的-api-key？
      bearerFormat: API Key

````