You are here

public function SearchApiSolrCommands::executeRawStreamingExpression in Search API Solr 8.3

Same name and namespace in other branches
  1. 4.x src/Commands/SearchApiSolrCommands.php \Drupal\search_api_solr\Commands\SearchApiSolrCommands::executeRawStreamingExpression()

Executes a streaming expression from STDIN.

@command search-api-solr:execute-raw-streaming-expression

@usage drush search-api-solr:execute-streaming-expression node_index - < streaming_expression.txt Execute the raw streaming expression in streaming_expression.txt

@aliases solr-erse

Parameters

string $indexId: A search index ID.

mixed $expression: The streaming expression. Use '-' to read from STDIN.

Return value

string The JSON encoded raw streaming expression result

Throws

\Drupal\search_api_solr\SearchApiSolrException

\Drupal\search_api\SearchApiException

File

src/Commands/SearchApiSolrCommands.php, line 173

Class

SearchApiSolrCommands
Defines Drush commands for the Search API Solr.

Namespace

Drupal\search_api_solr\Commands

Code

public function executeRawStreamingExpression($indexId, $expression) {

  // Special flag indicating that the value has been passed via STDIN.
  if ($expression === '-') {
    $expression = $this
      ->stdin()
      ->contents();
  }
  if (!$expression) {
    throw new SearchApiSolrException('No streaming expression provided.');
  }
  $indexes = $this->commandHelper
    ->loadIndexes([
    $indexId,
  ]);

  /** @var \Drupal\search_api\IndexInterface $index */
  $index = reset($indexes);
  if (!$index) {
    throw new SearchApiSolrException('Failed to load index.');
  }
  if (!$index
    ->status()) {
    throw new SearchApiSolrException('Index is not enabled.');
  }
  if ($server = $index
    ->getServerInstance()) {

    /** @var \Drupal\search_api_solr\SolrBackendInterface $backend */
    $backend = $server
      ->getBackend();
    if (!$backend instanceof SolrBackendInterface || !$backend
      ->getSolrConnector() instanceof SolrCloudConnectorInterface) {
      throw new SearchApiSolrException('The index must be located on Solr Cloud to execute streaming expressions.');
    }
    $queryHelper = \Drupal::service('search_api_solr.streaming_expression_query_helper');
    $query = $queryHelper
      ->createQuery($index);
    $queryHelper
      ->setStreamingExpression($query, $expression, basename(__FILE__) . ':' . __LINE__);
    $result = $backend
      ->executeStreamingExpression($query);
    return $result
      ->getBody();
  }
  throw new SearchApiSolrException('Server could not be loaded.');
}