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

创建一个索引

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

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


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

在这里插入图片描述
指定参数创建索引

$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的数据类型
分析器:分词器
在这里插入图片描述
查看映射信息

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

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

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

在这里插入图片描述
查看多个索引映射信息

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

查看索引信息

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

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

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

在这里插入图片描述
修改索引

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

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

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

修改前
在这里插入图片描述
修改后
在这里插入图片描述
删除索引

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

$params = ['index' => 'my_index'];
$response = $client->indices()->delete($params);
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值