Skip to main content

Indexes: Indexing Attachments

The AttachmentsFor method returns a list of attachments in a given document as well as basic information like Name or Size about each of them.

IEnumerable<AttachmentName> AttachmentsFor(object doc);

Creating an index using AttachmentsFor()

The AttachmentsFor method is available in AbstractIndexCreationTask.

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

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