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

# Search a knowledge base by natural-language query

> Run a similarity search over a knowledge base.

Embeds the query with the knowledge base's configured embedding
model and returns the top-K most similar chunks.

Args:
    body (`SearchKnowledgeBaseRequest`):
        The query text and ``top_k``.
    knowledge_base_id (`str`):
        The knowledge base to search.
    user_id (`str`):
        Injected authenticated user ID.
    service (`KnowledgeBaseService`):
        Injected knowledge base service.

Returns:
    `SearchKnowledgeBaseResponse`:
        Matched chunks ordered by descending similarity.



## OpenAPI

````yaml /versions/2.0.4dev/en/deploy/openapi.json post /knowledge_bases/{knowledge_base_id}/search
openapi: 3.1.0
info:
  title: AgentScope
  version: 2.0.3
servers: []
security: []
paths:
  /knowledge_bases/{knowledge_base_id}/search:
    post:
      tags:
        - knowledge_bases
      summary: Search a knowledge base by natural-language query
      description: |-
        Run a similarity search over a knowledge base.

        Embeds the query with the knowledge base's configured embedding
        model and returns the top-K most similar chunks.

        Args:
            body (`SearchKnowledgeBaseRequest`):
                The query text and ``top_k``.
            knowledge_base_id (`str`):
                The knowledge base to search.
            user_id (`str`):
                Injected authenticated user ID.
            service (`KnowledgeBaseService`):
                Injected knowledge base service.

        Returns:
            `SearchKnowledgeBaseResponse`:
                Matched chunks ordered by descending similarity.
      operationId: search_knowledge_base_knowledge_bases__knowledge_base_id__search_post
      parameters:
        - name: knowledge_base_id
          in: path
          required: true
          schema:
            type: string
            description: The knowledge base id.
            title: Knowledge Base Id
          description: The knowledge base id.
        - name: x-user-id
          in: header
          required: true
          schema:
            type: string
            description: >-
              Caller's user ID. Temporary header-based identity; will be
              replaced by JWT auth.
            title: X-User-Id
          description: >-
            Caller's user ID. Temporary header-based identity; will be replaced
            by JWT auth.
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchKnowledgeBaseRequest'
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/SearchKnowledgeBaseResponse'
        '404':
          description: Not found
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/HTTPValidationError'
components:
  schemas:
    SearchKnowledgeBaseRequest:
      properties:
        query:
          type: string
          title: Query
          description: The natural-language search query.
        top_k:
          type: integer
          maximum: 50
          minimum: 1
          title: Top K
          description: Maximum number of results to return.
          default: 5
      type: object
      required:
        - query
      title: SearchKnowledgeBaseRequest
      description: Request body for searching a knowledge base.
    SearchKnowledgeBaseResponse:
      properties:
        results:
          items:
            $ref: '#/components/schemas/VectorSearchResult'
          type: array
          title: Results
          description: Matched chunks ordered by descending similarity score.
        total:
          type: integer
          title: Total
          description: Total number of returned results.
      type: object
      required:
        - results
        - total
      title: SearchKnowledgeBaseResponse
      description: Response body for a knowledge base search.
    HTTPValidationError:
      properties:
        detail:
          items:
            $ref: '#/components/schemas/ValidationError'
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    VectorSearchResult:
      properties:
        score:
          type: number
          title: Score
        document_id:
          type: string
          title: Document Id
        chunk:
          $ref: '#/components/schemas/Chunk'
      type: object
      required:
        - score
        - document_id
        - chunk
      title: VectorSearchResult
      description: |-
        A single result returned by a similarity search.

        Pairs the matched :class:`Chunk` with its similarity score.
        ``Chunk`` is intentionally not extended with a ``score`` field so
        its semantics stay stable; instead the score lives in this
        wrapper whose only purpose is "I am a query hit."
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
        input:
          title: Input
        ctx:
          type: object
          title: Context
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError
    Chunk:
      properties:
        content:
          anyOf:
            - $ref: '#/components/schemas/TextBlock'
            - $ref: '#/components/schemas/DataBlock'
          title: Content
        source:
          type: string
          title: Source
        chunk_index:
          type: integer
          title: Chunk Index
        total_chunks:
          type: integer
          title: Total Chunks
        metadata:
          additionalProperties: true
          type: object
          title: Metadata
      type: object
      required:
        - content
        - source
        - chunk_index
        - total_chunks
      title: Chunk
      description: |-
        A final indexable chunk produced by a :class:`ChunkerBase`.

        Each ``Chunk`` corresponds to one record in the vector store.
        The required structural fields (``source``, ``chunk_index``,
        ``total_chunks``) enable downstream features such as "expand
        context around a hit" during retrieval.
    TextBlock:
      properties:
        type:
          type: string
          const: text
          title: Type
          default: text
        text:
          type: string
          title: Text
        id:
          type: string
          title: Id
      type: object
      required:
        - text
      title: TextBlock
      description: The text block.
    DataBlock:
      properties:
        type:
          type: string
          const: data
          title: Type
          default: data
        id:
          type: string
          title: Id
        source:
          anyOf:
            - $ref: '#/components/schemas/Base64Source'
            - $ref: '#/components/schemas/URLSource'
          title: Source
        name:
          anyOf:
            - type: string
            - type: 'null'
          title: Name
      type: object
      required:
        - source
      title: DataBlock
      description: The data block for binary content (images, audio, video, etc.).
    Base64Source:
      properties:
        type:
          type: string
          const: base64
          title: Type
          default: base64
        data:
          type: string
          title: Data
        media_type:
          type: string
          title: Media Type
      type: object
      required:
        - data
        - media_type
      title: Base64Source
      description: The base64 source.
    URLSource:
      properties:
        type:
          type: string
          const: url
          title: Type
          default: url
        url:
          type: string
          minLength: 1
          format: uri
          title: Url
        media_type:
          type: string
          title: Media Type
      type: object
      required:
        - url
        - media_type
      title: URLSource
      description: The URL source.

````