/** * Add an array of Solr Documents to the index all at once * * @param array $documents Should be an array of ApacheSolrDocument instances * @param boolean $allowDups * @param boolean $overwritePending * @param boolean $overwriteCommitted * * @return response objecte * * @throws Exception If an error occurs during the service call */ public function addDocuments($documents, $overwrite = NULL, $commitWithin = NULL) { $attr = ''; if (isset($overwrite)) { $attr .= ' overwrite="' . empty($overwrite) ? 'false"' : 'true"'; } if (isset($commitWithin)) { $attr .= ' commitWithin="' . intval($commitWithin) . '"'; } $rawPost = "<add{$attr}>"; foreach ($documents as $document) { if (is_object($document) && $document instanceof ApacheSolrDocument) { $rawPost .= ApacheSolrDocument::documentToXml($document); } } $rawPost .= '</add>'; return $this->update($rawPost); }
/** * Create an XML fragment from a ApacheSolrDocument instance appropriate for use inside a Solr add call * * @param ApacheSolrDocument $document * * @return string * an xml formatted string from the given document */ public static function documentToXml(ApacheSolrDocument $document) { $xml = '<doc'; if ($document->getBoost() !== FALSE) { $xml .= ' boost="' . $document->getBoost() . '"'; } $xml .= '>'; foreach ($document as $key => $value) { $key = htmlspecialchars($key, ENT_QUOTES, 'UTF-8'); $fieldBoost = $document->getFieldBoost($key); if (is_array($value)) { foreach ($value as $multivalue) { $xml .= '<field name="' . $key . '"'; if ($fieldBoost !== FALSE) { $xml .= ' boost="' . $fieldBoost . '"'; // Only set the boost for the first field in the set $fieldBoost = FALSE; } $xml .= '>' . htmlspecialchars($multivalue, ENT_NOQUOTES, 'UTF-8') . '</field>'; } } else { $xml .= '<field name="' . $key . '"'; if ($fieldBoost !== FALSE) { $xml .= ' boost="' . $fieldBoost . '"'; } $xml .= '>' . htmlspecialchars($value, ENT_NOQUOTES, 'UTF-8') . '</field>'; } } $xml .= '</doc>'; // Remove any control characters to avoid Solr XML parser exception return self::stripCtrlChars($xml); }
/** * Build the documents before sending them to Solr. * * Supports all types of * hook_apachesolr_index_document_build_' . $entity_type($documents[$id], $entity, $env_id); * * The function is the follow-up for apachesolr_update_index but then for * specific entity types * * @param ApacheSolrDocument $document * @param object $entity * @param string $env_id * The machine name of the environment. */ function hook_apachesolr_index_document_build_ENTITY_TYPE(ApacheSolrDocument $document, $entity, $env_id) { // Index field_main_image as a separate field if ($entity->type == 'profile') { $user = user_load(array('uid' => $entity->uid)); // Hard coded field, not recommended for inexperienced users. $document->setMultiValue('sm_field_main_image', $user->picture); } }
/** * Implements hook_solrcrawler_document_alter(). * * Alter a document before indexing. * * @param ApacheSolrDocument $document */ function hook_solrcrawler_document_alter(ApacheSolrDocument &$document) { $document->addField('foo', 'bar'); }