You are here

protected function SearchApiSolrMultilingualBackend::preQuery in Apache Solr Multilingual 8

Modify the query before it is sent to solr.

Replace all language unspecific fulltext query fields by language specific ones and add a language filter if required.

Parameters

\Solarium\QueryType\Select\Query\Query $solarium_query: The Solarium select query object.

\Drupal\search_api\Query\QueryInterface $query: The \Drupal\search_api\Query\Query object representing the executed search query.

File

src/Plugin/search_api/backend/SearchApiSolrMultilingualBackend.php, line 54
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 preQuery(Query $solarium_query, QueryInterface $query) {
  parent::preQuery($solarium_query, $query);
  $language_ids = $this
    ->getLanguageIdFiltersFromQuery($solarium_query, $query, TRUE);

  // @todo the configuration doesn't exist yet.
  $this->configuration['asm_limit_search_to_content_language'] = TRUE;
  if (empty($language_ids)) {
    if ($this->configuration['asm_limit_search_to_content_language']) {
      $language_ids[] = \Drupal::languageManager()
        ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
        ->getId();
    }
    else {
      foreach (\Drupal::languageManager()
        ->getLangauges() as $language) {
        $language_ids[] = $language
          ->getId();
      }
    }
  }
  if (!empty($language_ids)) {
    $edismax = $solarium_query
      ->getEDisMax();
    $query_fields = $edismax
      ->getQueryFields();
    $index = $query
      ->getIndex();
    $fulltext_fields = $index
      ->getFulltextFields();
    $multiple_field_names = $this
      ->getFieldNames($index);
    $single_field_names = $this
      ->getFieldNames($index, TRUE);
    foreach ($fulltext_fields as $fulltext_field) {
      foreach ([
        $single_field_names[$fulltext_field],
        $multiple_field_names[$fulltext_field],
      ] as $field_name) {
        $boost = '';
        if (preg_match('@' . $field_name . '(^[\\d.]?)@', $query_fields, $matches)) {
          $boost = $matches[1];
        }
        $language_specific_fields = [];
        foreach ($language_ids as $language_id) {
          $language_specific_fields[] = SearchApiSolrMultilingualUtility::getLanguageSpecificSolrDynamicFieldNameForSolrDynamicFieldName($field_name, $language_id) . $boost;
        }
        $query_fields = str_replace($field_name, implode(',', $language_specific_fields), $query_fields);
      }
      $edismax
        ->setQueryFields($query_fields);
    }
    $fq = new FilterQuery();
    $fq
      ->setKey('asm_language_filter');
    $fq
      ->setQuery($single_field_names[SEARCH_API_LANGUAGE_FIELD_NAME] . ':("' . implode('" OR "', $language_ids) . '")');
    $solarium_query
      ->addFilterQuery($fq);
  }
}