/** * Adds a new type to an index with the mappings. Index and type will be created if they do not exist. * * @param string $index The name of the index the mapping will be added to. * @param string $name The name of the mapping/type you are creating. * @param array $mapping An array of mapping properties that define the mapping/type. * * @return array like ['acknowledged' => (Boolean)]; * * @throws ProtectedIndexException When $index starts with 'hercules-'. * @throws UsageException When $index, $name, or $mapping are empty/null. */ public function addMapping($index, $name, array $mapping) { $errorMessages = []; if (empty($index)) { throw new UsageException('$index cannot be null.'); } //Block mappings from being added to any hercules-* index new or existing. if (preg_match('/^(hercules-)/', $index, $matches)) { throw new ProtectedIndexException("Mappings cannot be added to protected {$matches}* Indices."); } //Attempt to create the index. If the index already exists an indexAlreadyExists Exception is thrown try { $response = $this->client->create($index); if ($response['acknowledged'] != true) { $errorMessages[] = "The request to create {$index} was not acknowledged."; } } catch (\Exception $e) { //Report error message only if it was not an error stating the index already existed. //IndexAlreadyExistsException is one of ES's custom exceptions. if (get_class($e) === 'IndexAlreadyExistsException') { $errorMessages[] = "Failed to create {$index} with error {$e->getMessage()}."; } } if (empty($name)) { $errorMessages[] = '$name cannot = null.'; } if (empty($mapping)) { $errorMessages[] = "An empty mapping cannot be created. That doesn't make any sense."; } if (!empty($errorMessages)) { throw new UsageException(implode("\n", $errorMessages)); } $req = ['index' => $index, 'type' => $name, 'body' => $mapping]; return $this->client->putMapping($req); }