You are here

public function SearchApiSolrBackend::getDocuments in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getDocuments()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getDocuments()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getDocuments()

Retrieves Solr documents from search api index items.

Parameters

\Drupal\search_api\IndexInterface $index: The search api index.

\Drupal\search_api\Item\ItemInterface[] $items: An array of items to get documents for.

\Solarium\QueryType\Update\Query\Query $update_query: The existing update query the documents should be added to.

Return value

\Solarium\QueryType\Update\Query\Document[] An array of solr documents.

Overrides SolrBackendInterface::getDocuments

2 calls to SearchApiSolrBackend::getDocuments()
SearchApiSolrBackend::getDocument in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Retrieves a Solr document from an search api index item.
SearchApiSolrBackend::indexItems in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Indexes the specified items.

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 736

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function getDocuments(IndexInterface $index, array $items, UpdateQuery $update_query = NULL) {
  $connector = $this
    ->getSolrConnector();
  $schema_version = $connector
    ->getSchemaVersion();
  $documents = array();
  $index_id = $this
    ->getIndexId($index
    ->id());
  $field_names = $this
    ->getSolrFieldNames($index);
  $languages = $this->languageManager
    ->getLanguages();
  $base_urls = array();
  if (!$update_query) {
    $update_query = $connector
      ->getUpdateQuery();
  }

  /** @var \Drupal\search_api\Item\ItemInterface[] $items */
  foreach ($items as $id => $item) {

    /** @var \Solarium\QueryType\Update\Query\Document $doc */
    $doc = $update_query
      ->createDocument();
    $doc
      ->setField('id', $this
      ->createId($index_id, $id));
    $doc
      ->setField('index_id', $index_id);

    // Add document level boost from Search API item.
    if ($boost = $item
      ->getBoost()) {
      $doc
        ->setBoost($boost);
    }

    // Add the site hash and language-specific base URL.
    $doc
      ->setField('hash', SearchApiSolrUtility::getSiteHash());
    $lang = $item
      ->getLanguage();
    if (empty($base_urls[$lang])) {
      $url_options = array(
        'absolute' => TRUE,
      );
      if (isset($languages[$lang])) {
        $url_options['language'] = $languages[$lang];
      }

      // An exception is thrown if this is called during a non-HTML response
      // like REST or a redirect without collecting metadata. Avoid that by
      // collecting and discarding it.
      // See https://www.drupal.org/node/2638686.
      $base_urls[$lang] = Url::fromRoute('<front>', array(), $url_options)
        ->toString(TRUE)
        ->getGeneratedUrl();
    }
    $doc
      ->setField('site', $base_urls[$lang]);
    $item_fields = $item
      ->getFields();
    $item_fields += $special_fields = $this
      ->getSpecialFields($index, $item);

    /** @var \Drupal\search_api\Item\FieldInterface $field */
    foreach ($item_fields as $name => $field) {

      // If the field is not known for the index, something weird has
      // happened. We refuse to index the items and hope that the others are
      // OK.
      if (!isset($field_names[$name])) {
        $vars = array(
          '%field' => $name,
          '@id' => $id,
        );
        \Drupal::logger('search_api_solr')
          ->warning('Error while indexing: Unknown field %field on the item with ID @id.', $vars);
        $doc = NULL;
        break;
      }
      $this
        ->addIndexField($doc, $field_names[$name], $field
        ->getValues(), $field
        ->getType());

      // Enable sorts in some special cases.
      if (!array_key_exists($name, $special_fields) && version_compare($schema_version, '4.4', '>=')) {
        $values = $field
          ->getValues();
        $first_value = reset($values);
        if ($first_value) {

          // Truncate the string to avoid Solr string field limitation.
          // @see https://www.drupal.org/node/2809429
          // @see https://www.drupal.org/node/2852606
          // 32 characters should be enough for sorting and it makes no sense
          // to heavily increase the index size. The DB backend limits the
          // sort strings to 32 characters, too.
          if ($first_value instanceof TextValue && mb_strlen($first_value
            ->getText()) > 32) {
            $first_value = new TextValue(Unicode::truncate($first_value
              ->getText(), 32));
          }
          if (strpos($field_names[$name], 't') === 0 || strpos($field_names[$name], 's') === 0) {

            // Always copy fulltext fields to a dedicated field for faster
            // alpha sorts. Copy strings as well to normalize them.
            $this
              ->addIndexField($doc, 'sort_' . $name, [
              $first_value,
            ], $field
              ->getType());
          }
          elseif (preg_match('/^([a-z]+)m(_.*)/', $field_names[$name], $matches)) {

            // For other multi-valued fields (which aren't sortable by nature)
            // we use the same hackish workaround like the DB backend: just
            // copy the first value in a single value field for sorting.
            $values = $field
              ->getValues();
            $this
              ->addIndexField($doc, $matches[1] . 's' . $matches[2], [
              $first_value,
            ], $field
              ->getType());
          }
        }
      }
    }
    if ($doc) {
      $documents[] = $doc;
    }
  }

  // Let other modules alter documents before sending them to solr.
  $this->moduleHandler
    ->alter('search_api_solr_documents', $documents, $index, $items);
  $this
    ->alterSolrDocuments($documents, $index, $items);
  return $documents;
}