You are here

public function StreamingExpressionBuilder::__construct in Search API Solr 8.2

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

StreamingExpressionBuilder constructor.

Parameters

\Drupal\search_api\IndexInterface $index:

Throws

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

File

src/Utility/StreamingExpressionBuilder.php, line 70

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();

  /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
  $backend = $server
    ->getBackend();
  $connector = $backend
    ->getSolrConnector();
  if (!$connector instanceof SolrCloudConnectorInterface) {
    throw new SearchApiSolrException('Streaming expression are only supported by a Solr Cloud connector.');
  }
  $this->collection = $connector
    ->getCollectionName();
  $this->index_filter_query = $backend
    ->getIndexFilterQueryString($index);
  $this->index = $index;
  $this->request_time = $backend
    ->formatDate(\Drupal::time()
    ->getRequestTime());
  $this->all_fields_mapped = $backend
    ->getSolrFieldNames($index) + [
    // Search API Solr Search specific fields.
    'id' => 'id',
    'index_id' => 'index_id',
    'hash' => 'hash',
    'site' => 'site',
    'timestamp' => 'timestamp',
    'context_tags' => 'sm_context_tags',
    // @todo to be removed
    'spell' => 'spell',
  ];
  $this->field_name_mapping = $this->all_fields_mapped + [
    // 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->sort_fields = [];
  foreach ($this->all_fields_mapped as $search_api_field => $solr_field) {
    if (strpos($solr_field, 't') === 0 || strpos($solr_field, 's') === 0) {
      $this->sort_fields['sort_' . $search_api_field] = 'sort_' . Utility::encodeSolrName($search_api_field);
    }
    elseif (preg_match('/^([a-z]+)m(_.*)/', $solr_field, $matches) && strpos($solr_field, 'random_') !== 0) {
      $this->sort_fields['sort' . Utility::decodeSolrName($matches[2])] = $matches[1] . 's' . $matches[2];
    }
    if (strpos($solr_field, 'sd') === 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->all_doc_value_fields_mapped[$search_api_field] = $solr_field;
    }
  }
  $this->query_helper = $connector
    ->getQueryHelper();
}