索引管理

索引管理操作可以让你管理集群中的索引,例如创建、删除和更新索引和索引的映射/配置。

创建一个索引

索引操作包含在一个特定的命名空间内,与其它直接从属于客户端对象的方法隔离开来。让我们创建一个索引作为示例:

  1. $client = ClientBuilder::create()->build();
  2. $params = [
  3. 'index' => 'my_index'
  4. ];
  5. // Create the index
  6. $response = $client->indices()->create($params);

你可以在一个创建索引 API 中指定任何参数。所有的参数通常会注入请求体中的 body 参数下:

  1. $client = ClientBuilder::create()->build();
  2. $params = [
  3. 'index' => 'my_index',
  4. 'body' => [
  5. 'settings' => [
  6. 'number_of_shards' => 3,
  7. 'number_of_replicas' => 2
  8. ],
  9. 'mappings' => [
  10. 'my_type' => [
  11. '_source' => [
  12. 'enabled' => true
  13. ],
  14. 'properties' => [
  15. 'first_name' => [
  16. 'type' => 'string',
  17. 'analyzer' => 'standard'
  18. ],
  19. 'age' => [
  20. 'type' => 'integer'
  21. ]
  22. ]
  23. ]
  24. ]
  25. ]
  26. ];
  27. // Create the index with mappings and settings now
  28. $response = $client->indices()->create($params);

创建一个索引(复杂示例)

这是一个以更为复杂的方式创建索引的示例,示例中展示了如何定义 analyzers,tokenizers,filters 和索引的 settings。虽然创建方式与之前的示例本质一样,但是这个复杂示例对于理解客户端的使用方法具有莫大帮助,因为这种特定的语法结构很容易被混淆。

  1. $params = [
  2. 'index' => 'reuters',
  3. 'body' => [
  4. 'settings' => [
  5. 'number_of_shards' => 1,
  6. 'number_of_replicas' => 0,
  7. 'analysis' => [
  8. 'filter' => [
  9. 'shingle' => [
  10. 'type' => 'shingle'
  11. ]
  12. ],
  13. 'char_filter' => [
  14. 'pre_negs' => [
  15. 'type' => 'pattern_replace',
  16. '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',
  17. 'replacement' => '~$1 $2'
  18. ],
  19. 'post_negs' => [
  20. 'type' => 'pattern_replace',
  21. '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+)',
  22. 'replacement' => '$1 ~$2'
  23. ]
  24. ],
  25. 'analyzer' => [
  26. 'reuters' => [
  27. 'type' => 'custom',
  28. 'tokenizer' => 'standard',
  29. 'filter' => ['lowercase', 'stop', 'kstem']
  30. ]
  31. ]
  32. ]
  33. ],
  34. 'mappings' => [
  35. '_default_' => [
  36. 'properties' => [
  37. 'title' => [
  38. 'type' => 'string',
  39. 'analyzer' => 'reuters',
  40. 'term_vector' => 'yes',
  41. 'copy_to' => 'combined'
  42. ],
  43. 'body' => [
  44. 'type' => 'string',
  45. 'analyzer' => 'reuters',
  46. 'term_vector' => 'yes',
  47. 'copy_to' => 'combined'
  48. ],
  49. 'combined' => [
  50. 'type' => 'string',
  51. 'analyzer' => 'reuters',
  52. 'term_vector' => 'yes'
  53. ],
  54. 'topics' => [
  55. 'type' => 'string',
  56. 'index' => 'not_analyzed'
  57. ],
  58. 'places' => [
  59. 'type' => 'string',
  60. 'index' => 'not_analyzed'
  61. ]
  62. ]
  63. ],
  64. 'my_type' => [
  65. 'properties' => [
  66. 'my_field' => [
  67. 'type' => 'string'
  68. ]
  69. ]
  70. ]
  71. ]
  72. ]
  73. ];
  74. $client->indices()->create($params);

删除一个索引

删除一个索引十分简单:

  1. $params = ['index' => 'my_index'];
  2. $response = $client->indices()->delete($params);

Put Settings API

Put Settings API 允许你更改索引的配置参数:

  1. $params = [
  2. 'index' => 'my_index',
  3. 'body' => [
  4. 'settings' => [
  5. 'number_of_replicas' => 0,
  6. 'refresh_interval' => -1
  7. ]
  8. ]
  9. ];
  10. $response = $client->indices()->putSettings($params);

Get Settings API

Get Settings API 可以让你知道一个或多个索引的当前配置参数:

  1. // Get settings for one index
  2. $params = ['index' => 'my_index'];
  3. $response = $client->indices()->getSettings($params);
  4. // Get settings for several indices
  5. $params = [
  6. 'index' => [ 'my_index', 'my_index2' ]
  7. ];
  8. $response = $client->indices()->getSettings($params);

Put Mappings API

Put Mappings API 允许你更改或增加一个索引的映射。

  1. // Set the index and type
  2. $params = [
  3. 'index' => 'my_index',
  4. 'type' => 'my_type2',
  5. 'body' => [
  6. 'my_type2' => [
  7. '_source' => [
  8. 'enabled' => true
  9. ],
  10. 'properties' => [
  11. 'first_name' => [
  12. 'type' => 'string',
  13. 'analyzer' => 'standard'
  14. ],
  15. 'age' => [
  16. 'type' => 'integer'
  17. ]
  18. ]
  19. ]
  20. ]
  21. ];
  22. // Update the index mapping
  23. $client->indices()->putMapping($params);

Get Mappings API

Get Mappings API 返回索引和类型的映射细节。你可以指定一些索引和类型,取决于你希望检索什么映射。

  1. // Get mappings for all indexes and types
  2. $response = $client->indices()->getMapping();
  3. // Get mappings for all types in 'my_index'
  4. $params = ['index' => 'my_index'];
  5. $response = $client->indices()->getMapping($params);
  6. // Get mappings for all types of 'my_type', regardless of index
  7. $params = ['type' => 'my_type' ];
  8. $response = $client->indices()->getMapping($params);
  9. // Get mapping 'my_type' in 'my_index'
  10. $params = [
  11. 'index' => 'my_index'
  12. 'type' => 'my_type'
  13. ];
  14. $response = $client->indices()->getMapping($params);
  15. // Get mappings for two indexes
  16. $params = [
  17. 'index' => [ 'my_index', 'my_index2' ]
  18. ];
  19. $response = $client->indices()->getMapping($params);

索引命名空间下的其他 API

索引命名空间下还有一些 API 允许你管理你的索引(add/remove templates, flush segments, close indexes等)。

如果你使用一个自动检索的 IDE,你应该可以轻易发现索引的命名空间:

  1. $client->indices()->

这里可以查看可用方法清单。而浏览 \Elasticsearch\Namespaces\Indices.php 文件则会看到所有可调用的方法清单。