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

# 用文本更新文档

> 更新现有文档的文本内容、名称或处理配置。内容变更时将重新触发索引。



## OpenAPI

````yaml /zh/api-reference/openapi_service.json post /datasets/{dataset_id}/documents/{document_id}/update-by-text
openapi: 3.0.1
info:
  title: Dify 服务 API
  description: 用于 Dify 应用与知识库的 REST API。应用类接口使用应用 API 密钥认证，知识库类接口使用知识库 API 密钥认证。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Dify 服务 API 的基础 URL。自部署时，替换为你的 API 基础 URL。
    variables:
      api_base_url:
        default: api.dify.ai/v1
        description: API 基础 URL 的主机与路径，不含 `https://` 前缀。
security:
  - ApiKeyAuth: []
tags:
  - name: 对话消息
    description: 与聊天消息和交互相关的操作。
  - name: 文件操作
    description: 文件上传和预览操作。
  - name: 终端用户
    description: 终端用户信息相关操作。
  - name: 消息反馈
    description: 用户反馈操作。
  - name: 会话管理
    description: 与管理会话相关的操作。
  - name: 语音与文字转换
    description: 文字转语音和语音转文字操作。
  - name: 应用配置
    description: 获取应用设置和信息的操作。
  - name: 标注管理
    description: 与管理标注直接回复相关的操作。
  - name: 人工介入
    description: 暂停等待人工输入的工作流恢复操作。
  - name: 工作流运行
    description: 用于执行和管理工作流的操作。
  - name: 文本生成消息
    description: 文本生成相关操作。
  - name: 知识库
    description: 用于管理知识库的操作，包括创建、配置和检索。
  - name: 文档
    description: 用于在知识库中创建、更新和管理文档的操作。
  - name: 分段
    description: 用于管理分段和子分段的操作。
  - name: 元数据
    description: 用于管理知识库元数据字段和文档元数据值的操作。
  - name: 标签
    description: 用于管理知识库标签和标签绑定的操作。
  - name: 模型
    description: 用于获取可用模型的操作。
  - name: 知识流水线
    description: 用于管理和运行知识流水线的操作，包括数据源插件和流水线执行。
paths:
  /datasets/{dataset_id}/documents/{document_id}/update-by-text:
    post:
      tags:
        - 文档
      summary: 用文本更新文档
      description: 更新现有文档的文本内容、名称或处理配置。内容变更时将重新触发索引。
      operationId: updateDocumentByText
      parameters:
        - name: dataset_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: 知识库 ID。
        - name: document_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: 文档 ID.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                name:
                  type: string
                  description: 文档名称。当提供 `text` 时必填。
                text:
                  type: string
                  description: 文档文本内容。
                process_rule:
                  type: object
                  description: 分块处理规则。
                  required:
                    - mode
                  properties:
                    mode:
                      type: string
                      enum:
                        - automatic
                        - custom
                        - hierarchical
                      description: >-
                        处理模式。`automatic` 使用内置规则，`custom` 允许手动配置，`hierarchical`
                        启用父子分段结构（配合 `doc_form: hierarchical_model` 使用）。
                    rules:
                      type: object
                      properties:
                        pre_processing_rules:
                          type: array
                          items:
                            type: object
                            properties:
                              id:
                                type: string
                                enum:
                                  - remove_stopwords
                                  - remove_extra_spaces
                                  - remove_urls_emails
                                description: 规则标识符。
                              enabled:
                                type: boolean
                                description: 该预处理规则是否启用。
                        segmentation:
                          type: object
                          properties:
                            separator:
                              type: string
                              default: |+

                              description: 用于拆分文本的自定义分隔符。
                            max_tokens:
                              type: integer
                              description: 每个分段的最大令牌数。
                            chunk_overlap:
                              type: integer
                              default: 0
                              description: 分段之间的令牌重叠数。
                doc_form:
                  type: string
                  enum:
                    - text_model
                    - hierarchical_model
                    - qa_model
                  default: text_model
                  description: >-
                    `text_model` 为标准文本分段，`hierarchical_model` 为父子分段结构，`qa_model`
                    为问答对提取。
                doc_language:
                  type: string
                  default: English
                  description: 用于处理优化的文档语言。
                retrieval_model:
                  $ref: '#/components/schemas/RetrievalModel'
                  description: 检索模型配置。控制查询此知识库时如何搜索和排序分段。
      responses:
        '200':
          description: 文档更新成功。
          content:
            application/json:
              schema:
                type: object
                properties:
                  document:
                    $ref: '#/components/schemas/Document'
                  batch:
                    type: string
                    description: 用于跟踪索引进度的批次 ID。
              examples:
                success:
                  summary: 响应示例
                  value:
                    document:
                      id: a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac
                      position: 1
                      data_source_type: upload_file
                      data_source_info:
                        upload_file_id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                      data_source_detail_dict:
                        upload_file:
                          id: a1b2c3d4-e5f6-7890-abcd-ef1234567890
                          name: guide.txt
                          size: 2048
                          extension: txt
                          mime_type: text/plain
                          created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                          created_at: 1741267200
                      dataset_process_rule_id: e1f2a3b4-c5d6-7890-ef12-345678901234
                      name: guide.txt
                      created_from: api
                      created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                      created_at: 1741267200
                      tokens: 512
                      indexing_status: completed
                      error: null
                      enabled: true
                      disabled_at: null
                      disabled_by: null
                      archived: false
                      display_status: available
                      word_count: 350
                      hit_count: 0
                      doc_form: text_model
                      doc_metadata: []
                      summary_index_status: null
                      need_summary: false
                    batch: '20250306150245647595'
        '400':
          description: >-
            - `provider_not_initialize` : 未找到有效的模型供应商凭据。前往 Settings -> Model
            Provider 补全供应商凭据。

            - `invalid_param` : 提供 `text` 时必须提供 `name`，或 `doc_form` 无效。

            - `invalid_param` : 文档不可用，无法更新（仅可更新处于可用状态的文档）。
          content:
            application/json:
              examples:
                provider_not_initialize:
                  summary: provider_not_initialize
                  value:
                    status: 400
                    code: provider_not_initialize
                    message: >-
                      No valid model provider credentials found. Please go to
                      Settings -> Model Provider to complete your provider
                      credentials.
                invalid_param_name:
                  summary: invalid_param (name required)
                  value:
                    status: 400
                    code: invalid_param
                    message: name is required when text is provided.
                invalid_param_not_available:
                  summary: invalid_param (not available)
                  value:
                    status: 400
                    code: invalid_param
                    message: Document is not available
        '403':
          description: |-
            - `forbidden` : 该知识库未启用 API 访问。
            - `forbidden` : 向量空间容量已达到您订阅套餐的上限。
            - `forbidden` : 已达到您订阅套餐的知识库请求速率限制。
          content:
            application/json:
              examples:
                forbidden_1:
                  summary: forbidden (api access)
                  value:
                    status: 403
                    code: forbidden
                    message: Dataset api access is not enabled.
                forbidden_2:
                  summary: forbidden (vector space)
                  value:
                    status: 403
                    code: forbidden
                    message: >-
                      The capacity of the vector space has reached the limit of
                      your subscription.
                forbidden_3:
                  summary: forbidden (rate limit)
                  value:
                    status: 403
                    code: forbidden
                    message: >-
                      Sorry, you have reached the knowledge base request rate
                      limit of your subscription.
        '404':
          description: |-
            - `not_found` : 未找到知识库。
            - `not_found` : 未找到文档。
          content:
            application/json:
              examples:
                not_found_1:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Dataset not found.
                not_found_2:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Document not found.
components:
  schemas:
    RetrievalModel:
      type: object
      required:
        - search_method
        - reranking_enable
        - top_k
        - score_threshold_enabled
      properties:
        search_method:
          type: string
          description: 用于检索的搜索方法。
          enum:
            - keyword_search
            - semantic_search
            - full_text_search
            - hybrid_search
        reranking_enable:
          type: boolean
          description: 是否启用重排序。
        reranking_model:
          type: object
          description: 重排序模型配置。
          properties:
            reranking_provider_name:
              type: string
              description: 重排序模型的供应商名称。
            reranking_model_name:
              type: string
              description: 重排序模型名称。
        reranking_mode:
          type: string
          enum:
            - reranking_model
            - weighted_score
          nullable: true
          description: 重排序模式。当 `reranking_enable` 为 `true` 时必填。
        top_k:
          type: integer
          description: 返回的最大结果数。
        score_threshold_enabled:
          type: boolean
          description: 是否启用分数阈值过滤。
        score_threshold:
          type: number
          nullable: true
          description: 结果的最低相关性分数。仅在 `score_threshold_enabled` 为 `true` 时生效。
        weights:
          type: object
          nullable: true
          description: 混合搜索的权重配置。
          properties:
            weight_type:
              type: string
              description: 平衡语义搜索和关键词搜索权重的策略。
              enum:
                - semantic_first
                - keyword_first
                - customized
            vector_setting:
              type: object
              description: 语义搜索权重设置。
              properties:
                vector_weight:
                  type: number
                  description: 分配给语义（向量）搜索结果的权重。
                embedding_provider_name:
                  type: string
                  description: 用于向量搜索的嵌入模型供应商。
                embedding_model_name:
                  type: string
                  description: 用于向量搜索的嵌入模型名称。
            keyword_setting:
              type: object
              description: 关键词搜索权重设置。
              properties:
                keyword_weight:
                  type: number
                  description: 分配给关键词搜索结果的权重。
        metadata_filtering_conditions:
          type: object
          nullable: true
          description: 仅检索文档元数据匹配指定条件的分段。条件由服务端基于文档元数据字段评估。
          properties:
            logical_operator:
              type: string
              enum:
                - and
                - or
              default: and
              nullable: true
              description: 多个条件之间的组合方式。
            conditions:
              type: array
              nullable: true
              description: 要评估的元数据条件列表。
              items:
                type: object
                required:
                  - name
                  - comparison_operator
                properties:
                  name:
                    type: string
                    description: 用于比较的元数据字段名称。
                  comparison_operator:
                    type: string
                    description: >-
                      应用的比较方式。字符串运算符（`contains`、`not contains`、`start with`、`end
                      with`、`is`、`is not`、`empty`、`not empty`、`in`、`not
                      in`）作用于字符串或数组类型的元数据。数值运算符（`=`、`≠`、`>`、`<`、`≥`、`≤`）作用于数值类型的元数据。时间运算符（`before`、`after`）作用于时间类型的元数据。
                    enum:
                      - contains
                      - not contains
                      - start with
                      - end with
                      - is
                      - is not
                      - empty
                      - not empty
                      - in
                      - not in
                      - '='
                      - ≠
                      - '>'
                      - <
                      - ≥
                      - ≤
                      - before
                      - after
                  value:
                    nullable: true
                    description: >-
                      用于比较的值。类型取决于 `comparison_operator`：大多数字符串运算符使用字符串；`in` 和
                      `not in` 使用字符串数组；数值运算符使用数字；`empty` 和 `not empty` 时省略。
                    oneOf:
                      - type: string
                      - type: array
                        items:
                          type: string
                      - type: number
    Document:
      type: object
      properties:
        id:
          type: string
          description: 文档的唯一标识符。
        position:
          type: integer
          description: 文档在列表中的显示位置。
        data_source_type:
          type: string
          description: 文档的创建方式。文件上传为 `upload_file`，Notion 导入为 `notion_import`。
        data_source_info:
          type: object
          description: 原始数据源信息，随 `data_source_type` 而异。
        data_source_detail_dict:
          type: object
          description: 详细的数据源信息，包括文件详情。
        dataset_process_rule_id:
          type: string
          description: 应用于该文档的处理规则 ID。
        name:
          type: string
          description: 文档名称。
        created_from:
          type: string
          description: 文档来源。通过 API 创建时为 `api`，通过 UI 创建时为 `web`。
        created_by:
          type: string
          description: 创建该文档的用户 ID。
        created_at:
          type: number
          description: 创建时间戳（Unix 纪元，单位为秒）。
        tokens:
          type: integer
          description: 文档中的令牌总数。
        indexing_status:
          type: string
          description: >-
            当前索引状态。`waiting` 表示排队中，`parsing` 表示正在提取内容，`cleaning`
            表示正在去噪，`splitting` 表示正在分块，`indexing` 表示正在构建向量，`completed`
            表示就绪，`error` 表示失败，`paused` 表示手动暂停。
        error:
          type: string
          nullable: true
          description: 索引失败时的错误消息。无错误时为 `null`。
        enabled:
          type: boolean
          description: 该文档是否启用检索。
        disabled_at:
          type: number
          nullable: true
          description: 文档被禁用的时间戳。启用时为 `null`。
        disabled_by:
          type: string
          nullable: true
          description: 禁用该文档的用户 ID。启用时为 `null`。
        archived:
          type: boolean
          description: 文档是否已归档。
        display_status:
          type: string
          description: 基于 `indexing_status` 和 `enabled` 状态派生的面向用户的显示状态。
        word_count:
          type: integer
          description: 文档的总字数。
        hit_count:
          type: integer
          description: 该文档在检索查询中被匹配的次数。
        doc_form:
          type: string
          description: >-
            文档分块模式。`text_model` 表示标准文本分块，`hierarchical_model` 表示父子结构，`qa_model`
            表示问答对提取。
        doc_metadata:
          type: array
          description: 分配给该文档的元数据值。
          items:
            type: object
            properties:
              id:
                type: string
                description: 元数据字段标识符。
              name:
                type: string
                description: 元数据字段名称。
              type:
                type: string
                description: 元数据字段值类型。
              value:
                type: string
                description: 此文档的元数据值。
        summary_index_status:
          type: string
          nullable: true
          description: 该文档的摘要索引状态。未配置摘要索引时为 `null`。
        need_summary:
          type: boolean
          description: 是否需要为该文档生成摘要。
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 认证。对于所有 API 请求，请在 `Authorization` HTTP 头中包含您的 API Key，并加上
        `Bearer ` 前缀。示例：`Authorization: Bearer {API_KEY}`。**强烈建议将 API Key
        存储在服务端，不要在客户端共享或存储，以避免 API Key 泄漏导致严重后果。**缺少或无效的 API Key 会返回 HTTP
        `401`，错误码为 `unauthorized`。

````