All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Ordering results by relevance or date

Jim Riley 19 Jul 2012 07:30 problem, complete

I'm trying to offer users an option to list search results based either on date (latest first) or relevance (based on a descending order of the low_search_score).

The search output at the moment is displaying by latest entry date - the "relevance option" doesnt seem to work

Dont think I've quite got the coding right on this one. Can anyone see what I'm doing wrong?

The code for my search form is:

{exp:low_search:form query="{segment_3}" result_page="site/search-output"}

{if error_message != ''}
{error_message}

{/if}

Fuzzy search

Any word
All words
Exact phrase

{collections show="not private_collection"}

{collection_label}

{/collections}

Show
10
25
50
results per page

Search

{/exp:low_search:form}

The output code for my search results is:

{exp:low_search:results query="{segment_3}" limit="40" orderby="orderby"}

{if count == 1}{/if}



{title}

Posted by {author} on {entry_date format="%F %j, %Y"} in the {channel_title} blog (score {low_search_score})

{low_search_excerpt}

{if count == total_results}{/if}

{paginate}
Page {current_page} of {total_pages} pages {pagination_links}

{/paginate}

{if no_results}

Sorry, your query did not return any results.
Check your spelling or try a different search term.


{/if}

{if no_results}No search results{/if}
{/exp:low_search:results}



Replies

  1. Low 19 Jul 2012 08:07

    Hi Jim,

    First off, a tip when showing code -- use http://pastie.org to share code. GetSatisfaction messes things up.

    If you want to give users the choice on how to sort the results, just add a drop down to the search form, like this: http://pastie.org/4282529

    And just as I was testing this, I found a bug. It seems that the relevance order is forced if the orderby="" parameter is not set in the low_search:results tag, thus ignoring what was posted in the form. I've already got a fix for it, so if you want, you can go ahead and open mod.low_search.php, go to line #708 where it says:

    $orderby = $this->EE->TMPL->fetch_param('orderby', 'low_search_score');


    ...and replace that with this:

    $orderby = ( ! empty($params['orderby'])) 
    ? (string) $params['orderby']
    : $this->EE->TMPL->fetch_param('orderby', 'low_search_score');


    Then you can omit the orderby="" parameter in the low_search:results tag and keep it as a select in the search form.

  2. Jim Riley 19 Jul 2012 08:23

    Fantastic - that works a treat.

    Just thinking out aloud, are there other options we can offer users in terms of orderby other than date and relevance?

  3. Low 19 Jul 2012 08:25

    You can use any of the values here: http://expressionengine.com/user_guid...

  4. Jim Riley 19 Jul 2012 12:12

    Does anyone have some example code of how I could add an option to select dates (from, to) which define a date range for search output?

  5. Low 19 Jul 2012 12:39

    You could use the start_on="" and stop_before="" parameters: http://expressionengine.com/user_guid...

    You just need to populate the values yourself.