You are here

protected function SearchBuilder::getSortSearchQuery in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSortSearchQuery()
  2. 8.5 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSortSearchQuery()
  3. 8.6 src/ElasticSearch/Parameters/Builder/SearchBuilder.php \Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder\SearchBuilder::getSortSearchQuery()

Helper function that return Sort for query in search.

Return value

array

Throws

\Exception

1 call to SearchBuilder::getSortSearchQuery()
SearchBuilder::getSearchQueryOptions in src/ElasticSearch/Parameters/Builder/SearchBuilder.php
Helper function return associative array with query options.

File

src/ElasticSearch/Parameters/Builder/SearchBuilder.php, line 270

Class

SearchBuilder
Class SearchBuilder.

Namespace

Drupal\elasticsearch_connector\ElasticSearch\Parameters\Builder

Code

protected function getSortSearchQuery() {
  $index_fields = $this->index
    ->getFields();
  $sort = [];
  foreach ($this->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 {

      // TODO: no silly exceptions...
      throw new \Exception(t('Incorrect sorting!.'));
    }
  }
  return $sort;
}