Indexes: Indexing Attachments

  • Attachments can be referenced and loaded using the AttachmentsFor and LoadAttachment/LoadAttachments methods.

  • Auto-indexes for attachments are not available at this time.

  • In this page:


Using AttachmentsFor()

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

IEnumerable<AttachmentName> AttachmentsFor(object doc);
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.

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.

public Stream GetContentAsStream();

public string GetContentAsString(Encoding encoding);

public string GetContentAsString(); // Default: UTF-8

Applications for Attachment Content: Machine Learning

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

In this blog post, Oren Eini demonstrates how machine learning image recognition can be added to an index using the additional sources feature. The resulting index allows filtering and querying based on image content.


Indexes with AttachmentsFor()

class Employees_ByAttachmentNames extends AbstractCsharpIndexCreationTask {
    constructor() {
        super(); = `docs.Employees.Select(e => new {
            e = e,
            attachments = this.AttachmentsFor(e)
        }).Select(this0 => new {
            AttachmentNames = Enumerable.ToArray(this0.attachments.Select(x => x.Name))

Indexes with LoadAttachment()

class Companies_With_Attachments_JavaScript extends AbstractCsharpIndexCreationTask {
    constructor() {
        super(); = `docs.Companies.Select(company => new {
            company = company,
            attachment = this.LoadAttachment(company, (System.String) company.ExternalId)
        }).Select(this0 => new {
            CompanyName =,
            AttachmentName = this0.attachment.Name,
            AttachmentContentType = this0.attachment.ContentType,
            AttachmentHash = this0.attachment.Hash,
            AttachmentSize = this0.attachment.Size,
            AttachmentContent = this0.attachment.GetContentAsString(Encoding.UTF8)

Indexes with LoadAttachments()

class Companies_With_All_Attachments_JS extends AbstractCsharpIndexCreationTask {
    constructor() {
        super(); = `docs.Companies.Select(company => new {
            company = company,
            attachments = this.LoadAttachments(company)
        }).SelectMany(this0 => this0.attachments, (this0, attachment) => new {
            AttachmentName = attachment.Name,
            AttachmentContent = attachment.GetContentAsString(Encoding.UTF8)

Querying the Index

//return all employees that have an attachment called "cv.pdf"
const employees = await session.query(Employees_ByAttachmentNames)
    .containsAny("attachmentNames", ["employees_cv.pdf"])