You are here

protected function SearchApiSolrBackend::addIndexField in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::addIndexField()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::addIndexField()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::addIndexField()

Helper method for indexing.

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

Parameters

\Solarium\QueryType\Update\Query\Document\Document $doc:

$key:

array $values:

$type:

Return value

bool|float|int|string The first value of $values that has been added to the index.

1 call to SearchApiSolrBackend::addIndexField()
SearchApiSolrBackend::getDocuments in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Retrieves Solr documents from search api index items.

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 1605

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function addIndexField(Document $doc, $key, array $values, $type) {

  // Don't index empty values (i.e., when field is missing).
  if (!isset($values)) {
    return '';
  }
  if (strpos($type, 'solr_text_') === 0) {
    $type = 'text';
  }
  $first_value = '';

  // All fields.
  foreach ($values as $value) {
    if (NULL !== $value) {
      switch ($type) {
        case 'boolean':
          $value = $value ? 'true' : 'false';
          break;
        case 'date':
          $value = $this
            ->formatDate($value);
          if ($value === FALSE) {
            continue 2;
          }
          break;
        case 'solr_date_range':
          $start = $this
            ->formatDate($value
            ->getStart());
          $end = $this
            ->formatDate($value
            ->getEnd());
          $value = '[' . $start . ' TO ' . $end . ']';
          break;
        case 'integer':
          $value = (int) $value;
          break;
        case 'decimal':
          $value = (double) $value;
          break;
        case 'text':

          /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface $value */
          $tokens = $value
            ->getTokens();
          if (is_array($tokens) && !empty($tokens)) {
            foreach ($tokens as $token) {

              // @todo handle token boosts broken?
              // @see https://www.drupal.org/node/2746263
              if ($value = $token
                ->getText()) {
                $doc
                  ->addField($key, $value, $token
                  ->getBoost());
                if (!$first_value) {
                  $first_value = $value;
                }
              }
            }
            continue 2;
          }
          else {
            $value = $value
              ->getText();
          }

        // No break, now we have a string.
        case 'string':
        default:

          // Keep $value as it is.
          if (!$value) {
            continue 2;
          }
      }
      $doc
        ->addField($key, $value);
      if (!$first_value) {
        $first_value = $value;
      }
    }
  }
  return $first_value;
}