索引管理操作
编辑索引管理操作编辑
索引管理操作允许您管理 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);
索引命名空间中的其他 API编辑
索引命名空间中还有许多其他 API 允许您管理 Elasticsearch 索引(添加/删除模板、刷新段、关闭索引等)。
如果您使用具有自动完成功能的 IDE,您应该能够通过键入以下内容轻松浏览索引命名空间
$client->indices()->
并浏览可用方法列表。或者,浏览 \Elasticsearch\Namespaces\Indices.php
文件将显示您所有可用方法调用的完整列表(以及每个方法的注释中的参数列表)。