OpenSearchMemory / Client / search_memories

search_memories

OpenSearchMemory.Client.search_memories(**kwargs)

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.

See also: AWS API Documentation

Request Syntax

response = client.search_memories(
    spaceId='string',
    memoryStrategies=[
        {
            'memoryType': 'SEMANTIC'|'SUMMARY'|'CUSTOM',
            'memoryStrategyNames': [
                'string',
            ]
        },
    ],
    actorId='string',
    query={...}|[...]|123|123.4|'string'|True|None,
    sort={...}|[...]|123|123.4|'string'|True|None,
    maxResults=123,
    nextToken='string'
)
Parameters:
  • spaceId (string) –

    [REQUIRED]

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

  • memoryStrategies (list) –

    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.

    • (dict) –

      A memory type and, optionally, specific strategy names of that type. A space can hold several strategies of the same type, so the type and its names are given together.

      • memoryType (string) – [REQUIRED]

        The memory type to search or list.

      • memoryStrategyNames (list) –

        Specific strategy names of memoryType. Omit it to include every strategy of that type.

        • (string) –

          The name of a strategy declared on a space, for example user-facts. Names are case-sensitive and must match the declared name exactly.

  • actorId (string) –

    [REQUIRED]

    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 (document) –

    [REQUIRED]

    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 (document) – The sort order (see SearchSort). Omit to return the most relevant memories first.

  • maxResults (integer) – The maximum number of memories to return, from 1 to 100. The default is 20.

  • nextToken (string) – 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.

Return type:

dict

Returns:

Response Syntax

{
    'memories': [
        {
            'memoryId': 'string',
            'memoryType': 'SEMANTIC'|'SUMMARY'|'CUSTOM',
            'strategyName': 'string',
            'content': 'string',
            'score': 123.0,
            'systemMetadata': {
                'actorId': 'string',
                'sessionId': 'string',
                'createdAt': datetime(2015, 1, 1),
                'updatedAt': datetime(2015, 1, 1)
            },
            'metadata': {
                'string': 'string'
            },
            'sourceEventIds': [
                'string',
            ]
        },
    ],
    'nextToken': 'string'
}

Response Structure

  • (dict) –

    • memories (list) –

      The matching memories, in the order sort requests, or most relevant first when it has none. Empty when nothing matches.

      • (dict) –

        A SearchMemories result: a matching memory and its relevance score.

        • memoryId (string) –

          The memory’s ID, assigned by the service. Always present.

        • memoryType (string) –

          The memory type of the result.

        • strategyName (string) –

          The name of the strategy the memory belongs to, for example user-facts: the strategy memoryStrategies selects on in SearchMemories. Absent when the memory’s strategy is no longer declared on the space, for example right after the strategy is deleted.

        • content (string) –

          The memory text.

        • score (float) –

          The relevance score of the result. A higher score is more relevant. Scores aren’t normalized to a fixed range and can be greater than 1, so use them only to order results.

        • systemMetadata (dict) –

          Provenance for the memory: the actor and session that own it, and when it was created and last updated. It is the same block that ListMemories, GetMemory, and SearchEvents return.

          In a query, filter or sort on the timestamps as system_metadata.created_at and system_metadata.updated_at. Scope by owner with the top-level actorId, not in the query. Searching memories doesn’t filter by session.

          Absent when the service has no provenance for the memory.

          • actorId (string) –

            The owning actor. Absent when none was recorded.

          • sessionId (string) –

            The owning session. Absent when none was recorded.

          • createdAt (datetime) –

            When the record was created. Always present on ListMemories and GetMemory. It can be absent from search results.

          • updatedAt (datetime) –

            When the record was last updated. Equals createdAt for a record that has never been updated, and always for a SearchEvents result.

        • metadata (dict) –

          The memory’s metadata tags. All values are strings. In a query, filter on a tag as metadata.<key>.

          • (string) –

            A metadata key: letters, digits, hyphens ( -), and underscores ( _). Dots are not allowed, because a query uses dots to separate nested field names.

            • (string) –

              The value of a metadata entry.

        • sourceEventIds (list) –

          The IDs of the events the memory was derived from, without duplicates. Use them with GetEvent or BatchGetEvent, or to search those events’ content with SearchEvents by filtering on event_ids in the query. Absent for a memory created directly with CreateMemory. A memory can be derived from several events. The service sets this value.

          • (string) –

            An event ID, assigned by the service.

    • nextToken (string) –

      The token for the next set of results. Absent when there are no more results.

Exceptions