Skip to main content

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:

Syntax

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);

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);
ParameterTypeDescription
docA server-side document, an entityThe document whose attachments you want to load
namestringThe 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

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.

Examples

Indexes with AttachmentsFor()

public class Employees_ByAttachmentNames : AbstractIndexCreationTask<Employee>
{
public class Result
{
public string[] AttachmentNames { get; set; }
}

public Employees_ByAttachmentNames()
{
Map = employees => from e in employees
let attachments = AttachmentsFor(e)
select new Result
{
AttachmentNames = attachments.Select(x => x.Name).ToArray()
};
}
}

Indexes with LoadAttachment()

private class Companies_With_Attachments : AbstractIndexCreationTask<Company>
{

public Companies_With_Attachments()
{
Map = companies => from company in companies
let attachment = LoadAttachment(company, company.ExternalId)
select new
{
CompanyName = company.Name,
AttachmentName = attachment.Name,
AttachmentContentType = attachment.ContentType,
AttachmentHash = attachment.Hash,
AttachmentSize = attachment.Size,
AttachmentContent = attachment.GetContentAsString(Encoding.UTF8),
};
}
}

Indexes with LoadAttachments()

private class Companies_With_All_Attachments : AbstractIndexCreationTask<Company>
{
public Companies_With_All_Attachments()
{
Map = companies => from company in companies
let attachments = LoadAttachments(company)
from attachment in attachments
select new
{
AttachmentName = attachment.Name,
AttachmentContent = attachment.GetContentAsString(Encoding.UTF8)
};
}
}

Querying the Index

//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = session
.Query<Employees_ByAttachmentNames.Result, Employees_ByAttachmentNames>()
.Where(x => x.AttachmentNames.Contains("cv.pdf"))
.OfType<Employee>()
.ToList();