You are here

protected function SearchApiSolrMultilingualBackend::alterSolrDocuments in Apache Solr Multilingual 8

Replaces language unspecific fulltext fields by language specific ones.

Parameters

\Solarium\QueryType\Update\Query\Document\Document[] $documents: An array of \Solarium\QueryType\Update\Query\Document\Document objects ready to be indexed, generated from $items array.

\Drupal\search_api\IndexInterface $index: The search index for which items are being indexed.

array $items: An array of items being indexed.

See also

hook_search_api_solr_documents_alter()

File

src/Plugin/search_api/backend/SearchApiSolrMultilingualBackend.php, line 167
Contains \Drupal\as_search\Plugin\search_api\backend\ASSearchApiSolrBackend.

Class

SearchApiSolrMultilingualBackend
Plugin annotation @SearchApiBackend( id = "search_api_solr_multilingual", label = @Translation("Solr Multilingual"), description = @Translation("Index items using an Apache Solr Multilingual search server.") )

Namespace

Drupal\apachesolr_multilingual\Plugin\search_api\backend

Code

protected function alterSolrDocuments(array &$documents, IndexInterface $index, array $items) {
  parent::alterSolrDocuments($documents, $index, $items);
  $fulltext_fields = $index
    ->getFulltextFields();
  $multiple_field_names = $this
    ->getFieldNames($index);
  $single_field_names = $this
    ->getFieldNames($index, TRUE);
  $fulltext_field_names = array_filter(array_flip($multiple_field_names) + array_flip($single_field_names), function ($value) use ($fulltext_fields) {
    return in_array($value, $fulltext_fields);
  });
  $field_name_map_per_language = [];
  foreach ($documents as $document) {
    $fields = $document
      ->getFields();
    $language_id = $fields[$single_field_names[SEARCH_API_LANGUAGE_FIELD_NAME]];
    foreach ($fields as $monolingual_solr_field_name => $field_value) {
      if (array_key_exists($monolingual_solr_field_name, $fulltext_field_names)) {
        $multilingual_solr_field_name = SearchApiSolrMultilingualUtility::getLanguageSpecificSolrDynamicFieldNameForSolrDynamicFieldName($monolingual_solr_field_name, $language_id);
        $field_name_map_per_language[$language_id][$monolingual_solr_field_name] = $multilingual_solr_field_name;
        $document
          ->addField($multilingual_solr_field_name, $field_value, $document
          ->getFieldBoost($monolingual_solr_field_name));

        // @todo removal should be configurable
        $document
          ->removeField($monolingual_solr_field_name);
      }
    }
  }
  $this
    ->ensureAllMultilingualFieldsExist($field_name_map_per_language, $index);
}