Magento 2 creates one fulltext search table per store view, but I am wondering where exactly these are created and how I can force it.
Context: We have a setup script (InstallData
) that creates store views, but in integration tests the fulltext tables for these store views are missing, which leads to errors from fixtures that try to save a product.
I tried reindexing in the setup script, after the stores were saved, but it did not help:
...
$this->getIndexer('catalogsearch_fulltext')->reindexAll();
}
private function getIndexer($indexerId)
{
return $this->indexerFactory->create()->load($indexerId);
}
Best Answer
It seems that it happens in the following observer -
Magento\CatalogSearch\Model\Indexer\Fulltext\Store
(on store_add event which seems to be fired when you save a new store view).
You can see that the observer eventually gets to
Magento\CatalogSearch\Model\Indexer\IndexerHandler::cleanIndex
, where it eventually calls\Magento\CatalogSearch\Model\Indexer\IndexStructure::delete
and\Magento\CatalogSearch\Model\Indexer\IndexStructure::create
, where it eventually gets to the following code that creates the catalogsearch_fulltext_scopeN table -(class Magento\CatalogSearch\Model\Indexer\IndexStructure)
(where $tableName is catalogsearch_fulltext_scopeN)
Therefore, in order to force the table creation, I suggest to run the following code in your setup script (it's an imitation of Magento\CatalogSearch\Model\Indexer\Fulltext\Store::clearIndex)