You are here

protected function SearchApiSolrBackend::addIndexField in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::addIndexField()
  2. 8.2 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 $doc: The Solarium document.

string $key: The key to use for the field.

array $values: The values for the field.

string $type: The field 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

File

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

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 (empty($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)) {

            // @todo Remove together with search_api_solr_legacy.
            $legacy_solr_version = FALSE;
            try {
              $connector = $this
                ->getSolrConnector();
              if ($legacy_solr_version = version_compare($connector
                ->getSolrMajorVersion(), '6', '<') && version_compare($connector
                ->getSolrMajorVersion(), '4', '>=')) {
                $boost = 0.0;
              }
            } catch (\Exception $e) {
            }
            foreach ($tokens as $token) {
              if ($value = $token
                ->getText()) {
                if ($legacy_solr_version) {

                  // Boosting field values at index time is only supported in
                  // old Solr versions.
                  // @todo Remove together with search_api_solr_legacy.
                  if ($token
                    ->getBoost() > $boost) {
                    $boost = $token
                      ->getBoost();
                  }
                  $doc
                    ->addField($key, $value, $boost);
                }
                else {
                  $doc
                    ->addField($key, $value);
                  $boost = $token
                    ->getBoost();
                  if (0.0 != $boost && 1.0 != $boost) {

                    // @todo This regex is a first approach to isolate the
                    //   terms to be boosted. We should consider to re-use the
                    //   logic of the tokenizer processor. But this might
                    //   require to turn some methods to public.
                    $doc
                      ->addField('boost_term', preg_replace('/([^\\s]{2,})/', '$1|' . sprintf('%.1f', $boost), str_replace('|', ' ', $value)));
                  }
                }
                if (!$first_value) {
                  $first_value = $value;
                }
              }
            }
            continue 2;
          }
          $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;
}