You are here

protected function SolrFieldManager::buildFieldDefinitionsFromSolr in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/SolrFieldManager.php \Drupal\search_api_solr\SolrFieldManager::buildFieldDefinitionsFromSolr()

Builds the field definitions for a Solr server from its Luke handler.

Parameters

\Drupal\search_api\IndexInterface $index: The index from which we are retrieving field information.

Return value

\Drupal\Core\TypedData\DataDefinitionInterface[] The array of field definitions for the server, keyed by field name.

Throws

\InvalidArgumentException

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\search_api\SearchApiException

1 call to SolrFieldManager::buildFieldDefinitionsFromSolr()
SolrFieldManager::buildFieldDefinitions in src/SolrFieldManager.php
Builds the field definitions for a Solr server.

File

src/SolrFieldManager.php, line 155

Class

SolrFieldManager
Manages the discovery of Solr fields.

Namespace

Drupal\search_api_solr

Code

protected function buildFieldDefinitionsFromSolr(IndexInterface $index) {

  /** @var \Drupal\search_api\ServerInterface|null $server */
  $server = $index
    ->getServerInstance();

  // Load the server entity.
  if ($server === NULL) {
    throw new \InvalidArgumentException('The Search API server could not be loaded.');
  }

  // In case the targeted Solr index may not have fields (yet) we'll return an
  // empty list.
  $fields = [];

  // Don't attempt to connect to server if config is disabled. Cache will
  // clear itself when server config is enabled again.
  if ($server
    ->status()) {
    $backend = $server
      ->getBackend();
    if (!$backend instanceof SolrBackendInterface) {
      throw new \InvalidArgumentException("The Search API server's backend must be an instance of SolrBackendInterface.");
    }
    try {
      $connector = $backend
        ->getSolrConnector();
      if ($connector instanceof SolrCloudConnectorInterface) {
        $connector
          ->setCollectionNameFromEndpoint($backend
          ->getCollectionEndpoint($index));
      }
      $luke = $connector
        ->getLuke();
      foreach ($luke['fields'] as $name => $definition) {
        $field = new SolrFieldDefinition($definition);
        $label = Unicode::ucfirst(trim(str_replace('_', ' ', $name)));
        $field
          ->setLabel($label);

        // The Search API can't deal with arbitrary item types. To make things
        // easier, just use one of those known to the Search API. Using strpos
        // matches point and trie variants as well, for example int, pint and
        // tint. Finally this function only feeds the presets for the config
        // form, so mismatches aren't critical.
        if (strpos($field
          ->getDataType(), 'text') !== FALSE) {
          $field
            ->setDataType('search_api_text');
        }
        elseif (strpos($field
          ->getDataType(), 'date') !== FALSE) {
          $field
            ->setDataType('timestamp');
        }
        elseif (strpos($field
          ->getDataType(), 'int') !== FALSE) {
          $field
            ->setDataType('integer');
        }
        elseif (strpos($field
          ->getDataType(), 'long') !== FALSE) {
          $field
            ->setDataType('integer');
        }
        elseif (strpos($field
          ->getDataType(), 'float') !== FALSE) {
          $field
            ->setDataType('float');
        }
        elseif (strpos($field
          ->getDataType(), 'double') !== FALSE) {
          $field
            ->setDataType('float');
        }
        elseif (strpos($field
          ->getDataType(), 'bool') !== FALSE) {
          $field
            ->setDataType('boolean');
        }
        else {
          $field
            ->setDataType('string');
        }
        $fields[$name] = $field;
      }
    } catch (SearchApiSolrException $e) {
      $this
        ->getLogger()
        ->error('Could not connect to server %server, %message', [
        '%server' => $server
          ->id(),
        '%message' => $e
          ->getMessage(),
      ]);
    }
  }
  return $fields;
}