class SearchIndex implements SearchIndexInterface (View source)

Provides search index management functions.

Properties

protected ConfigFactoryInterface $configFactory

The config factory.

protected Connection $connection

The database connection.

protected Connection $replica

The database replica connection.

protected CacheTagsInvalidatorInterface $cacheTagsInvalidator

The cache tags invalidator.

protected SearchTextProcessorInterface $textProcessor

The text processor.

Methods

__construct(ConfigFactoryInterface $config_factory, Connection $connection, Connection $replica, CacheTagsInvalidatorInterface $cache_tags_invalidator, SearchTextProcessorInterface $text_processor = NULL)

SearchIndex constructor.

string[]
index(string $type, int $sid, string $langcode, string $text, bool $update_weights = TRUE)

Updates the full-text search index for a particular item.

clear(string|null $type = NULL, int|array|null $sid = NULL, string|null $langcode = NULL)

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

markForReindex(string $type = NULL, int $sid = NULL, string $langcode = NULL)

Changes the timestamp on indexed items to 'now' to force reindexing.

updateWordWeights(array $words)

Updates the {search_total} database table.

Details

__construct(ConfigFactoryInterface $config_factory, Connection $connection, Connection $replica, CacheTagsInvalidatorInterface $cache_tags_invalidator, SearchTextProcessorInterface $text_processor = NULL)

SearchIndex constructor.

Parameters

ConfigFactoryInterface $config_factory

The config factory.

Connection $connection

The database connection.

Connection $replica

The database replica connection.

CacheTagsInvalidatorInterface $cache_tags_invalidator

The cache tags invalidator.

SearchTextProcessorInterface $text_processor

The text processor.

string[] index(string $type, int $sid, string $langcode, string $text, bool $update_weights = TRUE)

Updates the full-text search index for a particular item.

Parameters

string $type

The plugin ID or other machine-readable type of this item, which should be less than 64 bytes.

int $sid

An ID number identifying this particular item (e.g., node ID).

string $langcode

Language code for the language of the text being indexed.

string $text

The content of this item. Must be a piece of HTML or plain text.

bool $update_weights

(optional) TRUE if word weights should be updated. FALSE otherwise; defaults to TRUE. If you pass in FALSE, then you need to have your calls to this method in a try/finally block, and at the end of your index run in the finally clause, you will need to call self::updateWordWeights(), passing in all of the returned words, to update the word weights.

Return Value

string[]

The words to be updated.

Exceptions

SearchIndexException

clear(string|null $type = NULL, int|array|null $sid = NULL, string|null $langcode = NULL)

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.

Exceptions

SearchIndexException

markForReindex(string $type = NULL, int $sid = NULL, string $langcode = NULL)

Changes the timestamp on indexed items to 'now' to force reindexing.

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

Parameters

string $type

(optional) The plugin ID or other machine-readable type of this item. If omitted, the entire search index is marked for reindexing, and $sid and $langcode are ignored.

int $sid

(optional) An ID number identifying this particular item (e.g., node ID). If omitted, everything matching $type is marked, and $langcode is ignored.

string $langcode

(optional) The language code to mark. If omitted, everything matching $type and $sid is marked.

Exceptions

SearchIndexException

updateWordWeights(array $words)

Updates the {search_total} database table.

Parameters

array $words

An array whose keys are words from self::index() whose total weights need to be updated.

Exceptions

SearchIndexException