You are here

protected function SearchApiElasticsearchBackend::getSortSearchQuery in Elasticsearch Connector 8

Helper function that return Sort for query in search.

1 call to SearchApiElasticsearchBackend::getSortSearchQuery()
SearchApiElasticsearchBackend::getSearchQueryOptions in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Helper function return associative array with query options.

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 1159
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

protected function getSortSearchQuery(QueryInterface $query) {
  $index_fields = $this
    ->getIndexFields($query);
  $sort = array();
  foreach ($query
    ->getSorts() as $field_id => $direction) {
    $direction = Unicode::strtolower($direction);
    if ($field_id === 'search_api_relevance') {
      $sort['_score'] = $direction;
    }
    elseif ($field_id === 'search_api_id') {
      $sort['id'] = $direction;
    }
    elseif (isset($index_fields[$field_id])) {
      $sort[$field_id] = $direction;
    }
    else {
      throw new \Exception(t('Incorrect sorting!.'));
    }
  }
  return $sort;
}