Indexing Unmanaged Files for Solr Search

Indexing Unmanaged Files for Solr Search

Ki Kim's picture

As an add-on Drupal module for Apache Solr Search, Apache Solr Attachments enables indexing and searching of file attachments. Tika java application is a recommended choice to parse the text contents out of various file formats. Tika not only can parse plain text files or Microsoft Office documents, but it can also read meta data contained in image, audio and video formats.

File indexing, however, only works for files attached to contents, which are managed by Drupal. However, consider the situation in which content files are uploaded but not managed; that is, Drupal neither knows about nor tracks the files, even though they are linked in the contents. The files could have been uploaded through a wysiwyg editor without Drupal's knowledge, or the contents could have been migrated from another system.

Would there be a way to index unmanaged files? My colleague Gergely Lekli shared a method of splitting a potentially long body of contents into multiple Solr documents for each paragraph in his blog, Paragraph Level Search Results with Solr.

The method being presented here is essentially the same approach to creating multiple Solr documents from a single content (aka node in Drupal lingo), only applied to files. Apache Solr module provides an API to allow just that. That is what actually happens with Apache Solr Attachments module for managed files. If a Drupal node has three attached files parsable by Tika, total of 4 Solr documents would be indexed; one node plus three files. Solr documents derived from files will show up as a search result as if they are an independent entity.

Create Solr documents for each file

As the first step, we are implementing hook_apachesolr_index_documents_alter() in a custom module. In this example, the custom module is called unmanagedfilesearch and it depends on Apache Solr Search module.


function unmanagedfilesearch_apachesolr_index_documents_alter(&$documents, $entity, $entity_type, $env_id) {
// Deal with node entity for now, but it could be extended to other entities in general.
if ($entity_type == 'node') {
$solr = apachesolr_get_solr($env_id);
$site_hash = apachesolr_site_hash();
// To prepare for (re)indexing, remove previously indexed unmanaged files, if any,
    // from Solr Index that have entity_id greater than a billion.
    // Chose high number to distinguish from managed files.
    // Explained later in unmanagedfilesearch_parse_files().
$solr->deleteByQuery("id:$site_hash/file/*-$entity->nid AND entity_id:[1000000000 TO *]");
// Parse linked files in the content and create separate solr documents for each.
    // Pattern borrowed from Apache Solr Search module.
foreach (unmanagedfilesearch_parse_files($entity) as $file) {
$document = _apachesolr_index_process_entity_get_document($file, 'file');
$documents = array_merge($documents, unmanagedfilesearch_solr_document($document, $file, 'file', $env_id));


Before building Solr documents for (re)indexing, we remove existing indexed flles, but only unmanaged files. Unmanaged files do not have file ID in Drupal database, but still needs to be identified by number entity_id in Solr Index. In order to ensure unique entity_id as a file, fake ID is generated using the node ID and the file's position with a high numbered offset, a billion in this example. That is, the second unmanaged file of a node with ID 1234 would be 1234 * billion + 2 = 1234000000002.

This fake file ID does not live in Drupal database, but in Solr Index only to assign a unique number to each file. This scheme will work as long as the site's actual file ID will not reach one billion.

The function calls two other custom functions; unmanagedfilesearch_parse_files() and unmanagedfilesearch_solr_document().

Identify linked files in the content

unmanagedfilesearch_parse_files() function parses the body of the node to search for local files and return array of files if any. If looks for href attribute, collects all candidate values from them. Then it checks each URI to confirm they are local files that actually exist in the file system. Finally it builds an array of file objects.


function unmanagedfilesearch_parse_files($node) {
$body = $node->body[LANGUAGE_NONE][0]['value'];
// Parse href attributes in <A> links.
preg_match_all('/href=[\'"]([^\>\'"]*)[\'"]/', $body, $matches, PREG_SET_ORDER);
$files = array();
  foreach (
$matches as $match) {
// Determine if the file is local. Absolute URL could be local.
    // Beginning double slashes is implicit for the current page's protocol, but just apply http.
if (substr($match[1], 0, 2) == '//') {
$url = 'http:' . $match[1];
    elseif (
substr($match[1], 0, 1) == '/') {
$url = $GLOBALS['base_root'] . $match[1];
    else {
$url = $match[1];
$parse = parse_url($url);
// Get absolute URL to the file location.
$path_files = file_create_url('public://');
    if (isset(
$parse['host']) and $parse['host'] == $_SERVER['HTTP_HOST']) {
$uri = 'public://' . str_replace($path_files, '', $url);
// Convert back things (such as %20 back to a space).
$uri = urldecode($uri);
      if (
file_exists($uri)) {
// TODO: Check that the file is not managed by Drupal already.
if (apachesolr_attachments_allowed_mime($mimetype = file_get_mimetype($uri))) {
$file = new stdClass();
// Since the file is not managed, it has no file id. Just give large number (to avoid potential mix-up with managed files).
          // The lowest possible number to get is 1 billion plus 1. This assumes actual managed file id will not reach 1 billion, which should be okay for most sites.
$file->fid       = $node->nid * 1000000000 + count($files) + 1;
$file->uri       = $uri;
$file->filename  = drupal_basename($uri);
$file->filemime  = $mimetype;
$file->uid       = 1;
$file->status    = 1;
$file->filesize  = filesize($uri);
$file->timestamp = REQUEST_TIME;
// This variable is custom one that's not part of file object, but needed in unmanagedfilesearch_solr_document().
$file->parent_entity_id = $node->nid;
$files[] = $file;


Building file info for Solr document

unmanagedfilesearch_solr_document() is to build the file specific information for a Solr document.

The function is basically same as apachesolr_attachments_solr_document() in Apache Solr Attachments module. We could have used apachesolr_attachments_solr_document() only if it did not check for the file's status in the Drupal database, which is not applicable because the file does not exist as far as Drupal is concerned.

Our modified function removes the code that searches parents of the file in the database, and instead establishes current node as the file's sole parent. That's why we added a custom variable, parent_entity_id, to $file object in unmanagedfilesearch_parse_files() above.


function unmanagedfilesearch_solr_document(ApacheSolrDocument $document, $file, $entity_type, $env_id) {
module_load_include('inc', 'apachesolr_attachments', 'apachesolr_attachments.index');
$documents = array();
$text = apachesolr_attachments_get_attachment_text($file);
  if (empty(
$text)) {
// Custom addition
$parent = (object)array(
'parent_entity_type' => 'node',
'parent_entity_id'   => $file->parent_entity_id,
// load the parent entity and reset cache
$parent_entities = entity_load($parent->parent_entity_type, array($parent->parent_entity_id), NULL, TRUE);
$parent_entity = reset($parent_entities);
// Skip invalid entities
if (empty($parent_entity)) {
// Retrieve the parent entity id and bundle
list($parent_entity_id, $parent_entity_vid, $parent_entity_bundle) = entity_extract_ids($parent->parent_entity_type, $parent_entity);
$parent_entity_type = $parent->parent_entity_type;
// proceed with building this document only if the parent entity is not flagged for
  // indexing attachments with parent entity or not indexing attachements
if (variable_get('apachesolr_attachments_entity_bundle_indexing_' . $parent_entity_bundle, 'seperate') == 'seperate') {
// Get a clone of the bare minimum document
$filedocument = clone $document;
//Get the callback array to add stuff to the document
$callbacks = apachesolr_entity_get_callback($parent_entity_type, 'document callback');
$build_documents = array();
    if (
is_array($callbacks)) {
      foreach (
$callbacks as $callback) {
// Call a type-specific callback to add stuff to the document.
if (is_callable($callback)) {
$build_documents = array_merge($build_documents, $callback($filedocument, $parent_entity, $parent_entity_type, $env_id));
// Take the top document from the stack
$filedocument = reset($build_documents);
// Build our separate document and overwrite basic information
$filedocument->id = apachesolr_document_id($file->fid . '-' . $parent_entity_id, $entity_type);
$filedocument->url = file_create_url($file->uri);
$path = file_stream_wrapper_get_instance_by_uri($file->uri)->getExternalUrl();
// A path is not a requirement of an entity
if (!empty($path)) {
$filedocument->path = $path;
// Add extra info to our document
$filedocument->label = apachesolr_clean_text($file->filename);
$filedocument->content = apachesolr_clean_text($file->filename) . ' ' . $text;
$filedocument->ds_created = apachesolr_date_iso($file->timestamp);
$filedocument->ds_changed = $filedocument->ds_created;
$filedocument->created = apachesolr_date_iso($file->timestamp);
$filedocument->changed = $filedocument->created;
// Add Parent information fields. See <a href=""></a> for explanation
$parent_entity_info = entity_get_info($parent_entity_type);
$small_parent_entity = new stdClass();
$small_parent_entity->entity_type = $parent_entity_type;
$small_parent_entity->{$parent_entity_info['entity keys']['id']} = $parent_entity_id;
$small_parent_entity->{$parent_entity_info['entity keys']['bundle']} = $parent_entity_bundle;
// Not all entities has entity key label set, so it should be checked first to avoid errors.
if (isset($parent_entity_info['entity keys']['label'])) {
$small_parent_entity->{$parent_entity_info['entity keys']['label']} = $parent_entity->{$parent_entity_info['entity keys']['label']};
// Add all to one field because if it is spread out over
    // multiple fields there is no way of knowing which multifield value
    // belongs to which entity
    // It does not load the complete entity in to the index because that
    // would dramatically increase the index size and processing time
$filedocument->zm_parent_entity = drupal_json_encode($small_parent_entity);
$filedocument->sm_parent_entity_bundle = $parent_entity_type . "-" . $parent_entity_bundle;
$filedocument->sm_parent_entity_type = $parent_entity_type;
// Add Apachesolr Attachments specific fields.
$filedocument->ss_filemime = $file->filemime;
$filedocument->ss_filesize = $file->filesize;
$documents[] = $filedocument;


If you compare with original apachesolr_attachments_solr_document(), you will also notice that the foreach loop that iterates over possible multiple parents is removed.

This way, Solr Search can index unmanaged files that Drupal does not know about and present search results, in which managed and unmanaged files are virtually indistinguishable and treated equally.


Works like a charm, you should consider contributing or adding this to Solr Attachments module. Thank you!

Hi Ki, thank you for the module. This module is dependent on the solr attachments module, right?

Also, when i enable this module and start indexing content, I get the following error

"An AJAX HTTP error occurred. HTTP Result Code: 500 Debugging information follows. Path: /batch?id=1285&op=do StatusText: Service unavailable (with message) ResponseText: "

Is there anything that you could think of, that might be causing this issue.

Once I disable the unmanagedfilesearch module, it indexes fine.

Thank you.

Hi Ki, I was able to overcome the error that I mentioned in my comment above, but I get a new error when I index.

EntityMalformedException: Missing bundle property on entity of type file. in entity_extract_ids() (line 7721 of /var/www/reit/includes/

Was wondering if you could offer any insight into that.

Thank you.

Hi Sarat,

You just have to add the line :
"$file->type = 'document';" (for example)
after the line :
"$file->status = 1;"
in the function "unmanagedfilesearch_parse_files"

Thank you to Ki for this article, it help me a lot.


Post new comment

About Urban Insight

We create elegant, mobile-friendly websites.

We solve complex problems using Drupal and open source software.

Learn More


This module adds a new organizational layer to drupal, making it easy for managing large numbers of files and nodes. It allows the creation of onthefly directory structures and upload of mutliple files at once. Jpeg files are displayed as slideshows, other files can be downloaded from a block.