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.

List<AttachmentName> AttachmentsFor(Object doc);
private String name;
private String hash;
private String contentType;
private long size;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getHash() {
    return hash;
}

public void setHash(String hash) {
    this.hash = hash;
}

public String getContentType() {
    return contentType;
}

public void setContentType(String contentType) {
    this.contentType = contentType;
}

public long getSize() {
    return size;
}

public void setSize(long size) {
    this.size = size;
}

Creating an index using AttachmentsFor()

public static class Employees_ByAttachmentNames extends AbstractIndexCreationTask {
    public Employees_ByAttachmentNames() {
        map = "from e in docs.Employees\n" +
            "let attachments = AttachmentsFor(e)\n" +
            "select new\n" +
            "{\n" +
            "   attachmentNames = attachments.Select(x => x.Name).ToArray()\n" +
            "}";
    }
}

Querying the index

//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = session.query(Employee.class, Employees_ByAttachmentNames.class)
    .containsAny("attachmentNames", Lists.newArrayList("cv.pdf"))
    .toList();