Index Attachments
-
Indexing attachments allows you to query for documents based on their attachments' details and content.
-
Static indexes:
Both attachments' details and content can be indexed within a static-index definition. -
Auto-indexes:
Auto-indexing attachments via dynamic queries is 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.
IEnumerable<AttachmentName> AttachmentsFor(object doc);
public string Name;
public string Hash;
public string ContentType;
public 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
Examples
Indexes with AttachmentsFor
class Employees_ByAttachmentNames(AbstractIndexCreationTask):
class Result:
def __init__(self, attachment_names: List[str] = None):
self.attachment_names = attachment_names
def __init__(self):
super().__init__()
self.map = (
"from e in employees "
"let attachments = AttachmentsFor(e) "
"select new "
"{"
" attachment_names = attachments.Select(x => x.Name).ToArray()"
"}"
)
class Employees_ByAttachmentNames_JS(AbstractJavaScriptIndexCreationTask):
class Result:
def __init__(self, attachment_names: List[str] = None):
self.attachment_names = attachment_names
def __init__(self):
super().__init__()
self.maps = {
"""
map('Employees', function (e) {
var attachments = attachmentsFor(e);
return {
attachment_names: attachments.map(
function(attachment) {
return attachment.Name;
}
};
})
"""
}
Indexes with LoadAttachment
class Companies_With_Attachments(AbstractJavaScriptIndexCreationTask):
class Result:
def __init__(self, attachment_names: List[str] = None):
self.attachment_names = attachment_names
def __init__(self):
super().__init__()
self.maps = {
"""
map('Employees', function (e) {
var attachments = attachmentsFor(e);
return {
attachment_names: attachments.map(
function(attachment) {
return attachment.Name;
}
};
})
"""
}
class Companies_With_Attachments_JavaScript(AbstractJavaScriptIndexCreationTask):
def __init__(self):
super().__init__()
self.maps = {
"""
map('Companies', function (company) {
var attachment = loadAttachment(company, company.ExternalId);
return {
company_name: company.Name,
attachment_name: attachment.Name,
attachment_content_type: attachment.ContentType,
attachment_hash: attachment.Hash,
attachment_size: attachment.Size,
attachment_content: attachment.getContentAsString('utf8')
};
})
"""
}
Indexes with LoadAttachments
class Companies_With_All_Attachments(AbstractIndexCreationTask):
def __init__(self):
super().__init__()
self.map = (
"from company in companies "
"let attachments = LoadAttachments(company)"
"from attachment in attachments"
"select new"
"{"
" attachment_name = attachment.Name,"
" attachment_content = attachment.GetContentAsString(Encoding.UTF8)"
"}"
)
class Companies_With_All_Attachments_JS(AbstractJavaScriptIndexCreationTask):
def __init__(self):
super().__init__()
self.maps = {
"""
map('Companies', function (company) {
var attachments = loadAttachments(company);
return attachments.map(attachment => ({
attachment_name: attachment.Name,
attachment_content: attachment.getContentAsString('utf8')
}));
})
"""
}
Querying the Index
# return all employees that have an attachment called "cv.pdf"
employees = list(
session.query_index_type(
Employees_ByAttachmentNames, Employees_ByAttachmentNames.Result
).contains_any("attachment_names", ["cv.pdf"])
)
Leveraging indexed attachments
-
Access to the indexed attachment content opens a door to many different applications,
including ones that can be integrated directly into RavenDB. -
In this blog post, Oren Eini demonstrates how image recognition can be applied to indexed attachments using the additional sources feature.
The resulting index allows filtering and querying based on image content.