3

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

1 Answer 1

0

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.

1
  • I have one content type where the above configuration is not having the same result. No idea yet why.
    – aharown07
    Mar 23, 2022 at 15:00

Your Answer

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

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