Skip to content

Commit

Permalink
Merge pull request #8 from gdubost/v2
Browse files Browse the repository at this point in the history
Elasticsearch7.5 : Suppression des types dans les mappings
  • Loading branch information
Guillaume DUBOST authored Jan 7, 2020
2 parents 033c151 + 7621c22 commit 08fdb08
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 63 deletions.
19 changes: 11 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"name": "etna/elasticsearch-service-provider",
"description": "ElasticSearch Service Provider",
"keywords": ["elasticsearch", "silex"],
"keywords": [
"elasticsearch",
"silex"
],
"license": "proprietary",
"authors": [
{
Expand All @@ -11,9 +14,9 @@
}
],
"require": {
"php": ">=5.5",
"php": ">=7.1",
"silex/silex": "~2.0@dev",
"elasticsearch/elasticsearch": "~2.0"
"elasticsearch/elasticsearch": "^7.4"
},
"config": {
"bin-dir": "bin"
Expand All @@ -29,9 +32,9 @@
}
},
"repositories": [
{
"type": "composer",
"url": "http://blu-composer.herokuapp.com"
}
]
{
"type": "composer",
"url": "https://blu-composer.herokuapp.com"
}
]
}
56 changes: 1 addition & 55 deletions src/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,11 @@ public function __construct(array $es_options = null)
foreach ($es_options as $db_name) {
$name = strtoupper($db_name);
$elasicsearch_host = getenv("{$name}_ELASTICSEARCH_HOST");
$elasticsearch_types = getenv("{$name}_ELASTICSEARCH_TYPES");
if (false === $elasicsearch_host) {
throw new \Exception("{$name}_ELASTICSEARCH_HOST doesn't exist");
}
if (false === $elasticsearch_types) {
throw new \Exception("{$name}_ELASTICSEARCH_TYPES doesn't exist");

}
$this->es_options[$db_name] = [
"host" => $elasicsearch_host,
"types" => explode(',', $elasticsearch_types)
"host" => $elasicsearch_host,
];
}
}
Expand Down Expand Up @@ -69,15 +63,13 @@ public function register(Container $app)

$app["elasticsearch.{$name}.server"] = str_replace($parsed_url['path'], '', $es_option['host']) . "/";
$app["elasticsearch.{$name}.index"] = $index;
$app["elasticsearch.{$name}.types"] = $es_option['types'];

$app["elasticsearch.{$name}"] = ClientBuilder::create()
->setHosts([$app["elasticsearch.{$name}.server"]])
->build();
}

$app['elasticsearch.create_index'] = [$this, 'createIndex'];
$app["elasticsearch.create_type"] = [$this, 'createType'];
$app['elasticsearch.lock'] = [$this, 'lock'];
$app['elasticsearch.unlock'] = [$this, 'unlock'];
}
Expand Down Expand Up @@ -131,52 +123,6 @@ public function createIndex($name, $reset = false)
// Rajout de l'alias
$app["elasticsearch.{$name}"]->indices()->putAlias($alias);
echo "Index {$app["elasticsearch.$name.index"]} created successfully!\n\n";

foreach ($app["elasticsearch.{$name}.types"] as $type) {
self::createType($name, $type, $reset);
}
}
}

public function createType($name, $type, $reset = false)
{
$app = $this->app;

if (!in_array($name, $app["elasticsearch.names"])) {
throw new \Exception("Application is not configured for index {$name}");
}

if (!in_array($type, $app["elasticsearch.{$name}.types"])) {
throw new \Exception("Application is not configured for type {$app["elasticsearch.$name.index"]}/{$type}");
}

if (true === $reset) {
echo "\nCreating elasticsearch type {$type} for index {$app["elasticsearch.$name.index"]}\n";

$parameters_path = $app["elasticsearch_{$name}_parameters_path"];
if (!file_exists("{$parameters_path}/{$type}-mapping.json")) {
throw new \Exception("Mapping file for type {$type} does not exist");
}
$mapping = json_decode(file_get_contents("{$parameters_path}/{$type}-mapping.json"), true);

$this->unlock($name);

try {
$app["elasticsearch.{$name}"]->indices()->deleteMapping([
"index" => $app["elasticsearch.{$name}.index"],
"type" => $type
]);
} catch (\Exception $exception) {
echo "Type {$app["elasticsearch.$name.index"]}/{$type} doesn't exist... \n";
}

$app["elasticsearch.{$name}"]->indices()->putMapping([
"index" => $app["elasticsearch.{$name}.index"],
"type" => $type,
"body" => $mapping
]);

echo "Type {$app["elasticsearch.$name.index"]}/{$type} created successfully!\n\n";
}
}

Expand Down

0 comments on commit 08fdb08

Please sign in to comment.