0

Drupal 9.4.8. Search api solr 4.2.9

I have created a search api solr search in views

enter image description here

One of the fields included in this view is a custom view field: node_date_raw_field

From module hook_views_data():

  $data['views']['node_date_raw_field'] = [
    'title' => t('Node Date (raw)'),
    'help' => t('Node Date raw field plugin.'),
    'field' => [
      'id' => 'node_date_raw_field',
    ],
    'sort' => [
      'field' => 'node_date_raw_field',
      'id' => 'standard',
    ],
  ];

This field simply brings back the node created date field associated with the current row content datasource or file datasource. What I want to do is sort the search results of the view by this custom field using hook_views_query_alter().

None of the examples I have found so far seem to work. This below is what I have tried:

function sbn_views_query_alter(Drupal\views\ViewExecutable $view, Drupal\views\Plugin\views\query\QueryPluginBase $query) {
  if ($view->id() == 'solr_search') {

       # https://drupalize.me/tutorial/alter-query-used-view?p=3463
       # $query->sort('node_date_raw_field', 'DESC'); // Unlike the other options, nothing shows up in sort results
       # Error message: *Sorting by "node_date_raw_field" has no valid solr field*.

       // Override existing Order rules.
        #$query->orderby = [];
        # $query->addOrderBy(NULL, NULL, 'DESC', 'node_date_raw_field');
        # Simply doesn't sort.

        # https://www.zyxware.com/articles/3885/drupal-how-to-use-drupal-hookview…
        #$query->orderby[0] = array(
        #    'field' => 'node_date_raw_field',
        #    'direction' => 'desc',
        #);
        # Notice: Undefined property: Drupal\search_api\Plugin\views\query\SearchApiQuery::$orderby in sbn_views_query_alter()             

       # $query->addOrderBy(NULL, NULL, 'desc', 'node_date_raw_field');
  }
}

Everything above brings back unsorted results except for $query->sort which errors out.

I checked the $query field and the field node_date_raw_field is there:

 [field] => Array
                                        (
                                            [label] => Drupal\search_api\Plugin\views\field\SearchApiEntityField
                                            [title] => Drupal\search_api\Plugin\views\field\SearchApiEntityField
                                            [search_api_excerpt] => Drupal\search_api\Plugin\views\field\SearchApiStandard
                                            [uri] => Drupal\search_api\Plugin\views\field\SearchApiEntityField
                                            [node_label_field] => Drupal\sbn\Plugin\views\field\NodeLabelField
                                            [node_id_field] => Drupal\sbn\Plugin\views\field\NodeIDField
                                            [group_label_field] => Drupal\sbn\Plugin\views\field\GroupLabelField
                                            [node_date_field] => Drupal\sbn\Plugin\views\field\NodeDateField
                                            [node_date_raw_field] => Drupal\sbn\Plugin\views\field\NodeDateRawField
                                        )

Also checked the class: echo get_class($query); Class used: Drupal\search_api\Plugin\views\query\SearchApiQuery

Is it simply not possible to sort a search api solr view by a views custom field, or am I missing something above?

1 Answer 1

0

From this link, Programmatically change order, I found this comment:

Solr queries don't use the database (it would defeat the purpose of using Solr if they did). You'll need to use hook_search_api_solr_query_alter and alter the query provided by the Solarium library. Keep in mind it's not a database query, so you'll probably have to consult the Solr docs to find out the correct syntax to use to reproduce the sorting you need. If you're on an older version of Solr (4.x and below), the options for complex sorting are somewhat limited, so be sure that you're consulting the right version of the documentation to avoid frustration

Given that this view is a search api solr indexed view, hook_views_query_alter will never give me the results I am looking for. So, I had to rethink my approach.

The soltion was an Aggregated Field.

In Configuration -> Search and metadata -> Search API -> Solr index -> Fields

I created a new, aggregated field of the timestamp field from each of the solr datasources in my view:

enter image description here

This creates a field that contains the timestamp from whichever one of the datasources (file, group_content, node, comment) happens to be in the row that is being returned. If there is more than one datasource in the row, this field will be the datasource timestamp with the highest value (i.e., most recent date).

Got the machine name of this field after it was created, and made sure it was Integer:

enter image description here

Next, I went and edited the view. I added the aggregated field to the field list, and also made it the sort criteria for the view results:

enter image description here

Saved the view, re-indexed the site, and voila! Search results for the search api solr indexed view are sorted by the aggregated field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.