You are here

public function SearchApiSolrService::indexItems in Search API Solr 7

Indexes the specified items.

Parameters

SearchApiIndex $index: The search index for which items should be indexed.

array $items: An array of items to be indexed, keyed by their id. The values are associative arrays of the fields to be stored, where each field is an array with the following keys:

  • type: One of the data types recognized by the Search API, or the special type "tokens" for fulltext fields.
  • original_type: The original type of the property, as defined by the datasource controller for the index's item type.
  • value: The value to index.

The special field "search_api_language" contains the item's language and should always be indexed.

The value of fields with the "tokens" type is an array of tokens. Each token is an array containing the following keys:

  • value: The word that the token represents.
  • score: A score for the importance of that word.

Return value

array An array of the ids of all items that were successfully indexed.

Throws

SearchApiException If indexing was prevented by a fundamental configuration error.

Overrides SearchApiServiceInterface::indexItems

File

includes/service.inc, line 600

Class

SearchApiSolrService
Search service class using Solr server.

Code

public function indexItems(SearchApiIndex $index, array $items) {
  $documents = array();
  $ret = array();
  $index_id = $this
    ->getIndexId($index->machine_name);
  $fields = $this
    ->getFieldNames($index);
  $languages = language_list();
  $base_urls = array();
  foreach ($items as $id => $item) {
    $doc = new SearchApiSolrDocument();
    $doc
      ->setField('id', $this
      ->createId($index_id, $id));
    $doc
      ->setField('index_id', $index_id);
    $doc
      ->setField('item_id', $id);

    // If multi-site compatibility is enabled, add the site hash and
    // language-specific base URL.
    if (!empty($this->options['site_hash'])) {
      $doc
        ->setField('hash', search_api_solr_site_hash());
      $lang = $item['search_api_language']['value'];
      if (empty($base_urls[$lang])) {
        $url_options = array(
          'absolute' => TRUE,
        );
        if (isset($languages[$lang])) {
          $url_options['language'] = $languages[$lang];
        }
        $base_urls[$lang] = url(NULL, $url_options);
      }
      $doc
        ->setField('site', $base_urls[$lang]);
    }

    // Now add all fields contained in the item, with dynamic fields. Also,
    // gather the contents of all text fields to also add them to "content".
    $text_content = array();
    foreach ($item as $key => $field) {

      // If the field is not known for the index, something weird has
      // happened. We refuse to index the items and hope that the others are
      // OK.
      if (!isset($fields[$key])) {
        $type = search_api_get_item_type_info($index->item_type);
        $vars = array(
          '@field' => $key,
          '@type' => $type ? $type['name'] : $index->item_type,
          '@id' => $id,
        );
        watchdog('search_api_solr', 'Error while indexing: Unknown field @field set for @type with ID @id.', $vars, WATCHDOG_WARNING);
        $doc = NULL;
        break;
      }
      $text_content[] = $this
        ->addIndexField($doc, $fields[$key], $field['value'], $field['type']);
    }
    $doc
      ->setField('content', implode("\n\n", array_filter($text_content)));
    if ($doc) {
      $documents[] = $doc;
      $ret[] = $id;
    }
  }

  // Let other modules alter documents before sending them to solr.
  drupal_alter('search_api_solr_documents', $documents, $index, $items);
  $this
    ->alterSolrDocuments($documents, $index, $items);
  if (!$documents) {
    return array();
  }
  try {
    $this
      ->connect();
    $this->solr
      ->addDocuments($documents);
    if (!empty($index->options['index_directly'])) {
      $this
        ->scheduleCommit();
    }
    return $ret;
  } catch (SearchApiException $e) {
    watchdog_exception('search_api_solr', $e, "%type while indexing: !message in %function (line %line of %file).");
  }
  return array();
}