You are currently browsing legacy 4.1 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
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 returns all of the attachments for a document passed as the first argument.

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

public static class Employees_ByAttachmentNames extends AbstractIndexCreationTask {
    public static class Result {
        private String[] attachmentNames;

        public String[] getAttachmentNames() {
            return attachmentNames;
        }

        public void setAttachmentNames(String[] attachmentNames) {
            this.attachmentNames = attachmentNames;
        }
    }

    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" +
            "}";
    }
}

Example

//return all employees that have CV attached
session.query(Employees_ByAttachmentNames.Result.class, Employees_ByAttachmentNames.class)
    .containsAny("attachmentNames", Lists.newArrayList("cv.pdf"))
    .toList();