You are here

public function SearchApiSolrBackend::executeStreamingExpression in Search API Solr 8.3

Same name and namespace in other branches
  1. 8.2 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()

Throws

\Drupal\Component\Plugin\Exception\PluginException

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 1795

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.');
  }
  $index = $query
    ->getIndex();
  $this
    ->finalizeIndex($index);
  $stream = $connector
    ->getStreamQuery();
  $stream
    ->setExpression($stream_expression);
  $stream
    ->setOptions([
    'documentclass' => StreamDocument::class,
  ]);
  $this
    ->applySearchWorkarounds($stream, $query);
  $result = NULL;
  try {
    $result = $connector
      ->stream($stream, $this
      ->getCollectionEndpoint($index));
    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;
                  }
                }
                unset($array_value);
                $document->{$field_name} = $field_value;
              }
            }
          }
        }
      }
    }
  } catch (StreamException $e) {
    $message = $e
      ->getMessage() . "\n" . ExpressionBuilder::indent($e
      ->getExpression());
    if ($comment = $query
      ->getOption('solr_streaming_expression_comment', FALSE)) {
      $message .= "\nComment: " . $comment;
    }
    throw new SearchApiSolrException($message, $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;
}