Terms.php in Search API Solr 8.2
Same filename and directory in other branches
File
src/Plugin/search_api_autocomplete/suggester/Terms.phpView source
<?php
namespace Drupal\search_api_solr\Plugin\search_api_autocomplete\suggester;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api_autocomplete\Plugin\search_api_autocomplete\suggester\Server;
use Drupal\search_api_solr\SolrAutocompleteInterface;
/**
* Provides a suggester that retrieves suggestions from Solr's Terms component.
*
* @SearchApiAutocompleteSuggester(
* id = "search_api_solr_terms",
* label = @Translation("Solr Terms"),
* description = @Translation("Autocomplete the entered string based on Solr's Terms component. Note: Be careful when activating this feature if you run multiple indexes in one Solr core! The Terms component is not able to distinguish between the different indexes and returns matching terms for the complete core. If you run multiple indexes in one core the term counts are not correct and you might get suggestions that lead to zero results on a specific index! You can mitigate that effect if you ensure that the fulltext field names are completely different in the indexes.")
* )
*/
class Terms extends Server {
/**
* {@inheritdoc}
*/
public function getAutocompleteSuggestions(QueryInterface $query, $incomplete_key, $user_input) {
if (!($backend = static::getBackend($this
->getSearch()
->getIndex()))) {
return [];
}
if ($this->configuration['fields']) {
$query
->setFulltextFields($this->configuration['fields']);
}
else {
$query
->setFulltextFields($query
->getIndex()
->getFulltextFields());
}
return $backend
->getAutocompleteSuggestions($query, $this
->getSearch(), $incomplete_key, $user_input);
}
/**
* {@inheritdoc}
*/
protected static function getBackend(IndexInterface $index) {
if (!$index
->hasValidServer()) {
return NULL;
}
$server = $index
->getServerInstance();
$backend = $server
->getBackend();
if ($server
->supportsFeature('search_api_autocomplete') && $backend instanceof SolrAutocompleteInterface) {
return $backend;
}
return NULL;
}
}