diff --git a/config/schema/search_api.index.schema.yml b/config/schema/search_api.index.schema.yml index 3281e56..511fb76 100644 --- a/config/schema/search_api.index.schema.yml +++ b/config/schema/search_api.index.schema.yml @@ -17,65 +17,65 @@ search_api.index.*: read_only: type: boolean label: 'Read-only' + fields: + type: sequence + label: 'Indexed fields' + sequence: + type: mapping + label: field + mapping: + label: + type: string + label: 'A label for the field' + datasource_id: + type: string + label: 'The datasource ID of the field' + property_path: + type: string + label: 'The property path of the field' + type: + type: string + label: 'The data type of the field' + boost: + type: float + label: 'The boost of the field' + indexed_locked: + type: boolean + label: 'Whether the field is locked or can be removed' + type_locked: + type: boolean + label: 'Whether the field''s data type is locked or can be changed' + hidden: + type: boolean + label: 'Whether the field should appear in the UI' + processors: + type: sequence + label: 'Processor settings' + sequence: + type: mapping + label: 'A processor' + mapping: + processor_id: + type: string + label: 'The plugin ID of the processor' + weights: + type: sequence + label: 'The processor''s weights for the different processing stages' + sequence: + type: integer + label: 'The processor''s weight for this stage' + settings: + type: plugin.plugin_configuration.search_api_processor.[%parent.processor_id] options: type: mapping label: 'Options' mapping: - fields: - type: sequence - label: 'Indexed fields' - sequence: - type: mapping - label: field - mapping: - label: - type: string - label: 'A label for the field' - datasource_id: - type: string - label: 'The datasource ID of the field' - property_path: - type: string - label: 'The property path of the field' - type: - type: string - label: 'The data type of the field' - boost: - type: float - label: 'The boost of the field' - indexed_locked: - type: boolean - label: 'Whether the field is locked or can be removed' - type_locked: - type: boolean - label: 'Whether the field''s data type is locked or can be changed' - hidden: - type: boolean - label: 'Whether the field should appear in the UI' cron_limit: type: integer label: 'Cron batch size' index_directly: type: boolean label: 'Index items immediately' - processors: - type: sequence - label: 'Processor settings' - sequence: - type: mapping - label: 'A processor' - mapping: - processor_id: - type: string - label: 'The plugin ID of the processor' - weights: - type: sequence - label: 'The processor''s weights for the different processing stages' - sequence: - type: integer - label: 'The processor''s weight for this stage' - settings: - type: plugin.plugin_configuration.search_api_processor.[%parent.processor_id] datasources: type: sequence label: 'Datasource plugin IDs' diff --git a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml index 85599b0..42d3422 100644 --- a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml +++ b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml @@ -2,205 +2,205 @@ id: default_index name: 'Default content index' description: 'Default content index created by the Database Search Defaults module' read_only: false +fields: + search_api_language: + label: 'Item language' + type: string + property_path: search_api_language + index_locked: true + type_locked: true + title: + label: Title + type: text + datasource_id: 'entity:node' + property_path: title + boost: 8 + rendered_item: + label: 'Rendered item' + type: text + property_path: rendered_item + created: + label: 'Authored on' + type: date + datasource_id: 'entity:node' + property_path: created + changed: + label: Changed + type: date + datasource_id: 'entity:node' + property_path: changed + status: + label: 'Publishing status' + type: boolean + datasource_id: 'entity:node' + property_path: status + index_locked: true + type_locked: true + sticky: + label: 'Sticky at top of lists' + type: boolean + datasource_id: 'entity:node' + property_path: sticky + field_tags: + label: Tags + type: string + datasource_id: 'entity:node' + property_path: 'field_tags:entity:name' + author: + label: 'Author name' + type: string + datasource_id: 'entity:node' + property_path: 'uid:entity:name' + uid: + label: 'Author ID' + type: integer + datasource_id: 'entity:node' + property_path: uid + index_locked: true + type_locked: true + search_api_node_grants: + label: 'Node access information' + type: string + property_path: search_api_node_grants + index_locked: true + type_locked: true + hidden: true + type: + type: string + datasource_id: 'entity:node' + property_path: type +processors: + content_access: + processor_id: content_access + weights: + preprocess_index: -6 + preprocess_query: -4 + settings: { } + highlight: + processor_id: highlight + weights: + postprocess_query: -9 + settings: + highlight: always + excerpt: true + excerpt_length: 256 + prefix: '' + suffix: '' + html_filter: + processor_id: html_filter + weights: + preprocess_index: -3 + preprocess_query: -6 + settings: + fields: + - rendered_item + title: true + alt: true + tags: + h1: 5 + h2: 3 + h3: 2 + string: 2 + b: 2 + ignorecase: + processor_id: ignorecase + weights: + preprocess_index: -5 + preprocess_query: -8 + settings: + fields: + - rendered_item + - title + language: + processor_id: language + weights: + preprocess_index: -50 + settings: { } + node_status: + processor_id: node_status + weights: + preprocess_index: -10 + settings: { } + rendered_item: + processor_id: rendered_item + weights: + preprocess_index: -8 + settings: + roles: + anonymous: anonymous + view_mode: + 'entity:node': + article: search_index + page: search_index + stopwords: + processor_id: stopwords + weights: + preprocess_query: -10 + postprocess_query: -10 + settings: + fields: + - rendered_item + - title + stopwords: + - a + - an + - and + - are + - as + - at + - be + - but + - by + - for + - if + - in + - into + - is + - it + - 'no' + - not + - of + - 'on' + - or + - s + - such + - t + - that + - the + - their + - then + - there + - these + - they + - this + - to + - was + - will + - with + tokenizer: + processor_id: tokenizer + weights: + preprocess_index: -2 + preprocess_query: -5 + settings: + fields: + - rendered_item + - title + spaces: '' + overlap_cjk: 1 + minimum_word_size: '3' + transliteration: + processor_id: transliteration + weights: + preprocess_index: -4 + preprocess_query: -7 + settings: + fields: + - rendered_item + - title options: index_directly: true cron_limit: 50 - processors: - content_access: - processor_id: content_access - weights: - preprocess_index: -6 - preprocess_query: -4 - settings: { } - highlight: - processor_id: highlight - weights: - postprocess_query: -9 - settings: - highlight: always - excerpt: true - excerpt_length: 256 - prefix: '' - suffix: '' - html_filter: - processor_id: html_filter - weights: - preprocess_index: -3 - preprocess_query: -6 - settings: - fields: - - rendered_item - title: true - alt: true - tags: - h1: 5 - h2: 3 - h3: 2 - string: 2 - b: 2 - ignorecase: - processor_id: ignorecase - weights: - preprocess_index: -5 - preprocess_query: -8 - settings: - fields: - - rendered_item - - title - language: - processor_id: language - weights: - preprocess_index: -50 - settings: { } - node_status: - processor_id: node_status - weights: - preprocess_index: -10 - settings: { } - rendered_item: - processor_id: rendered_item - weights: - preprocess_index: -8 - settings: - roles: - anonymous: anonymous - view_mode: - 'entity:node': - article: search_index - page: search_index - stopwords: - processor_id: stopwords - weights: - preprocess_query: -10 - postprocess_query: -10 - settings: - fields: - - rendered_item - - title - stopwords: - - a - - an - - and - - are - - as - - at - - be - - but - - by - - for - - if - - in - - into - - is - - it - - 'no' - - not - - of - - 'on' - - or - - s - - such - - t - - that - - the - - their - - then - - there - - these - - they - - this - - to - - was - - will - - with - tokenizer: - processor_id: tokenizer - weights: - preprocess_index: -2 - preprocess_query: -5 - settings: - fields: - - rendered_item - - title - spaces: '' - overlap_cjk: 1 - minimum_word_size: '3' - transliteration: - processor_id: transliteration - weights: - preprocess_index: -4 - preprocess_query: -7 - settings: - fields: - - rendered_item - - title - fields: - search_api_language: - label: 'Item language' - type: string - property_path: search_api_language - index_locked: true - type_locked: true - title: - label: Title - type: text - datasource_id: 'entity:node' - property_path: title - boost: 8 - rendered_item: - label: 'Rendered item' - type: text - property_path: rendered_item - created: - label: 'Authored on' - type: date - datasource_id: 'entity:node' - property_path: created - changed: - label: Changed - type: date - datasource_id: 'entity:node' - property_path: changed - status: - label: 'Publishing status' - type: boolean - datasource_id: 'entity:node' - property_path: status - index_locked: true - type_locked: true - sticky: - label: 'Sticky at top of lists' - type: boolean - datasource_id: 'entity:node' - property_path: sticky - field_tags: - label: Tags - type: string - datasource_id: 'entity:node' - property_path: 'field_tags:entity:name' - author: - label: 'Author name' - type: string - datasource_id: 'entity:node' - property_path: 'uid:entity:name' - uid: - label: 'Author ID' - type: integer - datasource_id: 'entity:node' - property_path: uid - index_locked: true - type_locked: true - search_api_node_grants: - label: 'Node access information' - type: string - property_path: search_api_node_grants - index_locked: true - type_locked: true - hidden: true - type: - type: string - datasource_id: 'entity:node' - property_path: type datasources: - 'entity:node' datasource_configs: diff --git a/search_api_db/src/Plugin/search_api/backend/Database.php b/search_api_db/src/Plugin/search_api/backend/Database.php index b1d9fc4..2336a9e 100644 --- a/search_api_db/src/Plugin/search_api/backend/Database.php +++ b/search_api_db/src/Plugin/search_api/backend/Database.php @@ -1283,7 +1283,7 @@ class Database extends BackendPluginBase { if (count($words) > 1 && max(array_map('strlen', $words)) <= 50) { // Overlong token is due to bad tokenizing. // Check for "Tokenizer" preprocessor on index. - if (empty($index->getOption('processors')['search_api_tokenizer']['status'])) { + if (empty($index->getProcessorSettings()['search_api_tokenizer']['status'])) { $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. It is recommended to enable the "Tokenizer" preprocessor for indexes using database servers. Otherwise, the backend class has to use its own, fixed tokenizing.'); } else { diff --git a/search_api_db/src/Tests/BackendTest.php b/search_api_db/src/Tests/BackendTest.php index 29ae8ad..3671af6 100644 --- a/search_api_db/src/Tests/BackendTest.php +++ b/search_api_db/src/Tests/BackendTest.php @@ -166,7 +166,7 @@ class BackendTest extends EntityUnitTestBase { $index->removeField('keywords'); $index->save(); - $index_fields = array_keys($index->getOption('fields', array())); + $index_fields = array_keys($index->getFields()); $db_info = \Drupal::keyValue(BackendDatabase::INDEXES_KEY_VALUE_STORE_ID)->get($this->indexId); $server_fields = array_keys($db_info['field_tables']); @@ -189,13 +189,13 @@ class BackendTest extends EntityUnitTestBase { $property = 'body'; $this->addField($index, $property); - $processors = $index->getOption('processors', array()); + $processors = $index->getProcessorSettings(); $processors['html_filter'] = array( 'processor_id' => 'html_filter', 'weights' => array(), 'settings' => array(), ); - $index->setOption('processors', $processors); + $index->setProcessorSettings($processors); $index->save(); } @@ -205,9 +205,9 @@ class BackendTest extends EntityUnitTestBase { protected function disableHtmlFilter() { /** @var \Drupal\search_api\IndexInterface $index */ $index = $this->getIndex(); - $processors = $index->getOption('processors'); + $processors = $index->getProcessorSettings(); unset($processors['html_filter']); - $index->setOption('processors', $processors); + $index->setProcessorSettings($processors); $index->removeField('body'); $index->save(); } diff --git a/src/Entity/Index.php b/src/Entity/Index.php index ca47735..5e76b1a 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -9,7 +9,6 @@ namespace Drupal\search_api\Entity; use Drupal\Component\Render\FormattableMarkup; use Drupal\Core\Cache\Cache; -use Drupal\Core\Cache\RefinableCacheableDependencyInterface; use Drupal\Core\Config\Entity\ConfigEntityBase; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityStorageInterface; @@ -59,6 +58,8 @@ use Drupal\views\Views; * "name", * "description", * "read_only", + * "fields", + * "processors", * "options", * "datasources", * "datasource_configs", @@ -111,6 +112,13 @@ class Index extends ConfigEntityBase implements IndexInterface { protected $read_only = FALSE; /** + * An array of field settings. + * + * @var array + */ + protected $fields = array(); + + /** * An array of options configuring this index. * * @var array @@ -198,13 +206,21 @@ class Index extends ConfigEntityBase implements IndexInterface { protected $cache = array(); /** + * The array of processor settings. + * + * @var array + * An array containing processor settings. + */ + protected $processors = array(); + + /** * Cached information about the processors available for this index. * * @var \Drupal\search_api\Processor\ProcessorInterface[]|null * * @see loadProcessors() */ - protected $processors; + protected $processorPlugins; /** * Whether reindexing has been triggered for this index in this page request. @@ -226,8 +242,6 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->options += array( 'cron_limit' => \Drupal::config('search_api.settings')->get('default_cron_limit'), 'index_directly' => TRUE, - 'fields' => array(), - 'processors' => array(), ); } @@ -263,6 +277,10 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getOption($name, $default = NULL) { + // @todo Remove before stable release. + if (in_array($name, array('fields', 'processors'))) { + throw new \UnexpectedValueException("Deprecated call, " . $name . " now has its own getter and no longer belongs in options."); + } return isset($this->options[$name]) ? $this->options[$name] : $default; } @@ -277,6 +295,10 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function setOption($name, $option) { + // @todo Remove before stable release. + if (in_array($name, array('fields', 'processors'))) { + throw new \UnexpectedValueException("Deprecated call, " . $name . " now has its own setter and no longer belongs in options."); + } $this->options[$name] = $option; // If the fields are changed, reset the static fields cache. if ($name == 'fields') { @@ -437,7 +459,7 @@ class Index extends ConfigEntityBase implements IndexInterface { // Filter processors by status if required. Enabled processors are those // which have settings in the "processors" option. if ($only_enabled) { - $processors_settings = $this->getOption('processors', array()); + $processors_settings = $this->getProcessorSettings(); $processors = array_intersect_key($processors, $processors_settings); } @@ -449,7 +471,7 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getProcessorsByStage($stage, $only_enabled = TRUE) { $processors = $this->loadProcessors(); - $processor_settings = $this->getOption('processors', array()); + $processor_settings = $this->getProcessorSettings(); $processor_weights = array(); // Get a list of all processors meeting the criteria (stage and, optionally, @@ -482,13 +504,13 @@ class Index extends ConfigEntityBase implements IndexInterface { * The loaded processors, keyed by processor ID. */ protected function loadProcessors() { - if (!isset($this->processors)) { + if (empty($this->processorPlugins)) { /** @var $processor_plugin_manager \Drupal\search_api\Processor\ProcessorPluginManager */ $processor_plugin_manager = \Drupal::service('plugin.manager.search_api.processor'); - $processor_settings = $this->getOption('processors', array()); + $processor_settings = $this->getProcessorSettings(); foreach ($processor_plugin_manager->getDefinitions() as $name => $processor_definition) { - if (class_exists($processor_definition['class']) && empty($this->processors[$name])) { + if (class_exists($processor_definition['class']) && empty($this->processorPlugins[$name])) { // Create our settings for this processor. $settings = empty($processor_settings[$name]['settings']) ? array() : $processor_settings[$name]['settings']; $settings['index'] = $this; @@ -496,7 +518,7 @@ class Index extends ConfigEntityBase implements IndexInterface { /** @var $processor \Drupal\search_api\Processor\ProcessorInterface */ $processor = $processor_plugin_manager->createInstance($name, $settings); if ($processor->supportsIndex($this)) { - $this->processors[$name] = $processor; + $this->processorPlugins[$name] = $processor; } } elseif (!class_exists($processor_definition['class'])) { @@ -505,12 +527,27 @@ class Index extends ConfigEntityBase implements IndexInterface { } } + return $this->processorPlugins; + } + + /** + * {@inheritdoc} + */ + public function getProcessorSettings() { return $this->processors; } /** * {@inheritdoc} */ + public function setProcessorSettings(array $processors) { + $this->processors = $processors; + return $this; + } + + /** + * {@inheritdoc} + */ public function preprocessIndexItems(array &$items) { foreach ($this->getProcessorsByStage(ProcessorInterface::STAGE_PREPROCESS_INDEX) as $processor) { $processor->preprocessIndexItems($items); @@ -559,7 +596,7 @@ class Index extends ConfigEntityBase implements IndexInterface { throw new SearchApiException(new FormattableMarkup('Cannot add field with machine name %field_id: machine name is already taken.', $args)); } - $this->options['fields'][$field_id] = $field->getSettings(); + $this->fields[$field_id] = $field->getSettings(); $this->resetCaches(); return $this; @@ -569,7 +606,7 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function renameField($old_field_id, $new_field_id) { - if (isset($this->options['fields'][$old_field_id])) { + if (isset($this->fields[$old_field_id])) { $args['%field_id'] = $old_field_id; throw new SearchApiException(new FormattableMarkup('Could not rename field with machine name %field_id: no such field.', $args)); } @@ -577,13 +614,13 @@ class Index extends ConfigEntityBase implements IndexInterface { $args['%field_id'] = $new_field_id; throw new SearchApiException(new FormattableMarkup('%field_id is a reserved value and cannot be used as the machine name of a normal field.', $args)); } - if (isset($this->options['fields'][$new_field_id])) { + if (isset($this->fields[$new_field_id])) { $args['%field_id'] = $new_field_id; throw new SearchApiException(new FormattableMarkup('%field_id is a reserved value and cannot be used as the machine name of a normal field.', $args)); } - $this->options['fields'][$new_field_id] = $this->options['fields'][$old_field_id]; - unset($this->options['fields'][$old_field_id]); + $this->fields[$new_field_id] = $this->fields[$old_field_id]; + unset($this->fields[$old_field_id]); $this->resetCaches(); return $this; @@ -602,7 +639,7 @@ class Index extends ConfigEntityBase implements IndexInterface { throw new SearchApiException(new FormattableMarkup('Cannot remove field with machine name %field_id: field is locked.', $args)); } - unset($this->options['fields'][$field_id]); + unset($this->fields[$field_id]); $this->resetCaches(); return $this; @@ -615,7 +652,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $fields = $this->getCache(__FUNCTION__, FALSE); if (!$fields) { $fields = array(); - foreach ($this->getOption('fields', array()) as $key => $field_info) { + foreach ($this->getFieldSettings() as $key => $field_info) { $fields[$key] = Utility::createField($this, $key, $field_info); } } @@ -656,7 +693,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $fulltext_fields = $this->getCache(__FUNCTION__); if (!$fulltext_fields) { $fulltext_fields = array(); - foreach ($this->getOption('fields', array()) as $key => $field_info) { + foreach ($this->getFieldSettings() as $key => $field_info) { if (Utility::isTextType($field_info['type'])) { $fulltext_fields[] = $key; } @@ -669,6 +706,21 @@ class Index extends ConfigEntityBase implements IndexInterface { /** * {@inheritdoc} */ + public function getFieldSettings() { + return $this->fields; + } + + /** + * {@inheritdoc} + */ + public function setFieldSettings(array $fields = array()) { + $this->fields = $fields; + return $this; + } + + /** + * {@inheritdoc} + */ public function getPropertyDefinitions($datasource_id, $alter = TRUE) { $alter = $alter ? 1 : 0; $properties = $this->getCache(__FUNCTION__); @@ -759,7 +811,7 @@ class Index extends ConfigEntityBase implements IndexInterface { if (!$this->status) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (index is disabled)", array('%index' => $this->label()))); } - if (empty($this->options['fields'])) { + if (empty($this->fields)) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (no fields selected)", array('%index' => $this->label()))); } @@ -983,7 +1035,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasourcePlugins = NULL; $this->trackerPlugin = NULL; $this->serverInstance = NULL; - $this->processors = NULL; + $this->processorPlugins = NULL; $this->properties = NULL; $this->cache = array(); if ($include_stored) { @@ -1036,17 +1088,17 @@ class Index extends ConfigEntityBase implements IndexInterface { // Remove all "locked" and "hidden" flags from all fields of the index. If // they are still valid, they should be re-added by the processors. - foreach ($this->options['fields'] as $field_id => $field_settings) { - unset($this->options['fields'][$field_id]['indexed_locked']); - unset($this->options['fields'][$field_id]['type_locked']); - unset($this->options['fields'][$field_id]['hidden']); + foreach ($this->fields as $field_id => $field_settings) { + unset($this->fields[$field_id]['indexed_locked']); + unset($this->fields[$field_id]['type_locked']); + unset($this->fields[$field_id]['hidden']); } // We first have to check for locked processors, otherwise their // preIndexSave() methods might not be called in the next step. foreach ($this->getProcessors(FALSE) as $processor_id => $processor) { - if ($processor->isLocked() && !isset($this->options['processors'][$processor_id])) { - $this->options['processors'][$processor_id] = array( + if ($processor->isLocked() && !isset($this->processors[$processor_id])) { + $this->processors[$processor_id] = array( 'processor_id' => $processor_id, 'weights' => array(), 'settings' => array(), @@ -1200,8 +1252,8 @@ class Index extends ConfigEntityBase implements IndexInterface { * The previous version of the index. */ protected function reactToProcessorChanges(IndexInterface $original) { - $original_settings = $original->getOption('processors', array()); - $new_settings = $this->getOption('processors', array()); + $original_settings = $original->getProcessorSettings(); + $new_settings = $this->getProcessorSettings(); // Only actually do something when the processor settings are changed. if ($original_settings != $new_settings) { diff --git a/src/Form/IndexProcessorsForm.php b/src/Form/IndexProcessorsForm.php index e0149fa..d32e5ac 100644 --- a/src/Form/IndexProcessorsForm.php +++ b/src/Form/IndexProcessorsForm.php @@ -112,7 +112,7 @@ class IndexProcessorsForm extends EntityForm { } } - $processor_settings = $this->entity->getOption('processors'); + $processor_settings = $this->entity->getProcessorSettings(); $form['#tree'] = TRUE; $form['#attached']['library'][] = 'search_api/drupal.search_api.index-active-formatters'; @@ -279,10 +279,10 @@ class IndexProcessorsForm extends EntityForm { // Sort the processors so we won't have unnecessary changes. ksort($new_settings); - $settings_changed = $new_settings != $this->entity->getOption('processors', array()); + $settings_changed = $new_settings != $this->entity->getProcessorSettings(); $form_state->set('processors_changed', $settings_changed); if ($settings_changed) { - $this->entity->setOption('processors', $new_settings); + $this->entity->setProcessorSettings($new_settings); } } diff --git a/src/IndexInterface.php b/src/IndexInterface.php index d92fd0a..3d8fb82 100644 --- a/src/IndexInterface.php +++ b/src/IndexInterface.php @@ -277,6 +277,24 @@ interface IndexInterface extends ConfigEntityInterface { public function getProcessorsByStage($stage, $only_enabled = TRUE); /** + * Retrieves this index's processor settings. + * + * @return array + * An array of processors and their settings. + */ + public function getProcessorSettings(); + + /** + * Sets this index's processor settings. + * + * @param array $processors + * An array of processors and their settings. + * + * @return $this + */ + public function setProcessorSettings(array $processors); + + /** * Preprocesses data items for indexing. * * Lets all enabled processors for this index preprocess the indexed data. @@ -402,6 +420,24 @@ interface IndexInterface extends ConfigEntityInterface { public function getFulltextFields(); /** + * Retrieves this index's field settings. + * + * @return array + * An array of field settings. + */ + public function getFieldSettings(); + + /** + * Sets this index's field settings. + * + * @param array $fields + * An array of field settings. + * + * @return $this + */ + public function setFieldSettings(array $fields); + + /** * Retrieves the properties of one of this index's datasources. * * @param string|null $datasource_id diff --git a/src/Item/Field.php b/src/Item/Field.php index adaba08..a53121f 100644 --- a/src/Item/Field.php +++ b/src/Item/Field.php @@ -378,10 +378,10 @@ class Field implements \IteratorAggregate, FieldInterface { public function setType($type, $notify = FALSE) { $this->type = $type; if ($notify) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFieldSettings(); if (isset($fields[$this->fieldIdentifier])) { $fields[$this->fieldIdentifier]['type'] = $type; - $this->index->setOption('fields', $fields); + $this->index->setFieldSettings($fields); } } return $this; @@ -439,7 +439,7 @@ class Field implements \IteratorAggregate, FieldInterface { */ public function getBoost() { if (!isset($this->boost)) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFieldSettings(); $this->boost = isset($fields[$this->fieldIdentifier]['boost']) ? (float) $fields[$this->fieldIdentifier]['boost'] : 1.0; } return $this->boost; diff --git a/src/Plugin/views/argument/SearchApiMoreLikeThis.php b/src/Plugin/views/argument/SearchApiMoreLikeThis.php index f1da51f..f39c8b0 100644 --- a/src/Plugin/views/argument/SearchApiMoreLikeThis.php +++ b/src/Plugin/views/argument/SearchApiMoreLikeThis.php @@ -79,9 +79,7 @@ class SearchApiMoreLikeThis extends SearchApiArgument { } $fields = isset($this->options['fields']) ? $this->options['fields'] : array(); if (!$fields) { - foreach ($this->query->getIndex()->getOption('fields', array()) as $key => $field) { - $fields[] = $key; - } + $fields = array_keys($this->query->getIndex()->getFields()); } $mlt = array( 'id' => $this->argument, diff --git a/src/Tests/Processor/ProcessorIntegrationTest.php b/src/Tests/Processor/ProcessorIntegrationTest.php index cfac2e1..912b715 100644 --- a/src/Tests/Processor/ProcessorIntegrationTest.php +++ b/src/Tests/Processor/ProcessorIntegrationTest.php @@ -175,10 +175,10 @@ class ProcessorIntegrationTest extends WebTestBase { */ public function checkLanguageIntegration() { $index = $this->loadIndex(); - $processors = $index->getOption('processors', array()); + $processors = $index->getProcessorSettings(); $this->assertTrue(!empty($processors['language']), 'The "language" processor is enabled by default.'); unset($processors['language']); - $index->setOption('processors', $processors)->save(); + $index->setProcessorSettings($processors)->save(); $processors = $this->loadIndex()->getProcessors(); $this->assertTrue(!empty($processors['language']), 'The "language" processor cannot be disabled.'); } @@ -247,10 +247,10 @@ class ProcessorIntegrationTest extends WebTestBase { */ public function checkUrlFieldIntegration() { $index = $this->loadIndex(); - $processors = $index->getOption('processors', array()); + $processors = $index->getProcessorSettings(); $this->assertTrue(!empty($processors['add_url']), 'The "Add URL" processor is enabled by default.'); unset($processors['add_url']); - $index->setOption('processors', $processors)->save(); + $index->setProcessorSettings($processors)->save(); $processors = $this->loadIndex()->getProcessors(); $this->assertTrue(!empty($processors['add_url']), 'The "Add URL" processor cannot be disabled.'); } diff --git a/src/Tests/Processor/ProcessorTestBase.php b/src/Tests/Processor/ProcessorTestBase.php index 994ec12..b5d4c3f 100644 --- a/src/Tests/Processor/ProcessorTestBase.php +++ b/src/Tests/Processor/ProcessorTestBase.php @@ -92,7 +92,7 @@ abstract class ProcessorTestBase extends EntityUnitTestBase { 'tracker' => 'default', )); $this->index->setServer($this->server); - $this->index->setOption('fields', array( + $this->index->setFieldSettings(array( 'subject' => array( 'label' => 'Subject', 'type' => 'text', @@ -107,7 +107,7 @@ abstract class ProcessorTestBase extends EntityUnitTestBase { ), )); if ($processor) { - $this->index->setOption('processors', array( + $this->index->setProcessorSettings(array( $processor => array( 'processor_id' => $processor, 'weights' => array(), diff --git a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml index adb2704..4f42d29 100644 --- a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml +++ b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml @@ -2,58 +2,58 @@ id: database_search_index name: 'Test index' description: 'An index used for testing' read_only: false +fields: + id: + label: ID + type: integer + datasource_id: 'entity:entity_test' + property_path: id + name: + label: Name + type: text + datasource_id: 'entity:entity_test' + property_path: name + boost: 5.0 + body: + label: Body + type: text + datasource_id: 'entity:entity_test' + property_path: body + type: + label: Type + type: string + datasource_id: 'entity:entity_test' + property_path: type + keywords: + label: Keywords + type: string + datasource_id: 'entity:entity_test' + property_path: keywords + category: + label: Category + type: string + datasource_id: 'entity:entity_test' + property_path: category + search_api_language: + label: 'Item language' + type: string + property_path: search_api_language + index_locked: true + type_locked: true +processors: + add_url: + processor_id: add_url + weights: + preprocess_index: -30 + settings: { } + language: + processor_id: language + weights: + preprocess_index: -50 + settings: { } options: cron_limit: -1 index_directly: false - fields: - id: - label: ID - type: integer - datasource_id: 'entity:entity_test' - property_path: id - name: - label: Name - type: text - datasource_id: 'entity:entity_test' - property_path: name - boost: 5.0 - body: - label: Body - type: text - datasource_id: 'entity:entity_test' - property_path: body - type: - label: Type - type: string - datasource_id: 'entity:entity_test' - property_path: type - keywords: - label: Keywords - type: string - datasource_id: 'entity:entity_test' - property_path: keywords - category: - label: Category - type: string - datasource_id: 'entity:entity_test' - property_path: category - search_api_language: - label: 'Item language' - type: string - property_path: search_api_language - index_locked: true - type_locked: true - processors: - add_url: - processor_id: add_url - weights: - preprocess_index: -30 - settings: { } - language: - processor_id: language - weights: - preprocess_index: -50 - settings: { } datasources: - 'entity:entity_test' datasource_configs: { }