I have created a module to manage sort types, with the following functionalities: add, edit, enable, disable and delete.

I created it to be a module along side of the apachesolr module, so it might need a few changes to go into the module itself.

For more information you can go to the krimson website where I wrote an article about the module:
http://krimson.be/articles/apache-solr-custom-sorting

It is currently tested with version 6.x-1.0-rc1 and I will test the module with the newer versions apachesolr this week.

Comments

robertdouglass’s picture

Status: Active » Needs review

Is this really needed?

/**
 * Menu wildcard loader.
 */
function apachesolr_custom_sort_load($sort_name) {
  return $sort_name;
}

Perhaps you could post us a diff of the ApacheSolr_Custom_Query and the class in the main module?

Thanks!

stijn.vanden.brande’s picture

Function 'apachesolr_custom_sort_load' is indeed unnecessary.

Here is the diff ( < = original file, > = my file):

2c2
< // $Id: Solr_Base_Query.php,v 1.1.4.40 2009/07/02 08:50:46 robertDouglass Exp $
---
> // $Id$
4c4
< class ApacheSolr_Custom_Query implements Drupal_Solr_Query_Interface {
---
> class Solr_Base_Query implements Drupal_Solr_Query_Interface {
265,267c265
<   /*
<    * SVB: changed from orignal file
<    */
---
> 
269,278c267
<     $sorts = array();
<     
<     foreach( $this->available_sorts as $key => $sort ) {
<       if( $sort['status'] ) {
<         $sorts[$key]['title'] = $sort['title'];
<         $sorts[$key]['default'] = $sort['default'];
<       }
<     }
<     
<     return $sorts;
---
>     return $this->available_sorts;
284,287d272
<     $this->available_sorts[$name]['standard'] = FALSE;
<     $this->available_sorts[$name]['status'] = TRUE;
<     
<     variable_set('apachesolr_custom_sort_types', $this->available_sorts);
296,297d280
<    * 
<    *  SVB: changed from orignal file
301c284,290
<     return variable_get('apachesolr_custom_sort_types', array());
---
>     return array(
>       'score' => array('title' => t('Relevancy'), 'default' => 'asc'),
>       'sort_title' => array('title' => t('Title'), 'default' => 'asc'),
>       'type' => array('title' => t('Type'), 'default' => 'asc'),
>       'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
>       'created' => array('title' => t('Date'), 'default' => 'desc'),
>     );
avpaderno’s picture

Maybe what robertDouglass was asking is a patch file that would show the differences between the actual file implementing the main class, and the file implementing your class.

stijn.vanden.brande’s picture

StatusFileSize
new1.47 KB

This is the patch file.

pwolanin’s picture

Version: 6.x-1.0-rc1 » 6.x-1.x-dev
Status: Needs review » Needs work

This looks like incorrect use of variable_set and would mean a default install has no sorts.

stijn.vanden.brande’s picture

Yes, indeed. I was using an install file at first.

Adjusted the default_sorts function to this:

protected function default_sorts() {
    // The array keys must always be real Solr index fields.
    $defaults = array(
      'score' => array('title' => t('Relevancy'), 'default' => 'asc'),
      'sort_title' => array('title' => t('Title'), 'default' => 'asc'),
      'type' => array('title' => t('Type'), 'default' => 'asc'),
      'sort_name' => array('title' => t('Author'), 'default' => 'asc'),
      'created' => array('title' => t('Date'), 'default' => 'desc'),
    );
    
    $sorts = variable_get('apachesolr_custom_sort_types', $defaults);
    
    return (empty($sorts) ? $defaults : $sorts);
}

Think this fixes it.

Sorry for the late response.

Will post the new patch later today

stijn.vanden.brande’s picture

StatusFileSize
new4.1 KB

The new patch file

simonseeks’s picture

StatusFileSize
new10.24 KB

Hi stijn,

We really liked your module and the ideas in it. We're in the process of implementing quite a significant solr search engine on simonseeks.com. We've found the configuration of sort options still a bit limited. I hope you don't mind but we've taken the core ideas and approach in your module and extended them quite a bit.

The main difference is that we've introduced the concepts of "sort groups" which is a named collection of fields that we can choose to show on any given page. Example usage would be:

  • url is simonseeks.com/accommodation/* - show the "price" and "star rating" fields in the sort block
  • url is simonseeks.com/travel-guides/* - show the "community rating" and "number of comments" fields in the sort block

Usage is fairly straightforward (like you we've used the "Apache Solr Core: Sorting" block so this needs to be assigned to a region):

  • Install module.
  • Select "Apache Solr Custom Sort Groups" from the admin menu
  • Click the "New Sort Group" tab - then fill in the following fields:
  • Group name field - is arbitrary text you use to identify the group of fields.
  • Name field - this is the text that will be displayed in the sort block.
  • Field name - is the solr field to sort on.
  • Initial Sort order - I'm sure you can guess.
  • Visibility Settings - these work exactly the same way as block settings.

If you then save the settings and navigate to the page that matches your visibility settings you should hopefully see the fields you configured for the sort group.

I'm really interested to hear what you (or anyone else) thinks of this approach. It would be great if we could get this type of functionality into this module or maybe even the core solr modules. I really don't like having to duplicate loads of content from "Solr_Base_Query.php".

The module seems to be working well, I've been on a journey (!) with the ahah stuff so there may be a few quirks in there but I think the fundamentals are sound!

Paul.

pwolanin’s picture

Note -the we have committed to keeping the 6.x-1.x branch pretty stable and not take major new features.

simonseeks’s picture

That's a shame.

How would we go about trying to get this type of functionality into another branch?

Paul.

sped2773’s picture

Hi Paul I installed your module against 6.x-2.0 alpha2 and it works well and was the exact functionality I was looking for as the sort options is pretty inflexible as it stands.

pwolanin maybe this is something that can go in the 6.x-2.x-dev branch as a contrib?

stijn.vanden.brande’s picture

Hi Paul,

great idea.
I will test it and have a look at the code as soon as possible.

Stijn

simonseeks’s picture

Hi Stijn,

That's great news - thanks for getting back to me. The module seems to be holding up fairly well internally - so far we've identified one small bug with the ahah drag/drop ordering not being honoured when a group is saved.

Look forward to hearing from you.

Thanks,
Paul

simonseeks’s picture

If anyone's interested we've now rolled out the first phase of our solr implementation using this custom sort module. You can see an example on our London hotel and travel guide pages here.

Paul.

robertdouglass’s picture

Great stuff. I'm going to make sure and review this for inclusion in the 6.x-2.x branch.

idontknowtheanswer’s picture

StatusFileSize
new10.33 KB

Hi Robert,

That's good to hear - really useful to get this kind of functionality into core. I've attached our latest version which fixes some of the issues I mentioned in a previous post about sort ordering not working using AHAH. I've tried to model the AHAH implementation on Quicktabs - it's working but I'm not 100% confident it's the purest implementation.

If you need any further information or help please let me know.

Paul.

robertdouglass’s picture

Version: 6.x-1.x-dev » 6.x-2.x-dev

changing version number. Have you tested this latest with 6.x.2.x?

simonseeks’s picture

(sorry for posting under two accounts - trying to move over to the simonseeks one permanently)

Yes - it's currently developed against 6.x-2.0-beta1.

dpalmer’s picture

Hi Paul,

this module is exactly what I needed, unfortunately, I downloaded and installed your latest zip that you posted last night, enabled the module, but I don't see any menu options for it under site configuration. I even when to the direct url that is provided in the module, admin/settings/apachesolr_sort_group/ and it just redirects me to the site configuration page. Any suggestions?

Thanks,

dpalmer

simonseeks’s picture

Hi dpalmer,

Sorry for the slow reply - we've had a public holiday in the UK.

I've tried doing a full uninstall/reinstall here and can't replicate the issue. It's been setup internally by a few other people without any issue.

The module requires permissions for "administer search" - it might be worth checking your user has those rights. The module also requires the apachesolr module - you shouldn't have been able to install it without having apachesolr installed.

Hope that's of some help,
Paul.

dpalmer’s picture

I'm logged in as a site admin and I have that permission. I have also used devel to switch to the super user so at that point permissions are no longer an issue. I also have apachesolr installed. I'm trying to set this up on a very large drupal site so maybe there's some conflicting modules.

I just set up a sandbox site with apachesolr and your custom sort module. It worked fine with no issues.

My sandbox site is running Drupal 6.16, Apache Solr version DRUPAL-6--2-0-BETA1, and the most recent custom sort module that you provided on this post.

The actual site that I need apachesolr & sort types on is running Drupal 6.15 with DRUPAL-6--2-0 BETA1 and it doesn't work. We can't update this site to Drupal 6.16 because some of modules we use are currently incompatible with Drupal 6.16.

Any suggestions?

simonseeks’s picture

Nothing springs to mind. There's nothing in the custom sort module that has a dependency on anything in Drupal 6.16 AFAIK - we're not running the very latest version. Are there any errors in the drupal watchdog and/or the Apache log?

Had you previously installed the initial version of this module? If so did you fully uninstall the module using example.com/admin/build/modules/uninstall? The module does set some custom variables which aren't removed unless you do a full uninstall - it may be that these are conflicting somehow. The version I've posted follows this pattern as well.

It may be worth doing a full uninstall of the later version, install the initial version from stijn, do a full uninstall of that version and then finally reinstall the last version. That should make sure the variables table is correctly synced up.

Let me know how you get on.
Paul.

stijn.vanden.brande’s picture

Hi Paul,

first of all sorry for the late response, it's been kind of hectic.
I've tested the module and everything worked, except for the ahah bug you mentioned earlier.
But I am thinking that putting all that data in one variable is kind of going overboard, so maybe this qualifies for a separate table.
What do you think?

Greets,
Stijn

simonseeks’s picture

Hi Stijn,

I agree - it does seem to be a lot of information to store in one variable. A schema similar to that of the blocks table would seem to be a good way forward.

Probably one for discussion with Robert if this kind of functionality is to be included in the 6.x-2.x branch.

Paul.

robertdouglass’s picture

Reviewing the code:

In the ApacheSolr_Custom_Query you have a regression:

-    $queryvalues['filters'] = isset($queryvalues['filters']) ? trim($queryvalues['filters']) : '';
+    if (isset($queryvalues['filters'])) {
+      $queryvalues['filters'] = trim($queryvalues['filters']);
+    }

Also, I don't understand why you recreate the whole class:

class ApacheSolr_Custom_Query implements Drupal_Solr_Query_Interface {

Why not:

class ApacheSolr_Custom_Query extends Solr_Base_Query {

We're also going to have to give some thought about how to make this class loading more extensible, because for cases like Acquia Search we need to extend the Acquia provided class. I'd assume this to be incompatible with Acquia Search the way it is now.

Here's the current class loading code:

function apachesolr_drupal_query($keys = '', $filters = '', $solrsort = '', $base_path = '', $solr = NULL) {
  list($module, $class) = variable_get('apachesolr_query_class', array('apachesolr', 'Solr_Base_Query'));
  include_once drupal_get_path('module', $module) .'/'. $class .'.php';

I think we won't be able to avoid building this system out some more before we succeed in making the right class hierarchy.

Please use sentence case in titles. First words and "Apache Solr" are capitalized, subsequent words are lowercase.

/**
* Implementation of hook menu.
* 
*/
function apachesolr_custom_sort_menu() {
  $items['admin/settings/apachesolr_sort_groups'] = array(
    'title' => 'Apache Solr Custom Sort Groups',
  );
  
  $items['admin/settings/apachesolr_sort_group/add'] = array(
    'title' => 'New Sort Group',
  
  $items['admin/settings/apachesolr_sort_group/%apachesolr_sort_group/delete'] = array(
    'title' => 'Delete sort group', // this one is correct!
  );

Same goes for "Group name":

/**
* Provides an list of the configured sort groups.
* 
*/
function apachesolr_custom_sort_apachesolr_sort_group_overview() {
  
  $settings_list = variable_get('sort_field_settings', array());
  
  $header = array(t('Group Name'), t('Fields'), t('Path'), array('data' => t('Operations'), 'colspan' => '3'));

function apachesolr_custom_sort_apachesolr_sort_group_form($form_state, $group_settings=NULL) {

  $form['group-name'] = array(  
                      '#title' => t('Group Name'),

  $form['sort-group-fields'] = array(
    '#title' => t('Selected Fields For Sorting'),

 
  $form['sort-group-fields']['add_sort'] = array(
    '#value' => t('Add Sort Field'),


function _get_sort_widget($delta, $display_name='', $field_name='', $initial_sort_order_order='asc') {                  
  $widget['sort_order'] = array(
                            '#title' => t('Initial Sort Order'),

Run the coder module once. There are several instances of missing whitespace:

function apachesolr_custom_sort_apachesolr_sort_group_form($form_state, $group_settings=NULL) {
  elseif ($group_settings==NULL) {

  // should be 2x:
  $group_settings = NULL
dpalmer’s picture

Paul,

Thanks for your help. Turns out the sorting was working relatively fine the whole time, just not on an Apache Solr View page. It seems like this custom sort module (as well as apache solr core sorting block) is incompatible with an apache solr view. I've decided to just use the standard solr search page and the sorting works great.

Thanks,
Donovan

simonseeks’s picture

Thanks for the review Robert. I'll look at your feedback. Not sure why ApacheSolr_Custom_Query implements Drupal_Solr_Query_Interface as opposed to extending it, that's something that carried over from the original implementation - maybe Stijn had something in mind when he did it...

Do you have a view on how to implement sorting for non standard fields that shouldn't be shown in the sort block? We have a requirement to implement a sort on a custom weighting field for search results. It makes no sense to show the sort field to the end user but the current implementation of sorting - in this module and the default sort - requires the sort fields to be assigned to the query objects $available_sorts variable. The value of $available_sorts seems to serve a dual purpose: it's used to populate the sort field block and also enable sorting via the querystring for the specific sort field.

To work round this we've extended the module to add an "exposed" flag to a field within a sort group. This allows us to assign the field to the query objects $available_sorts but stops the field being rendered in the sort block (we remove fields that aren't exposed via hook_sort_links_alter). It works but we're jumping through a few hoops to get it working.

Paul.

simonseeks’s picture

Hi Donovan,

Glad it's working for you. We'd love to be using Views with Solr but it's a bit bleeding edge even for us!

Paul.

robertdouglass’s picture

I was thinking about this and think that the changes to the query class should be made to the main class. Understandably so far you've developed as a standalone module, but I think making the sorting stuff more generic makes perfect sense, and your class seems to be a step in the right direction. Plus you can drop all of the class loading code you've got. So in the next revision, please exclude the custom class and submit a patch to the main class already in the module.

I think an exposed flag on sorts is a fine extension to that model.

stijn.vanden.brande’s picture

Hi,

I was using implements instead of extends, because I copied the file and made as few changed as possible.
I'm gonna have a look at full code to the overview again.
@Robert: perhaps you have a little bit of time at drupalcon to talk about how we are going to continue this the best?

Greets, Stijn

jpmckinney’s picture

Title: Apache Solr sorting » UI to add, edit, enable, disable and delete Apache Solr sorts
simonseeks’s picture

StatusFileSize
new10.64 KB
new7.81 KB

Hi All,

Sorry for the long delay - work is very hectic.

Attached is the patch file which is against Drupal_Apache_Solr_Service.php, 6.x-2.0-beta1. I've used winmerge to create it - I hope it's ok.

I've also attached the latest version of the full module which implements the expose functionality described above. This allows us to be able to sort on fields without having to actually display them to the user. If you install this version over the previous version you'll have to edit and re-save your existing sort groups to ensure the "expose" field is added to your existing settings. I apologise in advance for this slight crustiness.

Paul.

jpmckinney’s picture

Status: Needs work » Needs review
mirzu’s picture

just tried this out and got the following error on a page with an mlt block:

Fatal error: Class ApacheSolr_Custom_Query contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Drupal_Solr_Query_Interface::remove_keys) in /var/www/sites/all/modules/apachesolr_custom_sort/ApacheSolr_Custom_Query.php on line 474
pwolanin’s picture

Status: Needs review » Needs work
surgeonbor’s picture

subscribe

adam_b’s picture

subscribe

3dloco’s picture

With latest 6.x-2.x, I get same error as in #34 but on every search page...and I am not using apachesolr_views

dannyhadar’s picture

Thanks for the important module!

got this error:

Fatal error: Class ApacheSolr_Custom_Query contains 3 abstract methods and must therefore be declared abstract or implement the remaining methods (Drupal_Solr_Query_Interface::get_keys, Drupal_Solr_Query_Interface::set_keys, Drupal_Solr_Query_Interface::remove_keys) in /var/www/sites/newpan.lxst.codeoasis.com/modules/contrib/apachesolr_custom_sort/ApacheSolr_Custom_Query.php on line 452

drupalfmm’s picture

Hi, nice module. Any solution for error '3 abstract methods'?

drupalfmm’s picture

  /**
   * redeclaring methods in the class ApacheSolr_Custom_Query
   */
  public function get_keys() {}

  public function set_keys($keys) {}
  
  public function remove_keys(){}
jpmckinney’s picture

Version: 6.x-2.x-dev » 7.x-1.x-dev

Why are the patches zip files?

wouters_f’s picture

Maybe it could be usefull to create the apachesolr_sort module which extends the sorting possibilities.

I have started a sandbox --not knowing this stuff was here-- with some custom apachesolr sorting features.
(weights for and disabling sort fields)

http://drupal.org/sandbox/wouters_frederik/1124756
The sandbox module is for drupal 6.

trothwell’s picture

Great idea I'm defiantly going to look into this one. Doesn't look like it's remaining active within development though.

Although i'm getting the same issue in #34.

geerlingguy’s picture

Subscribe.

rayvan’s picture

subscribe

nick_vh’s picture

I ported the sandbox of drupal_sensei to D7 for those who are interested
#1195046: apachesolr_sort drupal 7 version

It's not nearly as advanced as the one from stijn.vanden.brande but it is limited in functionality and makes more chance to end up (eventually) in a branch of solr or as separate module + you need this patch http://drupal.org/node/1324842

nick_vh’s picture

Status: Needs work » Fixed

Closing this one in favor or apachesolr_sort module
http://drupal.org/project/apachesolr_sort

Please help out with the issues in that module if you want to push this effort forward. I've moved all the effort that has been done so far into an issue in that module so closing this one. Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

lucasvm’s picture

I have installed this module and now my site is broken im getting this error:

Fatal error: Class 'ApacheSolr_Custom_Query' not found in apachesolr.module on line 1450

i have deleted the module but still i get this, what can i do?

nick_vh’s picture

Issues for http://drupal.org/project/apachesolr_sort should go in the issue queue of that module