Commit 86d922bd authored by Markus Kalkbrenner's avatar Markus Kalkbrenner Committed by Markus Kalkbrenner
Browse files

Issue #3302986 by mkalkbrenner, damontgomery: Support for fulltext searches in...

Issue #3302986 by mkalkbrenner, damontgomery: Support for fulltext searches in combination with "Language (with fallback)" processor
parent 0536f0d2
Loading
Loading
Loading
Loading
+92 −0
Changes for src/Event/PreAddLanguageFallbackFieldEvent.php: 92 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\search_api_solr\Event;

use Drupal\Component\EventDispatcher\Event;

/**
 * Event to be fired before a value gets indexed as language fallback.
 *
 * @see \Drupal\search_api\Plugin\search_api\processor\LanguageWithFallback
 */
final class PreAddLanguageFallbackFieldEvent extends Event {

  /**
   * The language ID.
   *
   * @var string
   */
  protected $langcode;

  /**
   * The field value.
   *
   * @var mixed
   */
  protected $value;

  /**
   * The field type.
   *
   * @var string
   */
  protected $type;

  /**
   * Constructs a new class instance.
   *
   * @param string $langcode
   *   The language ID.
   * @param mixed $value
   *   The filed value.
   * @param string $type
   *   The field type.
   */
  public function __construct(string $langcode, mixed $value, string $type) {
    $this->langcode = $langcode;
    $this->value = $value;
    $this->type = $type;
  }

  /**
   * Retrieves the language ID.
   *
   * @return string
   *   The language ID.
   */
  public function getLangcode(): string {
    return $this->langcode;
  }

  /**
   * Retrieves the field values.
   *
   * @return mixed
   *   The field values.
   */
  public function getValue(): mixed {
    return $this->value;
  }

  /**
   * Set the field value.
   *
   * @param array $value
   *   The field value. If you supply NULL as the value and no modifier the
   *   field will be removed.
   */
  public function setValue(mixed $value): void {
    $this->value = $value;
  }

  /**
   * Retrieves the field type.
   *
   * @return string
   *   The field type.
   */
  public function getType(): string {
    return $this->type;
  }

}
+9 −0
Changes for src/Event/SearchApiSolrEvents.php: 9 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -121,6 +121,15 @@ final class SearchApiSolrEvents {
   */
  const POST_SET_FACETS = PostSetFacetsEvent::class;

  /**
   * Fired before a value gets indexed as language fallback.
   *
   * @Event
   *
   * @see \Drupal\search_api_solr\Event\PreAddLanguageFallbackFieldEvent
   */
  const PRE_ADD_LANGUAGE_FALLBACK_FIELD = PreAddLanguageFallbackFieldEvent::class;

  /**
   * Fired before any Search API fields gets mapped to a Solr document.
   *
+31 −3
Changes for src/Plugin/search_api/backend/SearchApiSolrBackend.php: 31 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ use Drupal\search_api_solr\Event\PostExtractResultsEvent;
use Drupal\search_api_solr\Event\PostFieldMappingEvent;
use Drupal\search_api_solr\Event\PostIndexFinalizationEvent;
use Drupal\search_api_solr\Event\PostSetFacetsEvent;
use Drupal\search_api_solr\Event\PreAddLanguageFallbackFieldEvent;
use Drupal\search_api_solr\Event\PreAutocompleteTermsQueryEvent;
use Drupal\search_api_solr\Event\PreCreateIndexDocumentEvent;
use Drupal\search_api_solr\Event\PreExtractFacetsEvent;
@@ -1205,6 +1206,18 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
        $language_id = LanguageInterface::LANGCODE_NOT_SPECIFIED;
        $item->setLanguage($language_id);
      }

      /** @see \Drupal\search_api\Plugin\search_api\processor\LanguageWithFallback */
      $fallback_languages = [];
      $fallback_field_names = [];
      $language_with_fallback_field = $item->getField('language_with_fallback', FALSE);
      if ($language_with_fallback_field) {
        $fallback_languages = array_diff($language_with_fallback_field->getValues(), [$language_id, LanguageInterface::LANGCODE_NOT_SPECIFIED]);
      }

      foreach ($fallback_languages as $fallback_language) {
        $fallback_field_names[$fallback_language] = $this->getLanguageSpecificSolrFieldNames($fallback_language, $index);
      }
      $field_names = $this->getLanguageSpecificSolrFieldNames($language_id, $index);
      $boost_terms = [];

@@ -1259,8 +1272,8 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
          break;
        }

        if ($field->getPropertyPath() === 'auto_aggregated_fulltext_field') {
        $type = $field->getType();
        if ($field->getPropertyPath() === 'auto_aggregated_fulltext_field') {
          if (!array_key_exists($type, $auto_aggregate_values)) {
            foreach ($item_fields as $tmp_field) {
              if ($tmp_field->getType() === $type && $tmp_field->getPropertyPath() !== 'auto_aggregated_fulltext_field') {
@@ -1270,10 +1283,20 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
            $auto_aggregate_values[$type] = array_merge(...$auto_aggregate_values[$type]);
          }

          $first_value = $this->addIndexField($doc, $field_names[$name], $auto_aggregate_values[$type], $field->getType(), $boost_terms);
          $first_value = $this->addIndexField($doc, $field_names[$name], $auto_aggregate_values[$type], $type, $boost_terms);
          foreach ($fallback_languages as $fallback_language) {
            $event = new PreAddLanguageFallbackFieldEvent($fallback_language, $auto_aggregate_values[$type], $type);
            $this->eventDispatcher->dispatch($event);
            $this->addIndexField($doc, $fallback_field_names[$fallback_language][$name], $event->getValue(), $type, $boost_terms);
          }
        }
        else {
          $first_value = $this->addIndexField($doc, $field_names[$name], $field->getValues(), $field->getType(), $boost_terms);
          $first_value = $this->addIndexField($doc, $field_names[$name], $field->getValues(), $type, $boost_terms);
          foreach ($fallback_languages as $fallback_language) {
            $event = new PreAddLanguageFallbackFieldEvent($fallback_language, $field->getValues(), $type);
            $this->eventDispatcher->dispatch($event);
            $this->addIndexField($doc, $fallback_field_names[$fallback_language][$name], $event->getValue(), $type, $boost_terms);
          }
        }

        // Enable sorts in some special cases.
@@ -1322,6 +1345,11 @@ class SearchApiSolrBackend extends BackendPluginBase implements SolrBackendInter
              // copy the first value in a single value field for sorting.
              $doc->addField($key, $first_value);
            }
            foreach ($fallback_languages as $fallback_language) {
              $event = new PreAddLanguageFallbackFieldEvent($fallback_language, $first_value, $type);
              $this->eventDispatcher->dispatch($event);
              $doc->addField(preg_replace('/^([a-z]+)m_/', '$1s_', $fallback_field_names[$fallback_language][$name]), $event->getValue());
            }
          }
        }
      }