You are here

public function StreamingExpressionBuilder::__construct in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 src/Utility/StreamingExpressionBuilder.php \Drupal\search_api_solr\Utility\StreamingExpressionBuilder::__construct()
  2. 8.2 src/Utility/StreamingExpressionBuilder.php \Drupal\search_api_solr\Utility\StreamingExpressionBuilder::__construct()

StreamingExpressionBuilder constructor.

Parameters

\Drupal\search_api\IndexInterface $index: The Search API index entity.

Throws

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

File

src/Utility/StreamingExpressionBuilder.php, line 140

Class

StreamingExpressionBuilder
Provides methods for creating streaming expressions targeting a given index.

Namespace

Drupal\search_api_solr\Utility

Code

public function __construct(IndexInterface $index) {
  $server = $index
    ->getServerInstance();
  $this->serverId = $server
    ->id();
  $this->backend = $server
    ->getBackend();
  $this->connector = $this->backend
    ->getSolrConnector();
  $index_settings = Utility::getIndexSolrSettings($index);
  if (!$this->connector instanceof SolrCloudConnectorInterface) {
    throw new SearchApiSolrException('Streaming expressions are only supported by a Solr Cloud connector.');
  }
  $language_ids = array_merge(array_keys(\Drupal::languageManager()
    ->getLanguages()), [
    LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $this->collection = $index_settings['advanced']['collection'] ?: $this->connector
    ->getCollectionName();
  $this->checkpointsCollection = $this->connector
    ->getCheckpointsCollectionName();
  $this->indexFilterQuery = $this->backend
    ->getIndexFilterQueryString($index);
  $this->targetedIndexId = $this->backend
    ->getTargetedIndexId($index);
  $this->targetedSiteHash = $this->backend
    ->getTargetedSiteHash($index);
  $this->index = $index;
  $this->requestTime = $this->backend
    ->formatDate(\Drupal::time()
    ->getRequestTime());
  $this->allFieldsMapped = [];
  foreach ($this->backend
    ->getSolrFieldNamesKeyedByLanguage($language_ids, $index) as $search_api_field => $solr_field) {
    foreach ($solr_field as $language_id => $solr_field_name) {
      $this->allFieldsMapped[$language_id][$search_api_field] = $solr_field_name;
    }
  }
  foreach ($language_ids as $language_id) {
    $this->allFieldsMapped[$language_id] += [
      // Search API Solr Search specific fields.
      'id' => Utility::hasIndexJustSolrDocumentDatasource($index) ? $index
        ->get('datasource_settings')['solr_document']['id_field'] : 'id',
      'index_id' => 'index_id',
      'hash' => 'hash',
      'site' => 'site',
      'timestamp' => 'timestamp',
      'context_tags' => 'sm_context_tags',
      // @todo to be removed
      'spell' => 'spell',
    ];
    $this->allFieldsIncludingGraphFieldsMapped[$language_id] = $this->allFieldsMapped[$language_id] + [
      // Graph traversal reserved names. We can't get a conflict here since
      // all dynamic fields are prefixed.
      'node' => 'node',
      'collection' => 'collection',
      'field' => 'field',
      'level' => 'level',
      'ancestors' => 'ancestors',
    ];
    $this->sortFieldsMapped[$language_id] = [];
    if (!Utility::hasIndexJustSolrDocumentDatasource($index)) {
      foreach ($this->allFieldsMapped[$language_id] as $search_api_field => $solr_field) {
        if (strpos($solr_field, 't') === 0 || strpos($solr_field, 's') === 0) {
          $this->sortFieldsMapped[$language_id]['sort_' . $search_api_field] = Utility::encodeSolrName('sort' . SolrBackendInterface::SEARCH_API_SOLR_LANGUAGE_SEPARATOR . $language_id . '_' . $search_api_field);
        }
        elseif (preg_match('/^([a-z]+)m(_.*)/', $solr_field, $matches) && strpos($solr_field, 'random_') !== 0) {
          $this->sortFieldsMapped[$language_id]['sort' . Utility::decodeSolrName($matches[2])] = $matches[1] . 's' . $matches[2];
        }
        if (strpos($solr_field, 's') === 0 || strpos($solr_field, 'i') === 0 || strpos($solr_field, 'f') === 0 || strpos($solr_field, 'p') === 0 || strpos($solr_field, 'b') === 0 || strpos($solr_field, 'h') === 0) {
          $this->allDocValueFieldsMapped[$language_id][$search_api_field] = $solr_field;
        }
      }
    }
  }
  $this->queryHelper = $this->connector
    ->getQueryHelper();
}