You are here

protected function SearchApiSolrBackend::addIndexField in Search API Solr 4.x

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. 8.2 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.

array $boost_terms: Reference to an array where special boosts per term should be stored.

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 2439

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, array &$boost_terms) {

  // 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) {

                    // This regular expressions are a first approach to
                    // isolate the terms to be boosted. It might be that
                    // there's some more sophisticated logic required here.
                    // The unicode mode is required to handle multibyte white
                    // spaces of languages like Japanese.
                    $terms = preg_split('/\\s+/u', str_replace('|', ' ', $value));
                    foreach ($terms as $term) {
                      $len = mb_strlen($term);

                      // The length boundaries are defined as this for
                      // fieldType name="boost_term_payload" in schema.xml.
                      // Shorter or longer terms will be skipped anyway.
                      if ($len >= 2 && $len <= 100) {
                        if (!array_key_exists($term, $boost_terms) || $boost_terms[$term] < $boost) {
                          $boost_terms[$term] = $boost;
                        }
                      }
                    }
                  }
                }
                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;
}