From 9b8818958caec2ca5f1d636977a993b19b10d61f Mon Sep 17 00:00:00 2001 From: prog-24 Date: Fri, 28 Sep 2018 00:43:12 +0100 Subject: [PATCH] Update ElasticquentTrait.php putMapping fails to update mapping if the index was already created. This change allows you to create mappings after the fact --- src/ElasticquentTrait.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index de56e32..e5f719e 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -437,6 +437,36 @@ public static function getMapping() return $instance->getElasticSearchClient()->indices()->getMapping($params); } + + /** + * Create Mapping. + * Use if you want to create mappings after the index has already being created. + *@return array + */ + public static function createMapping() + { + $instance = new static; + + $mappings = $instance->getMappingProperties(); + + if(!is_null($mappings)) { + + $index = array( + 'index' => $instance->getIndexName(), + ); + + $settings = $instance->getIndexSettings(); + if (!is_null($settings)) { + $index['body']['settings'] = $settings; + } + $index['body']['properties'] = $mappings; + $index['type'] = $instance->getTypeName(); + + return $instance->getElasticSearchClient()->indices()->putMapping($index); + } + + return false; + } /** * Put Mapping.