Add fields to a search index programmatically
Last updated on
20 January 2023
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Item\Field;
/**
* Add field to search API.
*/
function HOOK_update_N() {
$index = Index::load('ID');
/* Index the field. */
$field = new Field($index, 'field_machine_name');
$field->setType('text');
$field->setBoost(1.5); /* Do not set boost if your field type is integer*/
$field->setDatasourceId('datasourceId'); /* e.g. 'entity:commerce_product' */
$field->setPropertyPath('field_machine_name');
$field->setLabel('Field');
$index->addField($field);
$index->save();
}Help improve this page
Page status: No known problems
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion