You are here

public function SearchApiElasticsearchBackend::search in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::search()
  2. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::search()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::search()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::search()

Executes a search on this server.

Parameters

\Drupal\search_api\Query\QueryInterface $query: The query to execute.

Throws

\Drupal\search_api\SearchApiException Thrown if an error prevented the search from completing.

Overrides BackendSpecificInterface::search

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 407
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function search(QueryInterface $query) {

  // Results.
  $search_result = $query
    ->getResults();

  // Get index.
  $index = $query
    ->getIndex();
  $params = IndexFactory::index($index, TRUE);

  // Check elasticsearch index.
  if (!$this->client
    ->indices()
    ->existsType($params)) {
    return $search_result;
  }

  // Build Elastica query.
  $params = SearchFactory::search($query);

  // Do search.
  $response = $this->client
    ->search($params);
  try {
    return SearchFactory::parseResult($query, $response);
  } catch (\Exception $e) {
    watchdog_exception('Elasticsearch API', $e);
    return $search_result;
  }
}