You are here

protected function AbstractSearchApiSolrMultilingualBackend::alterSolrDocuments in Search API Multilingual Solr Search 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/AbstractSearchApiSolrMultilingualBackend.php, line 397

Class

AbstractSearchApiSolrMultilingualBackend
A abstract base class for all multilingual Solr Search API backends.

Namespace

Drupal\search_api_solr_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
    ->getSolrFieldNames($index);
  $field_names = $this
    ->getSolrFieldNames($index, TRUE);
  $fulltext_field_names = array_filter(array_flip($multiple_field_names) + array_flip($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[$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 = Utility::getLanguageSpecificSolrDynamicFieldNameForSolrDynamicFieldName($monolingual_solr_field_name, $language_id);
        $field_name_map_per_language[$language_id][$monolingual_solr_field_name] = SearchApiSolrUtility::encodeSolrName($multilingual_solr_field_name);
      }
    }
  }
  foreach ($field_name_map_per_language as $language_id => $map) {
    $solr_field_type_name = SearchApiSolrUtility::encodeSolrName('text' . '_' . $language_id);
    if (!$this
      ->isPartOfSchema('fieldTypes', $solr_field_type_name) && !$this
      ->createSolrMultilingualFieldType($solr_field_type_name) && !$this
      ->hasLanguageUndefinedFallback()) {
      throw new SearchApiSolrMultilingualException('Missing field type ' . $solr_field_type_name . ' in schema.');
    }

    // Handle dynamic fields for multilingual tm and ts.
    foreach ([
      'ts',
      'tm',
    ] as $prefix) {
      $multilingual_solr_field_name = SearchApiSolrUtility::encodeSolrName(Utility::getLanguageSpecificSolrDynamicFieldPrefix($prefix, $language_id)) . '*';
      if (!$this
        ->isPartOfSchema('dynamicFields', $multilingual_solr_field_name) && !$this
        ->createSolrDynamicField($multilingual_solr_field_name, $solr_field_type_name) && !$this
        ->hasLanguageUndefinedFallback()) {
        throw new SearchApiSolrMultilingualException('Missing dynamic field ' . $multilingual_solr_field_name . ' in schema.');
      }
    }
  }
  foreach ($documents as $document) {
    $fields = $document
      ->getFields();
    foreach ($field_name_map_per_language as $language_id => $map) {
      if ($fields[$field_names[SEARCH_API_LANGUAGE_FIELD_NAME]] == $language_id) {
        foreach ($fields as $monolingual_solr_field_name => $value) {
          if (isset($map[$monolingual_solr_field_name])) {
            $document
              ->addField($map[$monolingual_solr_field_name], $value, $document
              ->getFieldBoost($monolingual_solr_field_name));

            // @todo removal should be configurable
            $document
              ->removeField($monolingual_solr_field_name);
          }
        }
      }
    }
  }
}