We want to show the language neutral nodes regardless of the language filter (we have three languages). I tried this approach:

hook_apachesolr_update_index():
if(empty($node->language)) {
$document->language = $node->language = 'neutral';
}

and in hook_apachesolr_modify_query():
$query->add_filter('language', 'neutral');

This results in an empty resultset.

Any help is appreciated.

Comments

mkalkbrenner’s picture

That doesn't work because there's no language 'neutral' in the multilingual index.

Apache Solr Multilingual offers a feature to map language neutral content to an existing language. Maybe you can activate a fourth language.

Or implement some hooks to index the neutral node three times using three different languages.

jessehs’s picture

Status: Active » Needs review

I used the following code in a custom module's hook_apachesolr_modify_query to achieve this:

<?php
    $language_filters = $query->get_filters('language');
    // Remove the language filter as we're going to add an OR language filter.
    $query->remove_filter('language');
    // Add the OR language filter in a separate query.
    $query2 = clone $query;
    $query2->add_filter('language', $language_filters[0]['#value']);
    // Show language neutral results as well as the current language.
    $query2->add_filter('language', 'und');
    // Merge the language filter into the original query using OR.
    $query->add_subquery($query2, 'OR');
?>

Note that I'm using the 6.x-1.0-beta3 version of the apachesolr_multilingual, and the 6.x-1.5 version of the apachesolr module (although this probably works in 2.x too).
*Edit: removed extraneous, unrelated bit of code.

mkalkbrenner’s picture

Title: Always show language neutral nodes » Make occurance of language neutral nodes in search result configurable
Version: 6.x-2.0-beta1 » 7.x-1.x-dev
mkalkbrenner’s picture

Category: support » feature
mkalkbrenner’s picture

Status: Needs review » Active
mkalkbrenner’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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

jessehs’s picture

I revisited my earlier post, and realized it's not quite right. Here's another shot at this problem:

/**
 * Implementation of hook_apachesolr_modify_query().
 */
function my_module_apachesolr_modify_query(&$query, &$params, $caller) {
  $query2 = clone $query;
  // Remove the language-specific filter from the cloned query.
  $query2->remove_filter('language');
  // Add a language neutral filter to the cloned query.
  $query2->add_filter('language', 'und');
  // Merge the language filter into the original query using OR.
  $query->add_subquery($query2, 'OR');
}
jessehs’s picture

Heh, turns out this latest attempt was bad as well. The following is an approach which seems to work:


/**
 * Implementation of hook_apachesolr_modify_query().
 */
function my_module_apachesolr_modify_query(&$query, &$params, $caller) {
  $language_filters = $query->get_filters('language');
  $active_language = $language_filters[0]['#value'];
  $query->remove_filter('language');
  $languages = language_list('language');
  // Unset the currently active language (set by apachesolr_multilingual).
  // This was an INCLUSIVE filter rather than an EXCLUDING filter, so language
  // neutral content was excluded, since only one language was allowed.
  unset($languages[$active_language]);
  // Loop through all values and add EXCLUDING filters to exclude them.
  foreach ($languages as $langcode => $lang) {
    $query->add_filter('language', $langcode, TRUE);
  }
}
paul_gregory’s picture

Thanks Jesse, you're a life saver! We had been using your earlier code (#2) but we discovered it wasn't working properly. When I came back to this issue I was pleased to see you'd updated your patch :)

We're now using the code from #9 and it seems to be working brilliantly.

  • Commit cc3859a on 7.x-1.x, 6.x-3.x by mkalkbrenner:
    [#1271680] jessehs, mkalkbrenner: Make occurance of language neutral...