Skip to content

search_memories

search_memories(
    space_id: SpaceId,
    actor_id: ActorId,
    query: SearchQuery,
    *,
    config_overrides: Optional[OpenSearchMemoryClientConfig] = None,
    memory_strategies: Optional[StrategySelectorList] = None,
    sort: Optional[SearchSort] = None,
    max_results: Optional[MaxResults] = None,
    next_token: Optional[PaginationToken] = None,
) -> SearchMemoriesOutput

Searches a space's memories by relevance. The query is a subset of the OpenSearch Query DSL. The service validates it, limits the search to the requested strategies and to the actor in actorId, and returns one ranked list of memories. You can page through only a limited number of results. A request past that limit fails with ValidationException (reason invalidNextToken), and the message gives the limit.

Memories past their expiry (set from the space's memoryExpiryDays when each memory is written) are left out of the results. Only current memories are returned: a memory that consolidation has superseded (an older version of a fact that a newer memory replaced) is left out.

If the results are larger than 5 MiB, the request fails with ValidationException (reason queryBodyTooLarge). Lower maxResults.

If the request would exceed the service's rate for embedding requests, it fails with ThrottlingException. Retry with exponential backoff.

If the space uses a customer managed AWS KMS key, the call can also fail because of the key. It fails with AccessDeniedException if you aren't allowed to use the key, and with ThrottlingException if AWS KMS throttles the request. It fails with ValidationException if the key is disabled, pending deletion, or deleted.

Parameters:

  • space_id (SpaceId) –

    The space to search. Use actorId to narrow the search within it.

  • memory_strategies (Optional[StrategySelectorList], default: None ) –

    The strategies to search, as a list of selectors. Each selector names a memory type and, optionally, specific strategy names of that type. A space can hold several strategies of the same type. A selector without memoryStrategyNames searches every strategy of its type, and omitting memoryStrategies searches every strategy in the space. A name that is not a declared strategy of that type fails with ValidationException.

  • actor_id (ActorId) –

    The actor whose memories to search (exact match). Required: every search covers exactly one actor, and a memory stored without an actor is never returned. This is the only way to filter by actor: a query that references system_metadata.actor_id fails with ValidationException (reason nonFilterableField).

  • query (SearchQuery) –

    The query: a subset of the OpenSearch Query DSL (see SearchQuery). You can't use the actor and session fields system_metadata.actor_id and system_metadata.session_id here. Use the top-level actorId to filter by actor. Searching memories doesn't filter by session.

  • sort (Optional[SearchSort], default: None ) –

    The sort order (see SearchSort). Omit to return the most relevant memories first.

  • max_results (Optional[MaxResults], default: None ) –

    The maximum number of memories to return, from 1 to 100. The default is 20.

  • next_token (Optional[PaginationToken], default: None ) –

    The token for the next set of results. Use the value returned in the previous response in the next request to retrieve the next set of results.

Raises:

  • AccessDeniedException –

    You don't have sufficient access to perform this operation. To use this operation, you must have AWS Identity and Access Management (IAM) permissions for the operation, and on the resource it acts on. If the space uses a customer managed AWS Key Management Service (AWS KMS) key, you must also have permission to use the key.

  • InternalServerException –

    The service couldn't process the request because of an internal error. Retry the request.

  • ResourceNotFoundException –

    The specified resource doesn't exist. Check the IDs in the request.

  • ThrottlingException –

    The service throttled the request. Retry the request with exponential backoff.

  • ValidationException –

    The request isn't valid. The reason, message, and fieldList members describe the problem. Fix the request, and then try again.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Source code in docs_src/capo_opensearchmemory/_services/open_search_memory.py
def search_memories(
    self,
    space_id: "capo_opensearchmemory.types.space_id.SpaceId",
    actor_id: "capo_opensearchmemory.types.actor_id.ActorId",
    query: "capo_opensearchmemory.types.search_query.SearchQuery",
    *,
    config_overrides: Optional[OpenSearchMemoryClientConfig] = None,
    memory_strategies: Optional[
        "capo_opensearchmemory.types.strategy_selector_list.StrategySelectorList"
    ] = None,
    sort: Optional["capo_opensearchmemory.types.search_sort.SearchSort"] = None,
    max_results: Optional[
        "capo_opensearchmemory.types.max_results.MaxResults"
    ] = None,
    next_token: Optional[
        "capo_opensearchmemory.types.pagination_token.PaginationToken"
    ] = None,
) -> "capo_opensearchmemory.types.search_memories_output.SearchMemoriesOutput":
    """<p>Searches a space's memories by relevance. The query is a subset of the OpenSearch Query DSL. The service validates it, limits the search to the requested strategies and to the actor in <code>actorId</code>, and returns one ranked list of memories. You can page through only a limited number of results. A request past that limit fails with <code>ValidationException</code> (reason <code>invalidNextToken</code>), and the message gives the limit.</p> <p>Memories past their expiry (set from the space's <code>memoryExpiryDays</code> when each memory is written) are left out of the results. Only current memories are returned: a memory that consolidation has superseded (an older version of a fact that a newer memory replaced) is left out.</p> <p>If the results are larger than 5 MiB, the request fails with <code>ValidationException</code> (reason <code>queryBodyTooLarge</code>). Lower <code>maxResults</code>.</p> <p>If the request would exceed the service's rate for embedding requests, it fails with <code>ThrottlingException</code>. Retry with exponential backoff.</p> <p>If the space uses a customer managed AWS KMS key, the call can also fail because of the key. It fails with <code>AccessDeniedException</code> if you aren't allowed to use the key, and with <code>ThrottlingException</code> if AWS KMS throttles the request. It fails with <code>ValidationException</code> if the key is disabled, pending deletion, or deleted.</p>

    Args:
        space_id: <p>The space to search. Use <code>actorId</code> to narrow the search within it.</p>
        memory_strategies: <p>The strategies to search, as a list of selectors. Each selector names a memory type and, optionally, specific strategy names of that type. A space can hold several strategies of the same type. A selector without <code>memoryStrategyNames</code> searches every strategy of its type, and omitting <code>memoryStrategies</code> searches every strategy in the space. A name that is not a declared strategy of that type fails with <code>ValidationException</code>.</p>
        actor_id: <p>The actor whose memories to search (exact match). Required: every search covers exactly one actor, and a memory stored without an actor is never returned. This is the only way to filter by actor: a query that references <code>system_metadata.actor_id</code> fails with <code>ValidationException</code> (reason <code>nonFilterableField</code>).</p>
        query: <p>The query: a subset of the OpenSearch Query DSL (see <code>SearchQuery</code>). You can't use the actor and session fields <code>system_metadata.actor_id</code> and <code>system_metadata.session_id</code> here. Use the top-level <code>actorId</code> to filter by actor. Searching memories doesn't filter by session.</p>
        sort: <p>The sort order (see <code>SearchSort</code>). Omit to return the most relevant memories first.</p>
        max_results: <p>The maximum number of memories to return, from 1 to 100. The default is 20.</p>
        next_token: <p>The token for the next set of results. Use the value returned in the previous response in the next request to retrieve the next set of results.</p>

    Raises:
        capo_opensearchmemory.errors.access_denied_exception.AccessDeniedException: <p>You don't have sufficient access to perform this operation. To use this operation, you must have AWS Identity and Access Management (IAM) permissions for the operation, and on the resource it acts on. If the space uses a customer managed AWS Key Management Service (AWS KMS) key, you must also have permission to use the key.</p>
        capo_opensearchmemory.errors.internal_server_exception.InternalServerException: <p>The service couldn't process the request because of an internal error. Retry the request.</p>
        capo_opensearchmemory.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource doesn't exist. Check the IDs in the request.</p>
        capo_opensearchmemory.errors.throttling_exception.ThrottlingException: <p>The service throttled the request. Retry the request with exponential backoff.</p>
        capo_opensearchmemory.errors.validation_exception.ValidationException: <p>The request isn't valid. The <code>reason</code>, <code>message</code>, and <code>fieldList</code> members describe the problem. Fix the request, and then try again.</p>
        capo_opensearchmemory.errors.UnknownServiceError: The service returned an error code this client does not model.
    """

    def _handler(
        req: "OperationRequest[capo_opensearchmemory.types.search_memories_input.SearchMemoriesInput]",
    ) -> OperationResponse[
        "capo_opensearchmemory.types.search_memories_output.SearchMemoriesOutput"
    ]:
        import capo_opensearchmemory._operations.open_search_memory.search_memories

        output, http_response = (
            capo_opensearchmemory._operations.open_search_memory.search_memories.search_memories(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_input(
        input_: "capo_opensearchmemory.types.search_memories_input.SearchMemoriesInput",
    ) -> dict[str, Any]:
        import capo_opensearchmemory.types.search_memories_input

        return capo_opensearchmemory.types.search_memories_input.redact_sensitive(
            input_
        )

    def _redact_output(
        output: "capo_opensearchmemory.types.search_memories_output.SearchMemoriesOutput",
    ) -> dict[str, Any]:
        import capo_opensearchmemory.types.search_memories_output

        return capo_opensearchmemory.types.search_memories_output.redact_sensitive(
            output
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging(
            "SearchMemories",
            redact_input=_redact_input,
            redact_output=_redact_output,
        ),
    )
    input_: capo_opensearchmemory.types.search_memories_input.SearchMemoriesInput = {
        "space_id": space_id,
        "actor_id": actor_id,
        "query": query,
    }
    if memory_strategies is not None:
        input_["memory_strategies"] = memory_strategies
    if sort is not None:
        input_["sort"] = sort
    if max_results is not None:
        input_["max_results"] = max_results
    if next_token is not None:
        input_["next_token"] = next_token

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output