You are here

public function SearchIndex::clear in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/search/src/SearchIndex.php \Drupal\search\SearchIndex::clear()

Clears either a part of, or the entire search index.

This function is meant for use by search page plugins, or for building a user interface that lets users clear all or parts of the search index.

Parameters

string|null $type: (optional) The plugin ID or other machine-readable type for the items to remove from the search index. If omitted, $sid and $langcode are ignored and the entire search index is cleared.

int|array|null $sid: (optional) The ID or array of IDs of the items to remove from the search index. If omitted, all items matching $type are cleared, and $langcode is ignored.

string|null $langcode: (optional) Language code of the item to remove from the search index. If omitted, all items matching $sid and $type are cleared.

Throws

\Drupal\search\Exception\SearchIndexException If there is an error clearing the index.

Overrides SearchIndexInterface::clear

1 call to SearchIndex::clear()
SearchIndex::index in core/modules/search/src/SearchIndex.php
Updates the full-text search index for a particular item.

File

core/modules/search/src/SearchIndex.php, line 232

Class

SearchIndex
Provides search index management functions.

Namespace

Drupal\search

Code

public function clear($type = NULL, $sid = NULL, $langcode = NULL) {
  try {
    $query_index = $this->connection
      ->delete('search_index');
    $query_dataset = $this->connection
      ->delete('search_dataset');
    if ($type) {
      $query_index
        ->condition('type', $type);
      $query_dataset
        ->condition('type', $type);
      if ($sid) {
        $query_index
          ->condition('sid', $sid);
        $query_dataset
          ->condition('sid', $sid);
        if ($langcode) {
          $query_index
            ->condition('langcode', $langcode);
          $query_dataset
            ->condition('langcode', $langcode);
        }
      }
    }
    $query_index
      ->execute();
    $query_dataset
      ->execute();
  } catch (\Exception $e) {
    throw new SearchIndexException("Failed to clear index for type '{$type}', sid '{$sid}' and langcode '{$langcode}'", 0, $e);
  }
  if ($type) {

    // Invalidate all render cache items that contain data from this index.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'search_index:' . $type,
    ]);
  }
  else {

    // Invalidate all render cache items that contain data from any index.
    $this->cacheTagsInvalidator
      ->invalidateTags([
      'search_index',
    ]);
  }
}