Skip to content
Snippets Groups Projects
Commit 483d5ad9 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 #90083 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);
}
......@@ -291,6 +290,9 @@ class BackendClient implements BackendClientInterface {
$indexId = $this->getIndexId($index);
try {
$params = $this->fieldParamsBuilder->mapFieldParams($indexId, $index);
if ($this->checkFieldMapping($index, $params['body']['properties'])) {
$index->clear();
}
$this->client->indices()->putMapping($params);
}
catch (OpenSearchException $e) {
......@@ -298,6 +300,43 @@ class BackendClient implements BackendClientInterface {
}
}
/**
* Check if field mapping has changed.
*
* @param \Drupal\search_api\IndexInterface $index
* The index.
* @param array $properties
* The array of field mapping properties.
*
* @return bool
* Returns TRUE or FALSE.
*/
protected function checkFieldMapping(IndexInterface $index, array $properties): bool {
$indexId = $this->getIndexId($index);
$mapping = $this->client->indices()->getMapping(['index' => $indexId]);
// Missing original properties mapping.
if (!isset($mapping[$indexId]['mappings']['properties'])) {
return TRUE;
}
// Property has been added or removed.
if (array_diff_key($properties, $mapping[$indexId]['mappings']['properties'])) {
return TRUE;
}
// Properties have changed or are missing.
foreach ($properties as $name => $property) {
foreach ($mapping[$indexId]['mappings']['properties'][$name] as $key => $value) {
if (!isset($property[$key]) || $property[$key] !== $value) {
return TRUE;
}
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
......
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