You are here

protected function SearchApiSolrService::addIndexField in Search API Solr 7

Helper method for indexing.

Adds $value with field name $key to the document $doc. The format of $value is the same as specified in SearchApiServiceInterface::indexItems().

1 call to SearchApiSolrService::addIndexField()
SearchApiSolrService::indexItems in includes/service.inc
Indexes the specified items.

File

includes/service.inc, line 763

Class

SearchApiSolrService
Search service class using Solr server.

Code

protected function addIndexField(SearchApiSolrDocument $doc, $key, $value, $type, $multi_valued = FALSE) {
  $text_content = '';

  // Don't index empty values (i.e., when field is missing).
  if (!isset($value)) {
    return $text_content;
  }
  if (search_api_is_list_type($type)) {
    $type = substr($type, 5, -1);
    foreach ($value as $v) {
      $text_content .= $this
        ->addIndexField($doc, $key, $v, $type, TRUE) . "\n\n";
    }
    return trim($text_content);
  }
  switch ($type) {
    case 'tokens':
      foreach ($value as $v) {
        $text_content .= $v['value'] . ' ';
        $doc
          ->addField($key, $v['value']);
      }
      return trim($text_content);
    case 'boolean':
      $value = $value ? 'true' : 'false';
      break;
    case 'date':
      $value = is_numeric($value) ? (int) $value : strtotime($value);
      if ($value === FALSE) {
        return $text_content;
      }
      $value = format_date($value, 'custom', self::SOLR_DATE_FORMAT, 'UTC');
      break;
    case 'integer':
      $value = (int) $value;
      break;
    case 'decimal':
      $value = (double) $value;
      break;
  }
  if ($multi_valued) {
    $doc
      ->addField($key, $value);
  }
  else {
    $doc
      ->setField($key, $value);
  }
  if (search_api_is_text_type($type)) {
    $text_content = $value;
  }
  return $text_content;
}