The default sort option is relevancy, at the moment it is not possible to change this.

I've attached a patch creates a property for default search, and default search direction, and a setter for them both to SolrBaseQuery. This setter can be called from hook__apachesolr_query_prepare. Example usage:

function MYMODULE_apachesolr_query_prepare($query) {
  $query->setDefaultSort('ds_created', 'desc');
}

Please note there is a snippet of code which has been shared on a previous issue (https://drupal.org/node/721258) -

/**
 * Implements hook_apachesolr_prepare_query().
 */
function MYMODULE_apachesolr_query_prepare($query, $caller) {
  // If no manual sort set in querystring, set default sort to created desc
  if (!isset($_GET['solrsort'])) {
    $query->setSolrSort('ds_created', 'desc');
  }
}

This does change the default sort order, BUT it also prevents the relevancy sort order from being used. This is because the relevancy sort order link doesn't include the relevancy query parameters (?solrsort=score%20desc), and so the search order gets set to ds_created.

Comments

__cj’s picture

Issue summary: View changes
__cj’s picture

Issue summary: View changes
nick_vh’s picture

There's already a module that does this for you? It's apachesolr_sort. Let me know if that is sufficient and if you can, please close the ticket afterwards :-)

nick_vh’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Closing unless a strong argument comes back that we should put this in the main module

__cj’s picture

That's what I'd assumed.

The Apache Solr Sort lets you change the order of the sort by options, and disable the sort option, although it doesn't seem possible to disable the default sort option.

I would strongly agree the UI for this belongs with the the Apache Solr Sort, and I intend on sending a patch their way when it's possible to change the default sort order.

All this patch does is provide an easy way to change the default sort order with code.

__cj’s picture

Status: Closed (won't fix) » Active
__cj’s picture

Issue summary: View changes
StatusFileSize
new2.93 KB

Attached is an updated patch.

mas0h’s picture

Apache solr sort module only works if a keyword is not given to the search box, so this patch will be useful when a search keyword is given, but what this patch do after applying it? I can't see any settings after applying the patch?!

__cj’s picture

This patch only adds a function to set the default search order, which is hardcoded to relevancy in the code. There are no accompanying setting here.

nick_vh’s picture

  • +++ b/Solr_Base_Query.php
    @@ -586,7 +605,23 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa
    +  /**
    +   * Sets the default sort order.
    +   * ¶
    +   * @param string $name
    +   *   The sort order name.
    +   * ¶
    +   * @param string $direction
    +   *   The sort direction, this will be either 'asc' or 'desc'.
    +   * ¶
    +   */
    

    Spacing issues here in the comments.

  • +++ b/Solr_Base_Query.php
    @@ -586,7 +605,23 @@ class SolrBaseQuery extends SolrFilterSubQuery implements DrupalSolrQueryInterfa
    +  public function setDefaultSort($name, $direction) {
    +    $this->default_sort_name = $name;
    +    $this->default_sort_direction = $direction;
    +    $this->parseSortString();
    +    return $this;
       }
    

    Would it be useful to get a more simple getter function for the default sort?

  • Aside of that it looks good

    Thanks for your contribution. It's very much appreciated and I hope you continue to contribute to our issue queue. We can use all the help we can get.

    nick_vh’s picture

    Status: Active » Needs work
    __cj’s picture

    StatusFileSize
    new2.57 KB

    Updated patch attached.

    __cj’s picture

    Status: Needs work » Needs review

    The last submitted patch, 7: default_sort_order.patch, failed testing.

    The last submitted patch, default_sort_order.patch, failed testing.

    DeFr’s picture

    Status: Needs review » Needs work

    This is missing some change to SolrBaseQuery::getSolrsortUrlQuery, to remove the hardcoded assumption of score being the default sort too.

    Tentatively setting to NW to be able to RTBC the patch afterwards ;-)

    a.sotirov’s picture

    StatusFileSize
    new2.64 KB

    I just have made a new version of the patch for 7.x-1.12 which is basically the latest stable one.