Commit 8f28b185 authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3133997 by dpi, mkalkbrenner, GaëlG: Avoid query caching in Search API...

Issue #3133997 by dpi, mkalkbrenner, GaëlG: Avoid query caching in Search API when a Search API Solr Exception is thrown
parent 54b6225b
Loading
Loading
Loading
Loading
+207 −186
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ use Composer\Semver\Comparator;
use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\ContentEntityInterface;
@@ -1476,6 +1477,7 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
   * @throws \Drupal\search_api_solr\SearchApiSolrException
   */
  public function search(QueryInterface $query) {
    try {
      /** @var \Drupal\search_api\Entity\Index $index */
      $index = $query->getIndex();

@@ -1644,9 +1646,9 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
        }

        // In previous versions we set a high value for rows if no limit was set
      // in the options. The intention was to retrieve "all" results instead of
      // falling back to Solr's default of 10. But for Solr Cloud it turned out
      // that independent from the real number of documents, Solr seems to
        // in the options. The intention was to retrieve "all" results instead
        // of falling back to Solr's default of 10. But for Solr Cloud it turned
        // out that independent of the real number of documents, Solr seems to
        // allocate rows*shards memory for sorting the distributed result. That
        // could lead to out of memory exceptions. The default limit is now
        // configurable as advanced server option.
@@ -1660,7 +1662,6 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter

        $this->applySearchWorkarounds($solarium_query, $query);

      try {
        // Allow modules to alter the solarium query.
        $this->moduleHandler->alterDeprecated('hook_search_api_solr_query_alter is deprecated will be removed in Search API Solr 4.3.0. Handle the PreQueryEvent instead.', 'search_api_solr_query', $solarium_query, $query);
        $this->preQuery($solarium_query, $query);
@@ -1797,9 +1798,14 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
        $this->dispatch($event);
        $search_api_result_set = $event->getSearchApiResultSet();
      }
    }
    catch (\Exception $e) {
        throw new SearchApiSolrException('An error occurred while trying to search with Solr: ' . $e->getMessage(), $e->getCode(), $e);
      if ($query instanceof RefinableCacheableDependencyInterface) {
        // Avoid caching of an empty result in Search API and views.
        // @see https://www.drupal.org/project/search_api_solr/issues/3133997
        $query->mergeCacheMaxAge(0);
      }
      throw $e;
    }
}

@@ -1975,6 +1981,7 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function executeStreamingExpression(QueryInterface $query) {
    try {
      $stream_expression = $query->getOption('solr_streaming_expression', FALSE);
      if (!$stream_expression) {
        throw new SearchApiSolrException('Streaming expression missing.');
@@ -1995,7 +2002,6 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter

      $result = NULL;

    try {
      $result = $connector->stream($stream, $this->getCollectionEndpoint($index));

      if ($processors = $query->getIndex()->getProcessorsByStage(ProcessorInterface::STAGE_POSTPROCESS_QUERY)) {
@@ -2029,6 +2035,11 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
      }
    }
    catch (StreamException $e) {
      if ($query instanceof RefinableCacheableDependencyInterface) {
        // Avoid caching of an empty result in Search API and views.
        // @see https://www.drupal.org/project/search_api_solr/issues/3133997
        $query->mergeCacheMaxAge(0);
      }
      $message = $e->getMessage() . "\n" . ExpressionBuilder::indent($e->getExpression());
      if ($comment = $query->getOption('solr_streaming_expression_comment', FALSE)) {
        $message .= "\nComment: " . $comment;
@@ -2036,7 +2047,12 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
      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);
      if ($query instanceof RefinableCacheableDependencyInterface) {
        // Avoid caching of an empty result in Search API and views.
        // @see https://www.drupal.org/project/search_api_solr/issues/3133997
        $query->mergeCacheMaxAge(0);
      }
      throw $e;
    }

    return $result;
@@ -2048,6 +2064,7 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function executeGraphStreamingExpression(QueryInterface $query) {
    try {
      $stream_expression = $query->getOption('solr_streaming_expression', FALSE);
      if (!$stream_expression) {
        throw new SearchApiSolrException('Streaming expression missing.');
@@ -2065,11 +2082,15 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
      $graph->setExpression($stream_expression);
      $this->applySearchWorkarounds($graph, $query);

    try {
      return $connector->graph($graph, $this->getCollectionEndpoint($index));
    }
    catch (\Exception $e) {
      throw new SearchApiSolrException('An error occurred while trying execute a streaming expression on Solr: ' . $e->getMessage(), $e->getCode(), $e);
      if ($query instanceof RefinableCacheableDependencyInterface) {
        // Avoid caching of an empty result in Search API and views.
        // @see https://www.drupal.org/project/search_api_solr/issues/3133997
        $query->mergeCacheMaxAge(0);
      }
      throw $e;
    }
  }