You are here

public function SearchApiElasticsearchBackend::search in Elasticsearch Connector 8.5

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.2 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

1 call to SearchApiElasticsearchBackend::search()
SearchApiElasticsearchBackend::getAutocompleteSuggestions in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Implements SearchApiAutocompleteInterface::getAutocompleteSuggestions().

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 578

Class

SearchApiElasticsearchBackend
Elasticsearch Search API Backend definition.

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 = $this->indexFactory
    ->index($index, TRUE);

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

  // Add the facets to the request.
  if ($query
    ->getOption('search_api_facets')) {
    $this
      ->addFacets($query);
  }

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

  // Note that this requires fielddata option to be enabled.
  // @see ::getAutocompleteSuggestions()
  // @see \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::mapping()
  if ($incomplete_key = $query
    ->getOption('autocomplete')) {

    // Autocomplete suggestions are determined using a term aggregation (like
    // facets), but filtered to only include facets with the right prefix.
    // As the search facet field is analyzed, facets are tokenized terms and
    // all in lower case. To match that, we need convert the our filter to
    // lower case also.
    $incomplete_key = strtolower($incomplete_key);

    // Note that we cannot use the elasticsearch client aggregations API as
    // it does not support the "include" parameter.
    $params['body']['aggs']['autocomplete']['terms'] = [
      'field' => $query
        ->getOption('autocomplete_field'),
      'include' => $incomplete_key . '.*',
    ];
  }
  try {

    // Allow modules to alter the Elastic Search query.
    $this
      ->preQuery($query);

    // Do search.
    $response = $this->client
      ->search($params)
      ->getRawResponse();
    $results = SearchFactory::parseResult($query, $response);

    // Handle the facets result when enabled.
    if ($query
      ->getOption('search_api_facets')) {
      $this
        ->parseFacets($results, $query);
    }

    // Allow modules to alter the Elastic Search Results.
    $this
      ->postQuery($results, $query, $response);
    return $results;
  } catch (\Exception $e) {
    watchdog_exception('Elasticsearch API', $e);
    return $search_result;
  }
}