Elasticsearch-PHP(二) - 索引的增删改查

创建一个索引

setHosts($hosts)
                                ->build();

$params = [
    'index' => 'my_index'
];


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

Elasticsearch-PHP(二) - 索引的增删改查_第1张图片
指定参数创建索引

$params = [
    'index' => 'my_index2', // 索引名
    'body' => [
        'settings' => [ // 分片和副本数
            'number_of_shards' => 3, // 分片
            'number_of_replicas' => 0 // 副本,如果只有一台机器,设置为0
        ],
        'mappings' => [ // 映射
            'my_type' => [ // 类型
                '_source' => [
                    'enabled' => true // 开启即可,否则某些功能不可用
                ],
                'properties' => [ // 指定字段的类型或字段属性
                    'first_name' => [ // 字段
                        'type' => 'text', // 数据类型
                        'analyzer' => 'standard' // 分析器
                    ],
                    'age' => [ // 同上
                        'type' => 'integer'
                    ]
                ]
            ]
        ]
    ]
];

索引名:类似mysql的库
分片:类似mysql的表分区
副本:类似mysql的从库(备库)
映射:类似mysql的字段数据类型,类型说明
类型:类似mysql的表
_source:说明
字段:类似mysql的字段
数据类型:类似mysql的数据类型
分析器:分词器
Elasticsearch-PHP(二) - 索引的增删改查_第2张图片
查看映射信息

setHosts($hosts)
                                ->build();

$params = [
    'index' => 'my_index2',
    'type' => 'my_type'
];

$response = $client->indices()->getMapping($params);
var_dump($response);

Elasticsearch-PHP(二) - 索引的增删改查_第3张图片
查看多个索引映射信息

$params = [
    'index' => ['my_index', 'my_index2']
];

查看索引信息

setHosts($hosts)
                                ->build();

$params = [
    'index' => ['my_index', 'my_index2']
];

$response = $client->indices()->getSettings($params);
var_dump($response);

Elasticsearch-PHP(二) - 索引的增删改查_第4张图片
修改索引

setHosts($hosts)
                                ->build();

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

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

修改前
Elasticsearch-PHP(二) - 索引的增删改查_第5张图片
修改后
Elasticsearch-PHP(二) - 索引的增删改查_第6张图片
删除索引

setHosts($hosts)
                                ->build();

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

你可能感兴趣的:(Elasticsearch)