Score:1

Is there a way to default sort by date when no search keywords have been used, but default sort by relevance for all searches?

pe flag

At present, my project has default search pages for sections of the site. They are per-content type. When the user goes the "search page" for the content type, they see a views-generated search result page unfiltered by any keywords. In other words, it displays all the items for that content type. The view:

has expose form in a block enabled settings on the exposed form has 'input required' disabled

What I want to happen...

I'd like users to

  • see the unfiltered search result page sorted by newest first. At present, this page, if set to relevance, shows old content first.
  • automatically switch to relevance sorting when they enter keywords and execute a search. At present, I've set the first sort to date, to solve the 'old content first' problem, so users have to manually change the sort to relevance when doing a search.

Some options... I could separate the list pages from the search result pages. My question is, is there a way to make an unfiltered "search result" views page show the newest content first, while having the default sort actually set to relevance, so nobody has to manually change it? How does that sort really work when there aren't any keywords to supply "relevance"?

My config basics...

  • Drupal 9.3.7
  • search_api
  • facets
  • search_api_solr
Score:0
pe flag

I think I have one solution. I remain interested in knowing if there is a better way. Here's what seems to be working so far:

In the sort section of the view, I have these fields, in this order:

  • Relevance (descending): exposed
  • Date field (descending): exposed
  • Date field (same one, descending): not exposed

After clearing caches (important), the result seems to be that navigating to the search result page without doing a search, lists all the content (as filtered) by 'Date field' descending, but the search form and its exposed sort indicates Relevance. When I key in terms and launch the search, it comes back relevance sorted.

I'm not sure why this is working though. On the views config page the query without keywords looks like this:

Index: main_index
Keys: NULL
Conditions:
  [
    status = 1
  AND
    type IN array (
          'resource' => 'resource',
        )
  ]
Sorting: search_api_relevance DESC, field_display_date DESC
Options: array (
    'search_api_view' => 'object (Drupal\\views\\ViewExecutable)',
    'search_api_base_path' => 'search/resources',
  )

I may learn more from the mysql query log, but I admit: I'm never sure I understand what I'm seeing there.

pe flag
I have one content type where the above configuration is not having the same result. No idea yet why.
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.