Skip to content
Snippets Groups Projects
Commit d6bfca93 authored by Robert Phillips's avatar Robert Phillips
Browse files

Issue #3285438: Prevent index clear when field mapping hasn't changed.

parent 49da79d6
No related branches found
No related tags found
No related merge requests found
Pipeline #90097 failed
......@@ -269,7 +269,6 @@ class BackendClient implements BackendClientInterface {
*/
public function updateIndex(IndexInterface $index): void {
if ($this->indexExists($index)) {
$index->clear();
$this->updateSettings($index);
$this->updateFieldMapping($index);
}
......@@ -288,14 +287,58 @@ class BackendClient implements BackendClientInterface {
* Thrown when an underlying OpenSearch error occurs.
*/
public function updateFieldMapping(IndexInterface $index): void {
$indexId = $this->getIndexId($index);
try {
$params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index);
$this->client->indices()->putMapping($params);
if ($this->indexFieldsUpdated($index)) {
$indexId = $this->getIndexId($index);
try {
$params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index);
$this->client->indices()->putMapping($params);
$index->reindex();
}
catch (OpenSearchException $e) {
throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e);
}
}
catch (OpenSearchException $e) {
throw new SearchApiException(sprintf('An error occurred updating field mappings for index %s.', $indexId), 0, $e);
}
/**
* Checks if the recently updated index had any fields changed.
*
* @param \Drupal\search_api\IndexInterface $index
* The index that was just updated.
*
* @return bool
* TRUE if any of the fields were updated, FALSE otherwise.
*/
protected function indexFieldsUpdated(IndexInterface $index): bool {
if (!isset($index->original)) {
return TRUE;
}
/** @var \Drupal\search_api\IndexInterface $original */
$original = $index->original;
$old_fields = $original->getFields();
$new_fields = $index->getFields();
if (!$old_fields && !$new_fields) {
return FALSE;
}
if (array_diff_key($old_fields, $new_fields) || array_diff_key($new_fields, $old_fields)) {
return TRUE;
}
foreach ($new_fields as $field) {
if (!isset($old_fields[$field->getFieldIdentifier()])) {
continue;
}
$old_field = $old_fields[$field->getFieldIdentifier()];
if ($field->getSettings() != $old_field->getSettings()) {
return TRUE;
}
if ($field->getConfiguration() != $old_field->getConfiguration()) {
return TRUE;
}
}
return FALSE;
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment