Elasticsearch-PHP 中文文档 管理索引

verne · 2021-05-20 12:44:48 · 热度: 17

索引管理

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

创建一个索引

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

$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' => [
            'my_type' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'first_name' => [
                        'type' => 'keyword',
                        'analyzer' => 'standard'
                    ],
                    'age' => [
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];

// 创建带有配置信息和映射的索引
$response = $client->indices()->create($params);

创建一个索引 (高级示例)

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

$params = [
    'index' => 'reuters',
    'body' => [
        'settings' => [ (1)
            'number_of_shards' => 1,
            'number_of_replicas' => 0,
            'analysis' => [ (2)
                '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' => [ (3)
            '_default_' => [    (4)
                'properties' => [
                    'title' => [
                        'type' => 'keyword',
                        'analyzer' => 'reuters',
                        'term_vector' => 'yes',
                        'copy_to' => 'combined'
                    ],
                    'body' => [
                        'type' => 'keyword',
                        'analyzer' => 'reuters',
                        'term_vector' => 'yes',
                        'copy_to' => 'combined'
                    ],
                    'combined' => [
                        'type' => 'keyword',
                        'analyzer' => 'reuters',
                        'term_vector' => 'yes'
                    ],
                    'topics' => [
                        'type' => 'keyword',
                        'index' => 'not_analyzed'
                    ],
                    'places' => [
                        'type' => 'keyword',
                        'index' => 'not_analyzed'
                    ]
                ]
            ],
            'my_type' => [  (5)
                'properties' => [
                    'my_field' => [
                        'type' => 'keyword'
                    ]
                ]
            ]
        ]
    ]
];
$client->indices()->create($params);
  1. 顶级 settings 包含有关索引(分片数量等)以及分析器的配置
  2. analysis 嵌套在 settings 中,并包含标记器,过滤器,字符过滤器和分析器
  3. mappings 是嵌套在 settings 中的另一个元素,并且包含各种类型的映射
  4. *default* 是一个动态模板,应用于没有显式映射的所有字段
  5. my_type 类型是一个用户定义的类型示例,它拥有一个字段 my_field

删除索引

删除索引非常简单:

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

设置 API

Put Settings API 允许您修改任何动态的索引设置:

$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_replicas' => 0,
            'refresh_interval' => -1
        ]
    ]
];

$response = $client->indices()->putSettings($params);

获取设置 API

Get Settings API 将会显示一个或多个当前索引的配置

// 获取一个索引的配置信息
$params = ['index' => 'my_index'];
$response = $client->indices()->getSettings($params);

// 获取多个索引的配置信息
$params = [
    'index' => [ 'my_index', 'my_index2' ]
];
$response = $client->indices()->getSettings($params);

映射 API

Put Mappings API允许您修改或添加现有索引的映射。

// 设置索引和类型
$params = [
    'index' => 'my_index',
    'type' => 'my_type2',
    'body' => [
        'my_type2' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'first_name' => [
                    'type' => 'keyword',
                    'analyzer' => 'standard'
                ],
                'age' => [
                    'type' => 'integer'
                ]
            ]
        ]
    ]
];

// 更新索引映射
$client->indices()->putMapping($params);

获取映射 API

Get Mappings API 将返回有关索引和类型映射的详细信息。 根据您要检索的映射,您可以指定索引和类型的多种组合:

// 获取所有索引和类型的映射
$response = $client->indices()->getMapping();

// 获取 'my_index' 中的所有类型的映射
$params = ['index' => 'my_index'];
$response = $client->indices()->getMapping($params);

// 获取所有名为 'my_type' 的类型映射, 而不考虑索引
$params = ['type' => 'my_type' ];
$response = $client->indices()->getMapping($params);

// 获取 'my_index' 中的 'my_type' 的映射
$params = [
    'index' => 'my_index'
    'type' => 'my_type'
];
$response = $client->indices()->getMapping($params);

// 获取两个索引的映射
$params = [
    'index' => [ 'my_index', 'my_index2' ]
];
$response = $client->indices()->getMapping($params);

Indices 命名空间中的其他 API

indices 命名空间中有许多 API,允许您管理 elasticsearch 索引 (添加/删除模板,刷新,关闭索引, 等).

如果您使用自动完成功能的 IDE ,则应该可以通过键入以下内容轻松浏览 indices 命名空间:

$client->indices()->

仔细阅读可用方法列表。 或者, 浏览 \Elasticsearch\Namespaces\Indices.php 文件将显示可用方法调用的完整列表(以及每个方法的注释中的参数列表)。

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册