Possibility to remove empty rows of searchindex
table
#11705
-
Now that we have #11575 we can prune the search index of keywords that are not relevant for the specific use case of a CraftCMS installation. But CraftCMS still unconditionally creates a row for each searchable attribute of each element in the Even a row with an empty ~35 bytes per row might not seem much but it sums up to a sizeable amount. Does CraftCMS actually need rows in the Proposed change: remove all empty rowschange Lines 340 to 362 in 3db7fb2 to if ($this->hasEventHandlers(self::EVENT_BEFORE_INDEX_KEYWORDS)) {
$event = new IndexKeywordsEvent([
'element' => $element,
'attribute' => $attribute,
'fieldId' => $fieldId,
'keywords' => $keywords,
]);
$this->trigger(self::EVENT_BEFORE_INDEX_KEYWORDS, $event);
$keywords = $event->keywords;
}
// Save 'em
$columns = [
'elementId' => $element->id,
'attribute' => $attribute ?? 'field',
'fieldId' => $fieldId ? (string)$fieldId : '0',
'siteId' => $site->id,
];
if ($keywords !== '') {
// Add padding around keywords
$keywords = ' ' . $keywords . ' ';
} else {
return;
} If it is generally necessary to have Proposed change: skip row creation via eventchange Lines 340 to 349 in 3db7fb2 to if ($this->hasEventHandlers(self::EVENT_BEFORE_INDEX_KEYWORDS)) {
$event = new IndexKeywordsEvent([
'element' => $element,
'attribute' => $attribute,
'fieldId' => $fieldId,
'keywords' => $keywords,
]);
$this->trigger(self::EVENT_BEFORE_INDEX_KEYWORDS, $event);
$keywords = $event->keywords;
if ($event->handled) { // or add a new boolean property $skip
return;
}
} Here's an example: I have a CraftCMS site that has a volume with managed assets. Currently there are ~16,000 assets in the volume but this will only increase. The volume is managed which means custom code syncs new assets from a DAM (WoodWing) to the volume and deletes assets that are no longer needed. The folder structure and filenames are managed, too, and have no meaning to humans. Therefore we do not need the The following code can remove these keywords from the searchindex table in CraftCMS 4.2.0+: use craft\elements\Asset;
use craft\events\IndexKeywordsEvent;
use craft\services\Search;
use yii\base\Event;
Event::on(
Search::class,
Search::EVENT_BEFORE_INDEX_KEYWORDS,
static function (IndexKeywordsEvent $event) {
$element = $event->element;
if ($element instanceof Asset) {
if (
$element->getVolume()->handle == 'volumeWoodWing' &&
in_array($event->attribute, ['filename', 'kind', 'extension'])
) {
$event->keywords = '';
}
}
}
); After a When I manually delete the empty rows with That's a 14% size reduction. Footnotes
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
We do have a need for those rows: they are referenced for |
Beta Was this translation helpful? Give feedback.
-
Just made it possible to cancel indexing for an attribute/field via |
Beta Was this translation helpful? Give feedback.
Just made it possible to cancel indexing for an attribute/field via
EVENT_BEFORE_INDEX_KEYWORDS
for Craft 4.3, by setting$event->isValid = false
. (479c40b)