Dealing with localized fields in custom schemas

Created on 17 May 2023, over 1 year ago
Updated 16 June 2023, over 1 year ago

Setup

  • Solr version: 8.11.2
  • Drupal Core version: 9.5.8
  • Search API version: 8.x-1.29
  • Search API Solr version: 4.2.10
  • Configured Solr Connector: Standard

Support request

Background: I use a java API to index data from a mssql db to index solr documents in a a custom solr core. I then use this solr core to display data and create faceted searches on drupal. I have done this in Drupal 7, using the Sarnia module and now am I migrating this to D9.

Each solr document contain data in English and French. There is one field per language, for example:

i18n_citation_en
i18n_citation_fr

I use views to display the data. In D7, I was only loading the english verion of the field in the view, then I was altering the value by loading the proper language based on the current website locale. In D9, it seems like I don't have access to the full Solr document anymore the views variables.

What would be the best approach?

I was thinking this could be done when I add fields when configuring the index. For example, when the solr fields are being mapped, I could detect if the field name start by "i18n". If so, then keep all the attributes, and display one instance of the field (i.e.: i18n_citation). I could then pick this field in the view and create a plugin to load the proper value. Is this possible to do? From a views field plugin, could I have access the the whole Solr document and load the correct value based on the language? I have not found a way to do it yet.

I do not want to rebuild my API and solr schema to index documents in separate languages for many reasons, but one of them is that the client wants to be able to toggle between French and English at anytime, having the 2 languages in one solr document makes this easier. Also, many fields are not language dependent (like term ids).

Any guidance would be greatly appreciated.

For reference, here is the link of what I am trying to recreate in D9.
Search page: https://www.gg.ca/en/heraldry/public-register
Details page: https://www.gg.ca/en/heraldry/public-register/project/3444

All the data that is displayed there comes from Solr. All the terms from the backend system are migrated using the migrate module and the tIDs are mapped at indexing time from the Java API, by looking up the migrate map tables. I have custom module that do the TID to term name for facets and views from my coming from my sarnia solr index.

💬 Support request
Status

Fixed

Version

4.2

Component

Code

Created by

🇨🇦Canada Menthos

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

Comments & Activities

  • Issue created by @Menthos
  • 🇩🇪Germany mkalkbrenner 🇩🇪

    "Sarnia" has been merged into this module. You get full access to all fields in views using the "Solr Document Datasource".

  • 🇨🇦Canada Menthos

    So i found the solr document data in the $view array, now I am trying to figure out how to alter my view. When I alter the view, its not getting updated.

    I can get all the values of the source solr document, with all the fields.
    I can manipulate the values in the $view object in _hook_views_post_execute in $view->result[0]->_item
    The changes are not reflected in the actual data displayed.

    Anyone has any idea if there is a hook I can work with from that? Maybe the hook i am using is too late, i.e. the views fields are already populated from the item element, so even if i make changes there, they are not reflected?
    Code snippet:

    function osgg_search_api_extras_views_post_execute($view){
    
      $view_fields_values=&$view->result[0];
      $view_fields_definition=$view->result[0]->_item->getFields();
      $solr_original_document=$view->result[0]->_item->getOriginalObject();
      $original_fields=$solr_original_document->getValue()->getFields();
    
     foreach($view_fields_definition as $key=>$value){
    
      if($key=="i18n_citation_en"){
        //replace with french value
        $field=$view->result[0]->_item->getField($key);
        $field->setValues($original_fields["i18n_citation_fr"]->getValues());
      }
     }
    
    }
    
  • Status changed to Fixed over 1 year ago
  • 🇩🇪Germany mkalkbrenner 🇩🇪

    It might be easier to create a new feature for the Solr Document Datasource. Feel free to provide a patch.

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

Production build 0.71.5 2024