I tried to use this module with a search of Solr Document data source and got an error:

Error: Call to a member function create() on null in Drupal\search_api_solr\Plugin\search_api\datasource\SolrDocument->loadMultiple() (line 194 of /app/web/modules/contrib/search_api_solr/src/Plugin/search_api/datasource/SolrDocument.php)

#0 /app/web/modules/contrib/search_api/src/Entity/Index.php(879): Drupal\search_api_solr\Plugin\search_api\datasource\SolrDocument->loadMultiple(Array)
#1 /app/web/modules/contrib/search_api_autocomplete/src/Plugin/search_api_autocomplete/suggester/LiveResults.php(328): Drupal\search_api\Entity\Index->loadItemsMultiple(Array)
#2 /app/web/modules/contrib/search_api_autocomplete/src/Controller/AutocompleteController.php(135): Drupal\search_api_autocomplete\Plugin\search_api_autocomplete\suggester\LiveResults->getAutocompleteSuggestions(Object(Drupal\search_api\Query\Query), 'asse', 'asse')
#3 [internal function]: Drupal\search_api_autocomplete\Controller\AutocompleteController->autocomplete(Object(Drupal\search_api_autocomplete\Entity\Search), Object(Symfony\Component\HttpFoundation\Request))
#4 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#5 /app/web/core/lib/Drupal/Core/Render/Renderer.php(580): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#6 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#7 /app/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#8 /app/vendor/symfony/http-kernel/HttpKernel.php(169): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#9 /app/vendor/symfony/http-kernel/HttpKernel.php(81): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#10 /app/web/core/lib/Drupal/Core/StackMiddleware/Session.php(58): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#11 /app/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#12 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#13 /app/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#14 /app/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#15 /app/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 /app/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 /app/web/core/lib/Drupal/Core/DrupalKernel.php(718): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 /app/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#19 {main}

Comments

SerShevchyk created an issue. See original summary.

keshavv’s picture

Status: Active » Needs review
StatusFileSize
new728 bytes

Added patch.

sershevchyk’s picture

Status: Needs review » Needs work

The last patch applied well but autocomplete doesn't work. Also, I found the warning :

Could not load the following items on index Remote Data: "solr_document/1000740", "solr_document/1000713", "solr_document/1000608", "solr_document/1000634", "solr_document/1000699", "solr_document/1000633", "solr_document/1000548", "solr_document/1000547", "solr_document/1000534", "solr_document/1000530".

anweshasinha’s picture

Hi,
I have created patch for the search api autocomplete issue received above. Please review once.

viren18febs’s picture

Status: Needs work » Needs review
sershevchyk’s picture

Status: Needs review » Needs work

Any of the patches above didn't help with the problem on my project. I have created my own plugin and it helps me with the task, maybe it will be helpful for other developers. But we need to create a patch to this module and search_api.

<?php

namespace Drupal\custom\Plugin\search_api_autocomplete\suggester;

use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\Core\Url;
use Drupal\search_api\LoggerTrait;
use Drupal\search_api\Plugin\PluginFormTrait;
use Drupal\search_api\Processor\ProcessorPluginManager;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\Utility\Utility;
use Drupal\search_api_autocomplete\Plugin\search_api_autocomplete\suggester\LiveResults;
use Drupal\search_api_autocomplete\SearchApiAutocompleteException;
use Drupal\search_api_autocomplete\Suggester\SuggesterPluginBase;
use Drupal\search_api_autocomplete\Suggestion\SuggestionFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a suggester plugin that displays live results.
 *
 * @SearchApiAutocompleteSuggester(
 *   id = "solr_document_live_results",
 *   label = @Translation("Display live results (SolrDocument)"),
 *   description = @Translation("Display live results to visitors as they type. (Unless the server is configured to find partial matches, this will most likely only produce results once the visitor has finished typing.)"),
 * )
 */
class SolrDocumentLiveResults extends LiveResults implements PluginFormInterface {

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    /** @var static $plugin */
    $plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);

    return $plugin;
  }

  /**
   * {@inheritdoc}
   */
  public function getAutocompleteSuggestions(QueryInterface $query, $incomplete_key, $user_input) {
    $fulltext_fields = $this->configuration['fields'];
    $index = $query->getIndex();
    if ($fulltext_fields) {
      // Take care only to set fields that are still indexed fulltext fields.
      $index_fields = $index->getFulltextFields();
      $fulltext_fields = array_intersect($fulltext_fields, $index_fields);
      if ($fulltext_fields) {
        $query->setFulltextFields($fulltext_fields);
      }
      else {
        $args = [
          '@suggester' => $this->label(),
          '@search' => $this->getSearch()->label(),
          '@index' => $index->label(),
        ];
        $this->getLogger()->warning('Only invalid fulltext fields set for suggester "@suggester" in autocomplete settings for search "@search" on index "@index".', $args);
      }
    }
    $query->keys($user_input);

    try {
      $results = $query->execute();
    }
    catch (SearchApiException $e) {
      // If the query fails, there's nothing we can do about that.
      return [];
    }

    // Pre-load the result items for performance reasons.
    $item_ids = array_keys($results->getResultItems());

    // Group the requested items by datasource. This will also later be used to
    // determine whether all items were loaded successfully.
    $ids = [];
    foreach ($item_ids as $item_id) {
      [$datasource_id, $raw_id] = Utility::splitCombinedId($item_id);
      $ids[$raw_id] = $item_id;
    }

    $objects = [];
    try {
      // Query the index for the Solr documents.
      $results = $index->query()
        ->addCondition('search_api_id', array_keys($ids), 'IN')
        ->execute()
        ->getResultItems();
      foreach ($results as $id => $result) {
        $objects[$id] = \Drupal::getContainer()->get('solr_document.factory')->create($result);
      }
    }
    catch (SearchApiException $e) {
      // Couldn't load items from server, return an empty array.
    }

    $factory = new SuggestionFactory($user_input);

    $suggestions = [];
    foreach ($results as $item_id => $item) {
      // If the result object could not be loaded, there's little we can do
      // here.
      if (empty($objects[$item_id])) {
        continue;
      }

      $object = $objects[$item_id];
      $item->setOriginalObject($object);

      $label = $object->get('label')->getValue()[0];
      $id = $object->get('id')->getValue()[0];
      $url = Url::fromUserInput("/documents/$id");

      $suggestions[] = $factory->createUrlSuggestion($url, $label);
    }

    return $suggestions;
  }
}

drunken monkey’s picture

Status: Needs work » Closed (duplicate)

This was a bug in the Solr module and is already fixed, see #3345616: Fix SolrDocument::loadMultiple(). Please just update to the latest version of the Solr module.