You are here

public function SearchApiSolrBackend::executeStreamingExpression in Search API Solr 8.2

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::executeStreamingExpression()
  2. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::executeStreamingExpression()

Executes a streaming expression.

Parameters

\Drupal\search_api\Query\QueryInterface $query:

Return value

\Solarium\QueryType\Stream\Result

Throws

\Drupal\search_api\SearchApiException

\Drupal\search_api_solr\SearchApiSolrException

Overrides SolrBackendInterface::executeStreamingExpression

1 call to SearchApiSolrBackend::executeStreamingExpression()
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

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

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function executeStreamingExpression(QueryInterface $query) {
  $stream_expression = $query
    ->getOption('solr_streaming_expression', FALSE);
  if (!$stream_expression) {
    throw new SearchApiSolrException('Streaming expression missing.');
  }
  $connector = $this
    ->getSolrConnector();
  if (!$connector instanceof SolrCloudConnectorInterface) {
    throw new SearchApiSolrException('Streaming expression are only supported by a Solr Cloud connector.');
  }
  $this
    ->finalizeIndex($query
    ->getIndex());
  $stream = $connector
    ->getStreamQuery();
  $stream
    ->setExpression($stream_expression);
  $stream
    ->setOptions([
    'documentclass' => 'Drupal\\search_api_solr\\Solarium\\Result\\StreamDocument',
  ]);
  $this
    ->applySearchWorkarounds($stream, $query);
  $result = NULL;
  try {
    $result = $connector
      ->stream($stream);
    if ($processors = $query
      ->getIndex()
      ->getProcessorsByStage(ProcessorInterface::STAGE_POSTPROCESS_QUERY)) {
      foreach ($processors as $key => $processor) {
        if (!$processor instanceof SolrProcessorInterface) {
          unset($processors[$key]);
        }
      }
      if (count($processors)) {
        foreach ($processors as $processor) {

          /** @var \Drupal\search_api_solr\Solarium\Result\StreamDocument $document */
          foreach ($result as $document) {
            foreach ($document as $field_name => $field_value) {
              if (is_string($field_value)) {
                $document->{$field_name} = $processor
                  ->decodeStreamingExpressionValue($field_value) ?: $field_value;
              }
              elseif (is_array($field_value)) {
                foreach ($field_value as &$array_value) {
                  if (is_string($array_value)) {
                    $array_value = $processor
                      ->decodeStreamingExpressionValue($array_value) ?: $array_value;
                  }
                }
                $document->{$field_name} = $field_value;
              }
            }
          }
        }
      }
    }
  } catch (StreamException $e) {
    throw new SearchApiSolrException($e
      ->getMessage() . "\n" . Expression::indent($e
      ->getExpression()), $e
      ->getCode(), $e);
  } catch (\Exception $e) {
    throw new SearchApiSolrException('An error occurred while trying execute a streaming expression on Solr: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  }
  return $result;
}