I have a site with 14 pieces of news and 44 basic pages.
- I have created an index (Search API) with this bundle:
and this is the index status (the content is already indexed during cron):
- When I edit the config, add the Basic page bundle and save it:
The index status is updated:
I want to do the part 2 programmatically.
I tried two ways:
$index = Index::load('my_index');
$index->setOption('datasource_settings', [
'entity:node' => [
'bundles' => [
'default' => 0,
'selected' => ['news', 'page'],
'languages' => [
'default' => 1',
'selected' => [ ],
],
]
],
]);
$index->save();
and
$datasource_settings = [
'entity:node' => [
'bundles' => [
'default' => 0,
'selected' => ['news', 'page'],
'languages' => [
'default' => 'TRUE',
'selected' => [ ],
],
]
],
];
$search_index = \Drupal::configFactory()->getEditable('search_api.index.my_index');
$search_index->set('datasource_settings', $datasource_settings)->save();
The config is saved, but the status index is not updated:
I tried to use these codes:
Option 1:
$index = Index::load('my_index');
$index->reindex();
But the nodes number are not updated, and I lose the nodes indexed:
Option 2:
$index = Index::load('my_index');
$index->rebuildTracker();
Result:
Option 3:
$index = Index::load('my_index');
$index->clear();
Result:
So my question is: How to update an index config programmatically and get the index status updated like the UI does when you click the Save button.
I tried to look inside the search_api module code, but I got lost :(
Thank you in advance.