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);
public string Name;
public string Hash;
public string ContentType;
public long Size;
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();
//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = await asyncSession
.Query<Employees_ByAttachmentNames.Result, Employees_ByAttachmentNames>()
.Where(x => x.AttachmentNames.Contains("cv.pdf"))
.OfType<Employee>()
.ToListAsync();
//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = session
.Advanced
.DocumentQuery<Employee, Employees_ByAttachmentNames>()
.ContainsAny("AttachmentNames", new[] { "cv.pdf" })
.ToList();