> ## 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 /ja/api-reference/openapi_knowledge.json post /datasets/{dataset_id}/retrieve
openapi: 3.0.1
info:
  title: ナレッジAPI
  description: >-
    ナレッジベース、ドキュメント、チャンク、メタデータ、タグの管理（作成、取得、設定を含む）のための API です。**注意：**単一のナレッジベース
    API キーは、同じアカウント配下のすべての可視ナレッジベースを操作する権限を持ちます。データセキュリティにご注意ください。
  version: 1.0.0
servers:
  - url: https://{api_base_url}
    description: Knowledge 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: データソースプラグインとパイプライン実行を含むナレッジパイプラインの管理と実行のための操作です。
paths:
  /datasets/{dataset_id}/retrieve:
    post:
      tags:
        - データセット
      summary: ナレッジベースからチャンクを取得 / テスト検索
      description: >-
        ナレッジベースに対して検索クエリを実行し、最も関連性の高いチャンクを取得します。このエンドポイントは本番環境の検索とテスト検索の両方に使用できます。
      operationId: retrieveSegments
      parameters:
        - name: dataset_id
          in: path
          required: true
          schema:
            type: string
            format: uuid
          description: ナレッジベース ID です。
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - query
              properties:
                query:
                  type: string
                  maxLength: 250
                  description: 検索クエリテキストです。
                retrieval_model:
                  $ref: '#/components/schemas/RetrievalModel'
                  description: 検索モデルの設定です。このナレッジベースをクエリする際のチャンクの検索方法とランキング方法を制御します。
                external_retrieval_model:
                  type: object
                  description: 外部ナレッジベースの検索設定です。
                  properties:
                    top_k:
                      type: integer
                      description: 返す結果の最大数です。
                    score_threshold:
                      type: number
                      description: 結果フィルタリング用の最小関連性スコア閾値です。
                    score_threshold_enabled:
                      type: boolean
                      description: スコア閾値フィルタリングが有効かどうかです。
                attachment_ids:
                  type: array
                  items:
                    type: string
                  nullable: true
                  description: 検索コンテキストに含める添付ファイル ID のリストです。
      responses:
        '200':
          description: 検索結果です。
          content:
            application/json:
              schema:
                type: object
                properties:
                  query:
                    type: object
                    description: 元のクエリオブジェクトです。
                    properties:
                      content:
                        type: string
                        description: クエリテキストです。
                  records:
                    type: array
                    items:
                      type: object
                      properties:
                        segment:
                          type: object
                          description: ナレッジベースから一致したチャンクです。
                          properties:
                            id:
                              type: string
                              description: チャンクの一意識別子です。
                            position:
                              type: integer
                              description: ドキュメント内のチャンクの位置。
                            document_id:
                              type: string
                              description: このチャンクが属するドキュメントの ID です。
                            content:
                              type: string
                              description: チャンクのテキスト内容です。
                            sign_content:
                              type: string
                              description: 整合性検証用の署名付きコンテンツハッシュです。
                            answer:
                              type: string
                              description: 回答コンテンツです。Q&A モードのドキュメントで使用されます。
                            word_count:
                              type: integer
                              description: チャンク内容の単語数です。
                            tokens:
                              type: integer
                              description: チャンク内容のトークン数です。
                            keywords:
                              type: array
                              description: キーワードベースの検索のためにこのチャンクに関連付けられたキーワードです。
                              items:
                                type: string
                            index_node_id:
                              type: string
                              description: ベクトルストア内のインデックスノードの ID です。
                            index_node_hash:
                              type: string
                              description: インデックスされたコンテンツのハッシュです。変更の検出に使用されます。
                            hit_count:
                              type: integer
                              description: このチャンクが検索クエリでマッチした回数です。
                            enabled:
                              type: boolean
                              description: このチャンクが検索に対して有効かどうかです。
                            disabled_at:
                              type: number
                              nullable: true
                              description: チャンクが無効化されたタイムスタンプです。有効な場合は `null` です。
                            disabled_by:
                              type: string
                              nullable: true
                              description: チャンクを無効化したユーザーの ID です。有効な場合は `null` です。
                            status:
                              type: string
                              description: チャンクのインデックスステータスです。
                            created_by:
                              type: string
                              description: チャンクを作成したユーザーの ID です。
                            created_at:
                              type: number
                              description: 作成タイムスタンプ（Unix エポック、秒単位）です。
                            indexing_at:
                              type: number
                              nullable: true
                              description: インデックス作成が開始されたタイムスタンプです。まだ開始されていない場合は `null` です。
                            completed_at:
                              type: number
                              nullable: true
                              description: インデックス作成が完了したタイムスタンプです。まだ完了していない場合は `null` です。
                            error:
                              type: string
                              nullable: true
                              description: インデックス作成が失敗した場合のエラーメッセージです。エラーなしの場合は `null` です。
                            stopped_at:
                              type: number
                              nullable: true
                              description: インデックス作成が停止されたタイムスタンプです。停止されていない場合は `null` です。
                            document:
                              type: object
                              description: マッチしたチャンクの親ドキュメント情報です。
                              properties:
                                id:
                                  type: string
                                  description: ドキュメントの一意識別子です。
                                data_source_type:
                                  type: string
                                  description: ドキュメントの作成方法です。
                                name:
                                  type: string
                                  description: ドキュメント名です。
                                doc_type:
                                  type: string
                                  nullable: true
                                  description: ドキュメントタイプの分類です。未設定の場合は `null` です。
                                doc_metadata:
                                  type: object
                                  nullable: true
                                  description: ドキュメントのメタデータ値です。メタデータが設定されていない場合は `null` です。
                        child_chunks:
                          type: array
                          description: 階層インデックスを使用している場合、チャンク内でマッチした子チャンクです。
                          items:
                            type: object
                            properties:
                              id:
                                type: string
                                description: 子チャンクの一意識別子です。
                              content:
                                type: string
                                description: 子チャンクのテキスト内容です。
                              position:
                                type: integer
                                description: 親チャンク内の子チャンクの位置です。
                              score:
                                type: number
                                description: 子チャンクの関連性スコアです。
                        score:
                          type: number
                          description: 関連性スコアです。
                        tsne_position:
                          type: object
                          nullable: true
                          description: t-SNE 可視化の位置です。
                        files:
                          type: array
                          description: このチャンクに添付されたファイルです。
                          items:
                            type: object
                            properties:
                              id:
                                type: string
                                description: 添付ファイルの識別子です。
                              name:
                                type: string
                                description: 元のファイル名です。
                              size:
                                type: integer
                                description: ファイルサイズ（バイト）。
                              extension:
                                type: string
                                description: ファイル拡張子。
                              mime_type:
                                type: string
                                description: ファイルの MIME タイプ。
                              source_url:
                                type: string
                                description: 添付ファイルにアクセスする URL です。
                        summary:
                          type: string
                          nullable: true
                          description: 要約インデックス経由で取得された場合の要約コンテンツです。
                    description: 一致した検索レコードのリスト。
              examples:
                success:
                  summary: レスポンス例
                  value:
                    query:
                      content: What is Dify?
                    records:
                      - segment:
                          id: f3d1c7be-9f3a-40d8-8eb8-3a1ef9c3f2c1
                          position: 1
                          document_id: a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac
                          content: Dify is an open-source LLM app development platform.
                          sign_content: ''
                          answer: ''
                          word_count: 9
                          tokens: 12
                          keywords:
                            - dify
                            - platform
                            - llm
                          index_node_id: a1b2c3d4-e5f6-7890-abcd-000000000001
                          index_node_hash: abc123def456
                          hit_count: 1
                          enabled: true
                          disabled_at: null
                          disabled_by: null
                          status: completed
                          created_by: ad313dd6-ef04-4dd1-a5b0-c0f0b9e2e7e4
                          created_at: 1741267200
                          indexing_at: 1741267200
                          completed_at: 1741267200
                          error: null
                          stopped_at: null
                          document:
                            id: a8e0e5b5-78c6-4130-a5ce-25feb0e0b4ac
                            data_source_type: upload_file
                            name: guide.txt
                            doc_type: null
                            doc_metadata: null
                        child_chunks: []
                        score: 0.92
                        tsne_position: null
                        files: []
                        summary: null
        '400':
          description: >-
            - `dataset_not_initialized` :
            ナレッジベースはまだ初期化中またはインデキシング中です。しばらくお待ちください。

            - `provider_not_initialize` : 有効なモデルプロバイダーの認証情報が見つかりません。設定 →
            モデルプロバイダーで認証情報を完了してください。

            - `provider_quota_exceeded` : Your quota for Dify Hosted OpenAI has
            been exhausted. Please go to Settings -> Model Provider to complete
            your own provider credentials.

            - `model_currently_not_support` : Dify Hosted OpenAI trial currently
            not support the GPT-4 model.

            - `completion_request_error` : Completion request failed.

            - `invalid_param` : Invalid parameter value.
          content:
            application/json:
              examples:
                dataset_not_initialized:
                  summary: dataset_not_initialized
                  value:
                    status: 400
                    code: dataset_not_initialized
                    message: >-
                      The dataset is still being initialized or indexing. Please
                      wait a moment.
                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.
                provider_quota_exceeded:
                  summary: provider_quota_exceeded
                  value:
                    status: 400
                    code: provider_quota_exceeded
                    message: >-
                      Your quota for Dify Hosted OpenAI has been exhausted.
                      Please go to Settings -> Model Provider to complete your
                      own provider credentials.
                model_currently_not_support:
                  summary: model_currently_not_support
                  value:
                    status: 400
                    code: model_currently_not_support
                    message: >-
                      Dify Hosted OpenAI trial currently not support the GPT-4
                      model.
                completion_request_error:
                  summary: completion_request_error
                  value:
                    status: 400
                    code: completion_request_error
                    message: Completion request failed.
                invalid_param:
                  summary: invalid_param
                  value:
                    status: 400
                    code: invalid_param
                    message: Invalid parameter value.
        '403':
          description: '`forbidden` : 権限が不足しています。'
          content:
            application/json:
              examples:
                forbidden:
                  summary: forbidden
                  value:
                    status: 403
                    code: forbidden
                    message: Insufficient permissions.
        '404':
          description: '`not_found` : ナレッジベースが見つかりません。'
          content:
            application/json:
              examples:
                not_found:
                  summary: not_found
                  value:
                    status: 404
                    code: not_found
                    message: Dataset not found.
        '500':
          description: '`internal_server_error` : 検索中に内部エラーが発生しました。'
          content:
            application/json:
              examples:
                internal_server_error:
                  summary: internal_server_error
                  value:
                    status: 500
                    code: internal_server_error
                    message: An internal error occurred.
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
  securitySchemes:
    ApiKeyAuth:
      type: http
      scheme: bearer
      bearerFormat: API_KEY
      description: >-
        API Key 認証です。すべての API リクエストにおいて、`Authorization` HTTP ヘッダーに `Bearer `
        プレフィックスを付けた API Key を含めてください。例：`Authorization: Bearer {API_KEY}`。**API
        Key はサーバーサイドに保存し、クライアントサイドで共有・保存しないことを強く推奨します。API Key
        の漏洩は深刻な結果につながる可能性があります。**

````