Hello and thanks a lot for this great module.

I have to create new facets to filter content from many sites, Drupal and non-Drupal, stored in the same SORL index. The indexation part is working, and the results conform to what's expected (search fields, sensible results, existing facets are OK, etc.)

Examining the code of the module, I don't understand how I can hook in it to add my own facets.

In fact, I don't really understand why the facet management is separated from the regular monosite search of the apachesolr module.

Could you please enlighten me and point me in the right direction ?

Comments

Countzero’s picture

Well, I made some progress. Sort of.

I modified the file apachesolr_multisitesearch.admin.inc to include this :

  foreach (module_implements('apachesolr_multisitesearch_facets') as $module) {
    $module_facets[$module] = module_invoke($module, 'apachesolr_multisitesearch_facets');
    $module_list[$module] = $module;
  }

in function apachesolr_multisitesearch_enabled_facets_form().

I managed to add a facet to the admin screen at admin/settings/apachesolr/multisite-filters by implementing the new hook in my module :

function aedilis_solr_apachesolr_multisitesearch_facets($rebuild = FALSE) {
  $facets = array();
  $facets['nomserie'] = array(
    'info' => t('Aedilis search: Filter by Série'),
    'facet_field' => 'nomserie',
  );
    return $facets;
}

My facet appears in a frame named after my module name, which is fine.

Then I tried to implement hook_block to manage the block which should match my facet (I pasted and modified a bit from the base module) :

function aedilis_solr_block($op = 'list', $delta = 0, $edit = array()) {
    switch ($op) {
        case 'list':
              $enabled_facets = apachesolr_multisitesearch_enabled_facets('aedilis_solr');
              $facets = aedilis_solr_apachesolr_multisitesearch_facets();

              // Add the blocks
              $blocks = array();
              foreach ($enabled_facets as $delta => $facet_field) {
                if (isset($facets[$delta])) {
                  $blocks[$delta] = $facets[$delta] + array('cache' => BLOCK_CACHE_PER_PAGE,);
                }
              }
            return $blocks;
            break;        
            
          case 'view' :
            if (apachesolr_multisitesearch_has_searched()) {
                // Get the query and response. Without these no blocks make sense.
                $response = apachesolr_static_response_cache();
                if (empty($response)) {
                    return;
                }
                $query = apachesolr_current_query();
                if ($delta == 'nomserie') {
                    return apachesolr_facet_block($response, $query, 'apachesolr_multisitesearch', $delta, $delta, t('Filter by série'));            
                }
            }
                break;
            case 'configure':
              if ($delta != 'currentsearch') {
                return apachesolr_facetcount_form('apachesolr_multisitesearch', $delta);
              }
            case 'save':
              if ($delta != 'currentsearch') {
                apachesolr_facetcount_save($edit);
              }
                break;
    } 
}

... and my block appears in admin/build/block.

It also appears in the search results, but as for now, the result count doesn't make sense (251 results out of 8). I guess it's because I didn't implement _add_facet_params yet.

So it's my next step. I'll keep this thread updated as I go, but any help would speed up my efforts and would be appreciated.

Thanks for reading and see you soon.

Countzero’s picture

In fact, the inconsistency came from a faulty index.

So my facet works except that a click on any item in it results in no result whatever the value.

Also, querying directly in SOLR with :

solr/select/?q=cartulaire&fl=*,score&sort=site desc&facet=true&facet.field=nomserie 

... seems to return the expected results, so I'm pretty sure my code is to blame.

I'm stuck.

Countzero’s picture

OK, got it working.

I implemented a function to handle my facet, and use a numeric value to filter the results, and it seems to work (I got inspired by, and effectively copied the code from, this http://drupal.org/files/issues/apachebook_0.patch) :

I changed this line in the hook_block above :

return apachesolr_facet_block($response, $query, 'apachesolr_multisitesearch', $delta, $delta, t('Filter by série')); 

to :

return apachesolr_facet_block($response, $query, 'aedilis_solr', $delta, $delta, t('Filter by série'), 'aedilis_solr_serie');            

and of course coded - copied - this function :

function aedilis_solr_serie($facet) {
    if ($facet == 0) {
        return 'Pas de série';   
    }
   $sql = 'SELECT title FROM {node} WHERE nid = %d';
   return db_result(db_query($sql, $facet));
}

... and tadaaaa ! Everything's fine.

My mistake was to think there was some kind of automatic handling of the values, which is not the case, at least in this precise use case.

Now, on the road again to code my other facets.

Countzero’s picture

One bug remains : the unclick link for the facets show the id and not the text. Don't see how I can fix this as for now.

As usual (but is anybody reading this ?), any help would be appreciated.

Countzero’s picture

Solved.

I had to implement the theme functions for my facets :

function aedilis_solr_theme() {
  return array(
    'apachesolr_breadcrumb_nomserie' => array(
      'arguments' => array('sid' => NULL, 'exclude' => FALSE),
    ),
    'apachesolr_breadcrumb_intervenant' => array(
      'arguments' => array('aid' => NULL, 'exclude' => FALSE),
    ),
    );
}
function theme_apachesolr_breadcrumb_nomserie($sid) {
  if (is_numeric($sid)) {
    return db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $sid));
  }
  else {
    return 'Aucune série';
  }
}
function theme_apachesolr_breadcrumb_intervenant($aid) {
  if (is_numeric($aid)) {
    return db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $aid));
  }
  else {
    return 'Aucun auteur';
  }
}

I had to use the namespace of the original apachesolr module, because of this in the apachesolr_multisitesearch_currentsearch function :

        $fielddisplay = theme("apachesolr_breadcrumb_". $field['#name'], $field['#value']);

If I make a patch for the module, I think I will try to enhance it to allow modules to namespace their theme functions.

Countzero’s picture

I had a lot of trouble because of a tweaked schema.xml. Also, keep in mind that reindexing is not enough to take changes to schema.xml into account.

YOU HAVE TO RELOAD TOMCAT TO LOAD A NEW VERSION OF SCHEMA.XML.

It would have saved me a few hours of stress if I had known this before.

My work with this module is almost finished. I don't know if a handbook summarizing what I learned in the process would be of interest ? Given the numerous reactions to my posts since last week, I find myself doubting ...

pwolanin’s picture

@Countzero - any addition to the documentation is appreciated.

I just added a documentation stub - please add child pages here: http://drupal.org/node/828468

Countzero’s picture

Noted.

I'll try to write something as clear as possible before the end of the week.

Countzero’s picture

Status: Needs review » Active

Here's the page : http://drupal.org/node/829148.

Hope I didn't left too many mistakes.

Countzero’s picture

Status: Active » Needs review
csevb10’s picture

Status: Active » Needs review
Issue tags: +drupal.org redesign, +drupal.org redesign sprint 3, +drupal.org redesign search
StatusFileSize
new1.94 KB

I've actually created the patch for this because I feel like the ability to add your own facets should exist without having to patch the file yourself.
This creates an apachesolr_multisitesearch_facets() hook so that any module can implement multisitesearch facets. It also simplifies the code logic to remove the hard-coded case for apachesolr_multisitesearch module and makes the invocation in apachesolr_multisitesearch an implementation of hook_apachesolr_multisitesearch_facets().

pwolanin’s picture

StatusFileSize
new3.14 KB

I find calling your own hook a bit ugly, but maybe it's the easiest approach.

previous patch misses a function call in hook_bock that needs to change. Also, I'm not sure function _apachesolr_sort_facets would exist in memory. In any case it's a private function that should not be called by another module.

pwolanin’s picture

Status: Needs review » Fixed

committed to HEAD after a little local testing.

Status: Fixed » Closed (fixed)
Issue tags: -drupal.org redesign, -drupal.org redesign sprint 3, -drupal.org redesign search

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