Cross-language search return result in interface language

Created on 3 February 2022, almost 3 years ago
Updated 17 January 2023, almost 2 years ago

Doing a cross-language search but get the result displayed in the interface language

Setup

  • Solr version: 4.2
  • Drupal Core version: 9.1.9
  • Search API version: 1.19
  • Search API Solr version: 4.2

Issue

In a project, we have the requirement that users enter a search term in any language and then have matching results displayed in the selected interface language.

Concrete example:
1) We assume there is a record, e.g. a book that has the title "African Artworks" tagged with the keyword "Africa".
2) We further assume that this record is translated, e.g. in french, with the title "Oeuvres Africaines" and the keyword "Afrique".
3) We assume that a user has set the interface language to "english" and is now searching for the term "Afrique".
Desired behavior:
4.1) The record from 1) is returned. It is found due to the fact that "Afrique" is the translation of "Africa".
Furthermore, the user is shown the record in the english version (not french!), since this is the current interface language, i.e. the result contains the book with title "African Artworks" and keyword "Africa".
Current behavior:
4.2.) The search returns an empty result set, because no matches were found in the records for the currently set interface language (english).

We realized the desired behaviour (4.1.) by modifying the function "addFieldValues()" in the RenderedItem.php (search_api/src/Plugin/search_api/processor) as follows:

  public function addFieldValues(ItemInterface $item) {
  [...]
      $datasource_id = $item->getDatasourceId();
      $datasource = $item->getDatasource();
      $bundle = $datasource->getItemBundle($item->getOriginalObject());
      // When no view mode has been set for the bundle, or it has been set to
      // "Don't include the rendered item", skip this item.
      if (empty($configuration['view_mode'][$datasource_id][$bundle])) {
        // If it was really not set, also notify the user through the log.
        if (!isset($configuration['view_mode'][$datasource_id][$bundle])) {
          $unset_view_modes[$field->getFieldIdentifier()] = $field->getLabel();
        }
        continue;
      }
      $view_mode = (string) $configuration['view_mode'][$datasource_id][$bundle];

      <strong>// from here, the suggested modification begins
      try {
        // we consider only two languages in our scenario; TODO: the available languages have to be identified automatically
        $languages = ['en', 'fr'];
        $value = "";
        foreach($languages as $language){
        $build = $datasource->viewItem($item->getOriginalObject(), $view_mode, $language);
        if ($build) {
          // Add the excerpt to the render array to allow adding it to view modes.
          $build['#search_api_excerpt'] = $item->getExcerpt();
          $value .= (string) $this->getRenderer()->renderPlain($build);
        
        }
        if (!empty($value)) {
          $field->addValue($value);
        }
      }
    }</strong>
      catch (\Exception $e) {[...]}
[...]
}

Is there another way to implement my requirement? Or in case it is already implemented somehow: Which configuration do I miss in the module?

Thank you so much in advance!

💬 Support request
Status

Active

Version

1.0

Component

General code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024