You are here

protected function SearchApiSolrBackend::setSorts in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setSorts()
  2. 8 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setSorts()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::setSorts()

Sets sorting for the query.

1 call to SearchApiSolrBackend::setSorts()
SearchApiSolrBackend::search in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Options on $query prefixed by 'solr_param_' will be passed natively to Solr as query parameter without the prefix. For example you can set the "Minimum Should Match" parameter 'mm' to '75%' like this:

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 3304

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

protected function setSorts(Query $solarium_query, QueryInterface $query, $field_names = []) {
  foreach ($query
    ->getSorts() as $field => $order) {
    $f = '';

    // First wee need to handle special fields which are prefixed by
    // 'search_api_'. Otherwise they will erroneously be treated as dynamic
    // string fields by the next detection below because they start with an
    // 's'. This way we for example ensure that search_api_relevance isn't
    // modified at all.
    if (strpos($field, 'search_api_') === 0) {
      if ($field == 'search_api_random') {

        // The default Solr schema provides a virtual field named "random_*"
        // that can be used to randomly sort the results; the field is
        // available only at query-time. See schema.xml for more details about
        // how the "seed" works.
        $params = $query
          ->getOption('search_api_random_sort', []);

        // Random seed: getting the value from parameters or computing a new
        // one.
        $seed = !empty($params['seed']) ? $params['seed'] : mt_rand();
        $f = $field_names[$field] . '_' . $seed;
      }
    }
    else {

      // @todo Both detections are redundant to some parts of
      //   SearchApiSolrBackend::getDocuments(). They should be combined in a
      //   single place to avoid errors in the future.
      if (strpos($field_names[$field], 't') === 0 || strpos($field_names[$field], 's') === 0) {

        // For fulltext fields use the dedicated sort field for faster alpha
        // sorts. Use the same field for strings to sort on a normalized
        // value.
        $f = 'sort_' . Utility::encodeSolrName($field);
      }
      elseif (preg_match('/^([a-z]+)m(_.*)/', $field_names[$field], $matches)) {

        // For other multi-valued fields (which aren't sortable by nature) we
        // use the same hackish workaround like the DB backend: just copy the
        // first value in a single value field for sorting.
        $f = $matches[1] . 's' . $matches[2];
      }
    }
    if (!$f) {
      $f = $field_names[$field];
    }
    $solarium_query
      ->addSort($f, strtolower($order));
  }
}