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

# Search Memories

> Semantic search across stored Cognis memories. Returns the most relevant facts ranked by similarity score.

The `search` method performs a hybrid semantic search across stored memories and returns the most relevant results ranked by score. Call it before generating a response to give your agent relevant context from prior conversations.

## Method signature

```python theme={null}
cog.search(
    query: str,
    owner_id: str | None = None,
    agent_id: str | None = None,
    session_id: str | None = None,
    limit: int | None = None,
    cross_session: bool | None = None,
) -> List[CognisSearchResult]
```

At least one of `owner_id`, `agent_id`, or `session_id` must be provided.

## Parameters

| Parameter       | Type   | Required    | Description                                     |
| --------------- | ------ | ----------- | ----------------------------------------------- |
| `query`         | `str`  | Yes         | Natural language search query.                  |
| `owner_id`      | `str`  | Conditional | Filter by owner or user identifier.             |
| `agent_id`      | `str`  | Conditional | Filter by agent identifier.                     |
| `session_id`    | `str`  | Conditional | Filter by session identifier.                   |
| `limit`         | `int`  | No          | Maximum number of results to return.            |
| `cross_session` | `bool` | No          | Search across all sessions for the given owner. |

## Response

Returns a `List[CognisSearchResult]`. Each result has the following fields:

| Field        | Type            | Description                                                  |
| ------------ | --------------- | ------------------------------------------------------------ |
| `id`         | `str`           | Memory record ID.                                            |
| `content`    | `str`           | The memory content.                                          |
| `score`      | `float \| None` | Similarity score. Higher values indicate stronger relevance. |
| `owner_id`   | `str \| None`   | Owner identifier.                                            |
| `agent_id`   | `str \| None`   | Agent identifier.                                            |
| `session_id` | `str \| None`   | Session identifier.                                          |
| `metadata`   | `Dict \| None`  | Additional metadata attached to the memory.                  |
| `created_at` | `str \| None`   | Creation timestamp.                                          |

## Basic usage

<Tabs>
  <Tab title="Hosted (lyzr-adk)">
    ```python theme={null}
    from lyzr import Cognis

    cog = Cognis(api_key="sk-your-api-key")

    results = cog.search(query="What is the user's name?", owner_id="user_alice")
    for result in results:
        print(f"{result.content}  (score: {result.score})")
    ```
  </Tab>

  <Tab title="Open Source (lyzr-cognis)">
    ```python theme={null}
    from cognis import Cognis

    m = Cognis(owner_id="user_alice")

    resp = m.search("What is the user's name?")
    for r in resp["results"]:
        print(f"{r['content']}  (score: {r['score']})")

    m.close()
    ```
  </Tab>
</Tabs>

## Filtered search

Narrow results by combining scope identifiers.

```python theme={null}
# Within a specific session
results = cog.search(
    query="project details",
    owner_id="user_alice",
    session_id="project_session",
)

# Across a specific agent's memories
results = cog.search(
    query="user preferences",
    owner_id="user_alice",
    agent_id="support_bot",
)
```

## Cross-session search

Search across all sessions for an owner by setting `cross_session=True`.

```python theme={null}
results = cog.search(
    query="What programming languages does the user know?",
    owner_id="user_alice",
    cross_session=True,
)
```

## Limiting results

```python theme={null}
results = cog.search(
    query="hobbies",
    owner_id="user_alice",
    limit=5,
)
```

## Async usage

```python theme={null}
import asyncio
from lyzr import Cognis

async def main():
    async with Cognis(api_key="sk-your-api-key") as cog:
        results = await cog.asearch(
            query="What is the user's name?",
            owner_id="user_alice",
            limit=10,
        )
        for result in results:
            print(result.content)

asyncio.run(main())
```

Write queries as natural language questions for best results. For example, `"What is the user's favorite color?"` retrieves more relevant results than a bare keyword like `"favorite color"`.
