Language
C#
Article For
4.1 4.2 5.0 5.1 5.2

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

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();