索引管理操作
编辑索引管理操作
编辑索引管理操作允许您管理 Elasticsearch 集群中的索引,例如创建、删除和更新索引及其映射/设置。
创建索引
编辑索引操作都包含在一个单独的命名空间中,与根客户端对象上的其他方法分开。例如,让我们创建一个新的索引
$client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index' ]; // Create the index $response = $client->indices()->create($params);
您可以指定新索引创建 API 中通常包含的任何参数。所有通常位于请求体中的参数都位于 body 参数中
$client = ClientBuilder::create()->build(); $params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_shards' => 3, 'number_of_replicas' => 2 ], 'mappings' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'first_name' => [ 'type' => 'keyword' ], 'age' => [ 'type' => 'integer' ] ] ] ] ]; // Create the index with mappings and settings now $response = $client->indices()->create($params);
创建索引(高级示例)
编辑这是一个更复杂的创建索引的示例,展示了如何定义分析器、分词器、过滤器和索引设置。尽管从本质上讲与前面的示例相同,但更复杂的示例对于客户端的“现实世界”使用可能会有所帮助,因为这种特定的语法很容易出错。
$params = [ 'index' => 'reuters', 'body' => [ 'settings' => [ 'number_of_shards' => 1, 'number_of_replicas' => 0, 'analysis' => [ 'filter' => [ 'shingle' => [ 'type' => 'shingle' ] ], 'char_filter' => [ 'pre_negs' => [ 'type' => 'pattern_replace', 'pattern' => '(\\w+)\\s+((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\b', 'replacement' => '~$1 $2' ], 'post_negs' => [ 'type' => 'pattern_replace', 'pattern' => '\\b((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\s+(\\w+)', 'replacement' => '$1 ~$2' ] ], 'analyzer' => [ 'reuters' => [ 'type' => 'custom', 'tokenizer' => 'standard', 'filter' => ['lowercase', 'stop', 'kstem'] ] ] ] ], 'mappings' => [ 'properties' => [ 'title' => [ 'type' => 'text', 'analyzer' => 'reuters', 'copy_to' => 'combined' ], 'body' => [ 'type' => 'text', 'analyzer' => 'reuters', 'copy_to' => 'combined' ], 'combined' => [ 'type' => 'text', 'analyzer' => 'reuters' ], 'topics' => [ 'type' => 'keyword' ], 'places' => [ 'type' => 'keyword' ] ] ] ] ]; $client->indices()->create($params);
顶层 |
|
|
|
|
删除索引
编辑删除索引非常简单
$params = ['index' => 'my_index']; $response = $client->indices()->delete($params);
PUT 设置 API
编辑PUT 设置 API 允许您修改任何动态的索引设置
$params = [ 'index' => 'my_index', 'body' => [ 'settings' => [ 'number_of_replicas' => 0, 'refresh_interval' => -1 ] ] ]; $response = $client->indices()->putSettings($params);
GET 设置 API
编辑GET 设置 API 显示一个或多个索引的当前配置设置
// Get settings for one index $params = ['index' => 'my_index']; $response = $client->indices()->getSettings($params); // Get settings for several indices $params = [ 'index' => [ 'my_index', 'my_index2' ] ]; $response = $client->indices()->getSettings($params);
PUT 映射 API
编辑PUT 映射 API 允许您修改或添加到现有索引的映射。
// Set the index and type $params = [ 'index' => 'my_index', 'body' => [ '_source' => [ 'enabled' => true ], 'properties' => [ 'first_name' => [ 'type' => 'text', 'analyzer' => 'standard' ], 'age' => [ 'type' => 'integer' ] ] ] ]; // Update the index mapping $client->indices()->putMapping($params);
GET 映射 API
编辑GET 映射 API 返回有关您的索引的映射详细信息。根据您希望检索的映射,您可以指定一个或多个索引
// Get mappings for all indices $response = $client->indices()->getMapping(); // Get mappings in 'my_index' $params = ['index' => 'my_index']; $response = $client->indices()->getMapping($params); // Get mappings for two indices $params = [ 'index' => [ 'my_index', 'my_index2' ] ]; $response = $client->indices()->getMapping($params);
indices 命名空间中的其他 API
编辑indices 命名空间中还有许多其他 API 允许您管理 Elasticsearch 索引(添加/删除模板、刷新段、关闭索引等)。
如果您使用带有自动完成功能的 IDE,您应该能够通过键入以下内容轻松浏览 indices 命名空间
$client->indices()->
并浏览可用方法列表。或者,浏览 \Elasticsearch\Namespaces\Indices.php
文件将显示可用方法调用的完整列表(以及每个方法注释中的参数列表)。