Score:1

How to use groupBy on query based on the search API views?

tg flag

My search api query returns a list of articles with its nid and field category. I would like to group by category. I mean 1 article by category. Is there a way to accomplish this? I tried via the _views_query_alter but it looks that SearchApiQuery $query has not the function groupBy.

Ex:

HOOK_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query) {

     /** @var \Drupal\search_api\Plugin\views\query\SearchApiQuery $query */
     $query->addCondition('search_api_language', \Drupal::languageManager()->getCurrentLanguage()->getId());
     $query->groupBy('field_category');// NOT DEFINED
}

Not grouped:

 `{
   'nid': 1, 'field_category' :34,
   'nid': 2, 'field_category' :34,
   'nid': 3, 'field_category' :35
 }`

Grouped:

{
   'nid': 1, 'field_category' :34,
   'nid': 3, 'field_category' :35
 }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.