You are here

public function SearchApiIndex::index in Search API 7

Indexes items on this index.

Will return an array of IDs of items that should be marked as indexed – i.e., items that were either rejected by a data-alter callback or were successfully indexed.

Parameters

array $items: An array of items to index, of this index's item type.

Return value

array An array of the IDs of all items that should be marked as indexed.

Throws

SearchApiException If an error occurred during indexing.

File

includes/index_entity.inc, line 458
Contains SearchApiIndex.

Class

SearchApiIndex
Class representing a search index.

Code

public function index(array $items) {
  if ($this->read_only) {
    return array();
  }
  if (!$this->enabled) {
    throw new SearchApiException(t("Couldn't index values on '@name' index (index is disabled)", array(
      '@name' => $this->name,
    )));
  }
  if (empty($this->options['fields'])) {
    throw new SearchApiException(t("Couldn't index values on '@name' index (no fields selected)", array(
      '@name' => $this->name,
    )));
  }
  $fields = $this->options['fields'];
  $custom_type_fields = array();
  foreach ($fields as $field => $info) {
    if (isset($info['real_type'])) {
      $custom_type = search_api_extract_inner_type($info['real_type']);
      if ($this
        ->server()
        ->supportsFeature('search_api_data_type_' . $custom_type)) {
        $fields[$field]['type'] = $info['real_type'];
        $custom_type_fields[$custom_type][$field] = search_api_list_nesting_level($info['real_type']);
      }
    }
  }
  if (empty($fields)) {
    throw new SearchApiException(t("Couldn't index values on '@name' index (no fields selected)", array(
      '@name' => $this->name,
    )));
  }

  // Mark all items that are rejected as indexed.
  $ret = array_keys($items);
  drupal_alter('search_api_index_items', $items, $this);
  if ($items) {
    $this
      ->dataAlter($items);
  }
  $ret = array_diff($ret, array_keys($items));

  // Items that are rejected should also be deleted from the server.
  if ($ret) {
    $this
      ->server()
      ->deleteItems($ret, $this);
  }
  if (!$items) {
    return $ret;
  }
  $data = array();
  foreach ($items as $id => $item) {
    $data[$id] = search_api_extract_fields($this
      ->entityWrapper($item), $fields);
    unset($items[$id]);
    foreach ($custom_type_fields as $type => $type_fields) {
      $info = search_api_get_data_type_info($type);
      if (isset($info['conversion callback']) && is_callable($info['conversion callback'])) {
        $callback = $info['conversion callback'];
        foreach ($type_fields as $field => $nesting_level) {
          if (isset($data[$id][$field]['value'])) {
            $value = $data[$id][$field]['value'];
            $original_type = $data[$id][$field]['original_type'];
            $data[$id][$field]['value'] = _search_api_convert_custom_type($callback, $value, $original_type, $type, $nesting_level);
          }
        }
      }
    }
  }
  $this
    ->preprocessIndexItems($data);
  return array_merge($ret, $this
    ->server()
    ->indexItems($this, $data));
}