From 35438d634789e2e90e2439e0928c3398f0565bca Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Tue, 14 Sep 2021 15:50:34 +0200 Subject: [PATCH 1/3] Add to the mapping properties instead of overwriting them The properties key of the mappings key in the mapping array already has values provided by the ElasticPress mapping. In the old situation, that entire key was overwritten, instead of adding the custom mappings to the properties. --- src/Base/ElasticPress/ElasticPress.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Base/ElasticPress/ElasticPress.php b/src/Base/ElasticPress/ElasticPress.php index 355e298..a9e47f0 100644 --- a/src/Base/ElasticPress/ElasticPress.php +++ b/src/Base/ElasticPress/ElasticPress.php @@ -232,20 +232,19 @@ protected function formatOutputForElasticsearch(array $item): array */ public function addMappings(array $mapping): array { - $mapping['mappings']['properties'] = [ - 'expired' => [ - 'type' => 'object', - 'properties' => [ - 'on' => [ - 'type' => 'object', - 'enabled' => 'false' - ] + $mapping['mappings']['properties']['expired'] = [ + 'type' => 'object', + 'properties' => [ + 'on' => [ + 'type' => 'object', + 'enabled' => 'false' ] ], - 'post_date_gmt' => [ - 'type' => 'date', - 'format' => 'yyyy-MM-dd HH:mm:ss', - ], + ]; + + $mapping['mappings']['properties']['post_date_gmt'] = [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss', ]; return $mapping; From f744b651a6fc11cf52a0259ae192facd4de11f89 Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Mon, 21 Mar 2022 13:27:51 +0100 Subject: [PATCH 2/3] Set extra properties on right post key of ES mapping --- src/Base/ElasticPress/ElasticPress.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Base/ElasticPress/ElasticPress.php b/src/Base/ElasticPress/ElasticPress.php index 22a077f..48e9b2a 100644 --- a/src/Base/ElasticPress/ElasticPress.php +++ b/src/Base/ElasticPress/ElasticPress.php @@ -232,7 +232,7 @@ protected function formatOutputForElasticsearch(array $item): array */ public function addMappings(array $mapping): array { - $mapping['mappings']['properties']['expired'] = [ + $mapping['mappings']['post']['properties']['expired'] = [ 'type' => 'object', 'properties' => [ 'on' => [ @@ -242,7 +242,7 @@ public function addMappings(array $mapping): array ], ]; - $mapping['mappings']['properties']['post_date_gmt'] = [ + $mapping['mappings']['post']['properties']['post_date_gmt'] = [ 'type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss', ]; From 44f7fdc5a34f86963c7ac3498207362e564c6cdc Mon Sep 17 00:00:00 2001 From: Coen Jacobs Date: Mon, 21 Mar 2022 15:01:49 +0100 Subject: [PATCH 3/3] Use different mapping format based on ES version --- src/Base/ElasticPress/ElasticPress.php | 69 +++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Base/ElasticPress/ElasticPress.php b/src/Base/ElasticPress/ElasticPress.php index 48e9b2a..9a7ed68 100644 --- a/src/Base/ElasticPress/ElasticPress.php +++ b/src/Base/ElasticPress/ElasticPress.php @@ -224,13 +224,39 @@ protected function formatOutputForElasticsearch(array $item): array } /** - * Alter the mappings. + * Alter the mappings of all OpenPub document fields. This needs to be + * available in two different formats, which can be determined based on the + * ElasticPress version. * * @param array $mapping * * @return array */ public function addMappings(array $mapping): array + { + $mapping_version = $this->getElasticPressMappingVersion(); + + if ($mapping_version == 5) { + return $this->getMappingByVersion5($mapping); + } elseif ($mapping_version === 7) { + return $this->getMappingByVersion7($mapping); + } else { + // In case we're unable to determine the EP version, we abort to + // prevent errors in these edge case scenarios. This returns the + // original mappings that aren't changed. + return $mapping; + } + + return $mapping; + } + + /** + * Provides the extra mappings in the format expected by the ElasticPress + * version 5.x format. This includes the 'post' key under 'mappings' that + * is removed in the 7.x format. This needs to stay for legacy reasons, as + * ElasticPress still supports this legacy format. + */ + private function getMappingByVersion5(array $mapping) { $mapping['mappings']['post']['properties']['expired'] = [ 'type' => 'object', @@ -250,6 +276,47 @@ public function addMappings(array $mapping): array return $mapping; } + /** + * Provides the extra mappings in the format expected by the ElasticPress + * version 7.x format. The main difference between these formats is the + * lack of the 'post' key under 'mappings', which is omitted in the new + * mapping format going forward. + */ + private function getMappingByVersion7(array $mapping) + { + $mapping['mappings']['properties']['expired'] = [ + 'type' => 'object', + 'properties' => [ + 'on' => [ + 'type' => 'object', + 'enabled' => 'false' + ] + ], + ]; + + $mapping['mappings']['properties']['post_date_gmt'] = [ + 'type' => 'date', + 'format' => 'yyyy-MM-dd HH:mm:ss', + ]; + + return $mapping; + } + + private function getElasticPressMappingVersion(): int + { + $es_version = \ElasticPress\Elasticsearch::factory()->get_elasticsearch_version(); + + if (empty($es_version)) { + return 0; + } + + if (version_compare($es_version, '7.0', '>=')) { + return 7; + } else { + return 5; + } + } + /** * Define all the necessary settings. */