---
title: "Index Attachments"
url: "https://docs.ravendb.net/6.2/document-extensions/attachments/indexing"
version: "6.2"
language: "python"
---

# Index Attachments

> Client language: Python. Also available for [C#](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.md), [Java](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.java.md), [PHP](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.php.md), [Node.js](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.nodejs.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - Indexing attachments allows you to query for documents based on their attachments' details and content.
>
> - **Static indexes**:  
>   Both attachments' details and content can be indexed within a static-index definition.
>
> - **Auto-indexes**:  
>   Auto-indexing attachments via dynamic queries is not available at this time.
>
> - In this page:
>   - [Syntax](#syntax)
>   - [Examples](#examples)
>   - [Leveraging indexed attachments](#leveraging-indexed-attachments)

## Syntax

### Using `AttachmentsFor`

The `AttachmentsFor` method returns information about each attachment that extends
a specified document, including their names, sizes, and content type.

#### Method

```python
IEnumerable<AttachmentName> AttachmentsFor(object doc);
```

#### Result

```python
public string Name;
public string Hash;
public string ContentType;
public long Size;
```

The `AttachmentsFor` method is available in `AbstractIndexCreationTask`.

### Using `LoadAttachment`/`LoadAttachments`

`LoadAttachment` loads an attachment to the index by document and attachment name.  
`LoadAttachments` loads all the attachments of a given document.

```python
public IAttachmentObject LoadAttachment(object doc, string name);
public IEnumerable<IAttachmentObject> LoadAttachments(object doc);
```

| Parameter | Type | Description |
| - | - | - |
| **doc** | A server-side document, an entity | The document whose attachments you want to load |
| **name** | `string` | The name of the attachment you want to load |

#### `GetContentAs` Methods

To access the attachment content itself, use `GetContentAsStream`. To
convert the content into a `string`, use `GetContentAsString` with
the desired character encoding.

```csharp
public Stream GetContentAsStream();
public string GetContentAsString(Encoding encoding);
public string GetContentAsString(); // Default: UTF-8
```

## Examples

#### Indexes with `AttachmentsFor`

##### LINQ-syntax

```python
class Employees_ByAttachmentNames(AbstractIndexCreationTask):
  class Result:
      def __init__(self, attachment_names: List[str] = None):
          self.attachment_names = attachment_names

  def __init__(self):
      super().__init__()
      self.map = (
          "from e in employees "
          "let attachments = AttachmentsFor(e) "
          "select new "
          "{"
          "    attachment_names = attachments.Select(x => x.Name).ToArray()"
          "}"
      )
```

##### JavaScript-syntax

```python
class Employees_ByAttachmentNames_JS(AbstractJavaScriptIndexCreationTask):
  class Result:
      def __init__(self, attachment_names: List[str] = None):
          self.attachment_names = attachment_names

  def __init__(self):
      super().__init__()
      self.maps = {
          """
          map('Employees', function (e) {
              var attachments = attachmentsFor(e);
              return {
                  attachment_names: attachments.map(
                      function(attachment) {
                          return attachment.Name;
                      }
              };
          })
          """
      }
```

#### Indexes with `LoadAttachment`

##### LINQ-syntax

```python
class Companies_With_Attachments(AbstractJavaScriptIndexCreationTask):
  class Result:
      def __init__(self, attachment_names: List[str] = None):
          self.attachment_names = attachment_names

  def __init__(self):
      super().__init__()
      self.maps = {
          """
          map('Employees', function (e) {
              var attachments = attachmentsFor(e);
              return {
                  attachment_names: attachments.map(
                      function(attachment) {
                          return attachment.Name;
                      }
              };
          })
          """
      }
```

##### JavaScript-syntax

```python
class Companies_With_Attachments_JavaScript(AbstractJavaScriptIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.maps = {
          """
          map('Companies', function (company) {
              var attachment = loadAttachment(company, company.ExternalId);
              return {
                  company_name: company.Name,
                  attachment_name: attachment.Name,
                  attachment_content_type: attachment.ContentType,
                  attachment_hash: attachment.Hash,
                  attachment_size: attachment.Size,
                  attachment_content: attachment.getContentAsString('utf8')
              };
          })
          """
      }
```

#### Indexes with `LoadAttachments`

##### LINQ-syntax

```python
class Companies_With_All_Attachments(AbstractIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.map = (
          "from company in companies "
          "let attachments = LoadAttachments(company)"
          "from attachment in attachments"
          "select new"
          "{"
          "    attachment_name = attachment.Name,"
          "    attachment_content = attachment.GetContentAsString(Encoding.UTF8)"
          "}"
      )
```

##### JavaScript-syntax

```python
class Companies_With_All_Attachments_JS(AbstractJavaScriptIndexCreationTask):
  def __init__(self):
      super().__init__()
      self.maps = {
          """
          map('Companies', function (company) {
              var attachments = loadAttachments(company);
              return attachments.map(attachment => ({
                  attachment_name: attachment.Name,
                  attachment_content: attachment.getContentAsString('utf8')
              }));
          })
          """
      }
```

#### Querying the Index

```python
# return all employees that have an attachment called "cv.pdf"
employees = list(
  session.query_index_type(
      Employees_ByAttachmentNames, Employees_ByAttachmentNames.Result
  ).contains_any("attachment_names", ["cv.pdf"])
)
```

## Leveraging indexed attachments

- Access to the indexed attachment content opens a door to many different applications,  
  including ones that can be integrated directly into RavenDB.

- In this [blog post](https://ayende.com/blog/192001-B/using-machine-learning-with-ravendb),
  Oren Eini demonstrates how image recognition can be applied to indexed attachments using the
  [additional sources](https://docs.ravendb.net/6.2/indexes/extending-indexes.md) feature.  
  The resulting index allows filtering and querying based on image content.

## See also

- [What are Attachments](https://docs.ravendb.net/6.2/document-extensions/attachments/what-are-attachments.md)
- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.python.md)
