see on GitHub

Indexes : Indexing Attachments

To address the need of indexing (and searching for) the attachments we have introduced the AttachmentsFor method that can be used in indexing functions. This method will return a list of attachments in given document with basic information like Name or Size about every one of them.

Creating Indexes

The AttachmentsFor method available in AbstractIndexCreationTask returns all of the attachments for a document passed as the first argument.

IEnumerable<AttachmentName> AttachmentsFor(object doc);
public string Name;

public string Hash;

public string ContentType;

public long Size;

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
                           {
                               AttachmentNames = attachments.Select(x => x.Name).ToArray()
                           };
    }
}

Example

// return all employees that have CV attached
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 CV attached
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 CV attached
List<Employee> employees = session
    .Advanced
    .DocumentQuery<Employee, Employees_ByAttachmentNames>()
    .ContainsAny("AttachmentNames", new[] { "cv.pdf" })
    .ToList();