Indexes: Indexing Attachments


  • Attachments can be referenced and loaded using the AttachmentsFor and LoadAttachment/LoadAttachments methods.

  • Auto-indexes for attachments are not available at this time.

  • In this page:


Syntax

Using AttachmentsFor()

The AttachmentsFor method returns information about each attachment that extends a specified document, including their names, sizes, and content type. write index definition as string.

IEnumerable<AttachmentName> AttachmentsFor(object doc);
private String name;
private String hash;
private String contentType;
private long size;

The AttachmentsFor method is available in AbstractIndexCreationTask.

Using LoadAttachment()/LoadAttachments()

LoadAttachment() loads an attachment to the index by document and attachment name.
LoadAttachments() loads all the attachments of a given document.

public IAttachmentObject LoadAttachment(object doc, string name);
public IEnumerable<IAttachmentObject> LoadAttachments(object doc);
Parameter Type Description
doc A server-side document, an entity The document whose attachments you want to load
name String The name of the attachment you want to load

GetContentAs Methods

To access the attachment content itself, use GetContentAsStream(). To convert the content into a string, use GetContentAsString() with the desired character encoding.

public Stream GetContentAsStream();

public string GetContentAsString(Encoding encoding);

public string GetContentAsString(); // Default: UTF-8

Applications for Attachment Content: Machine Learning

Access to the attachment content opens the door to many different applications, including many that can be integrated directly into RavenDB.

In this blog post, Oren Eini demonstrates how machine learning image recognition can be added to an index using the additional sources feature. The resulting index allows filtering and querying based on image content.

Examples

Indexes with 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" +
            "   attachmentNames = attachments.Select(x => x.Name).ToArray()\n" +
            "}";
    }
}

Indexes with LoadAttachment()

private class Companies_With_Attachments_JavaScript extends AbstractJavaScriptIndexCreationTask {
    public Companies_With_Attachments_JavaScript() {
        setMaps(Collections.singleton(
            "map('Companies', function (company) {\n" +
                "   var attachment = LoadAttachment(company, company.ExternalId);\n" +
                "   return {\n" +
                "       CompanyName: company.Name,\n" +
                "       AttachmentName: attachment.Name,\n" +
                "       AttachmentContentType: attachment.ContentType,\n" +
                "       AttachmentHash: attachment.Hash,\n" +
                "       AttachmentSize: attachment.Size,\n" +
                "       AttachmentContent: attachment.getContentAsString('utf8')\n" +
                "   }\n"+
                "});"
            )
        );
    }
}

Indexes with LoadAttachments()

private class Companies_With_All_Attachments_JS extends AbstractJavaScriptIndexCreationTask {
    public Companies_With_All_Attachments_JS() {
        setMaps(Collections.singleton(
            "map('Companies', function (company) {\n" +
            "    var attachments = LoadAttachments(company);\n" +
            "    return attachments.map(attachment => ({\n" +
            "        AttachmentName: attachment.Name,\n" +
            "        AttachmentContent: attachment.getContentAsString('utf8')\n" +
            "     }));\n" +
            "})"
            )
        );
    }
}

Querying the Index

//return all employees that have an attachment called "cv.pdf"
List<Employee> employees = session.query(Employees_ByAttachmentNames.class)
    .containsAny("attachmentNames", Arrays.asList("employees_cv.pdf"))
    .selectFields(Company.class, "cv.pdf").ofType(Employee.class)
    .toList();