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

# Index Attachments

> Client language: Java. Also available for [C#](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.md), [Python](https://docs.ravendb.net/6.2/document-extensions/attachments/indexing.python.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. write index definition as string.

#### Method

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

#### AttachmentName

```java
private String name;
private String hash;
private String contentType;
private 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.

```java
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()`

```java
public static class Employees_ByAttachmentNames extends AbstractIndexCreationTask {
  public Employees_ByAttachmentNames() {
      map = "from e in docs.Employees\n" +
          "let attachments = AttachmentsFor(e)\n" +
          "select new {\n" +
          "   attachmentNames = attachments.Select(x => x.Name).ToArray()\n" +
          "}";
  }
}
```

#### Indexes with `LoadAttachment()`

```java
private class Companies_With_Attachments_JavaScript extends AbstractJavaScriptIndexCreationTask {
  public Companies_With_Attachments_JavaScript() {
      setMaps(Collections.singleton(
          "map('Companies', function (company) {\n" +
              "   var attachment = LoadAttachment(company, company.ExternalId);\n" +
              "   return {\n" +
              "       CompanyName: company.Name,\n" +
              "       AttachmentName: attachment.Name,\n" +
              "       AttachmentContentType: attachment.ContentType,\n" +
              "       AttachmentHash: attachment.Hash,\n" +
              "       AttachmentSize: attachment.Size,\n" +
              "       AttachmentContent: attachment.getContentAsString('utf8')\n" +
              "   }\n"+
              "});"
          )
      );
  }
}
```

#### Indexes with `LoadAttachments()`

```java
private class Companies_With_All_Attachments_JS extends AbstractJavaScriptIndexCreationTask {
  public Companies_With_All_Attachments_JS() {
      setMaps(Collections.singleton(
          "map('Companies', function (company) {\n" +
          "    var attachments = LoadAttachments(company);\n" +
          "    return attachments.map(attachment => ({\n" +
          "        AttachmentName: attachment.Name,\n" +
          "        AttachmentContent: attachment.getContentAsString('utf8')\n" +
          "     }));\n" +
          "})"
          )
      );
  }
}
```

#### Querying the Index

```java
//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = session.query(Employees_ByAttachmentNames.class)
  .containsAny("attachmentNames", Arrays.asList("employees_cv.pdf"))
  .selectFields(Company.class, "cv.pdf").ofType(Employee.class)
  .toList();
```

## Leveraging indexed attachments

- Access to the indexed attachment content opens the door to many different applications,  
  including many 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.java.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.java.md)
- [What are Indexes](https://docs.ravendb.net/6.2/indexes/what-are-indexes.java.md)
