本文整理汇总了PHP中Elasticsearch\Client::putMapping方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::putMapping方法的具体用法?PHP Client::putMapping怎么用?PHP Client::putMapping使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elasticsearch\Client
的用法示例。
在下文中一共展示了Client::putMapping方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: addMapping
/**
* 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);
}