Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to EP mapping properties instead of overwriting them #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 78 additions & 12 deletions src/Base/ElasticPress/ElasticPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,99 @@ 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['mappings']['properties'] = [
'expired' => [
'type' => 'object',
'properties' => [
'on' => [
'type' => 'object',
'enabled' => 'false'
]
$mapping_version = $this->getElasticPressMappingVersion();

if ($mapping_version == 5) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Een extra =

 if ($mappingVersion === 5) {

Copy link
Contributor

@SimonvanWijhe SimonvanWijhe Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graag variabelen in camelCase voor consistentie met de rest van het project.

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',
'properties' => [
'on' => [
'type' => 'object',
'enabled' => 'false'
]
],
'post_date_gmt' => [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss',
];

$mapping['mappings']['post']['properties']['post_date_gmt'] = [
'type' => 'date',
'format' => 'yyyy-MM-dd HH:mm:ss',
];

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.
*/
Expand Down