PHP操作Elasticsearch7.6

mengyue 2020-07-28

目录

首先打开Elasticsearch官网了解对应编程语言的API https://www.elastic.co/guide/en/elasticsearch/client/index.html

PHP操作Elasticsearch7.6

点击 PHP API即可查看当前7.X版本的文档内容了

安装操作Elasticsearch的PHP库

我们使用TP5来作为示例

首先需要安装操作Elasticsearch的PHP客户端库,我们打开https://packagist.org/,搜索Elasticsearch。

PHP操作Elasticsearch7.6

这里有个Elasticsearch-PHP和Elasticsearch版本的对照表,我们需要根据我们自己使用的Elasticsearch的版本下载对应的Elasticsearch-PHP

PHP操作Elasticsearch7.6

由于我的Elasticsearch版本是7.6.2,所以这里我们可以下载最新的Elasticsearch-PHP版本为7.8.0

PHP操作Elasticsearch7.6

我们进入到自己的项目目录里安装Elasticsearch-PHP

composer require elasticsearch/elasticsearch=7.8.*

PHP操作Elasticsearch7.6

PHP连接Elasticsearch

官方配置文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/configuration.html

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
var_dump($client);

创建索引和映射

创建一个名为users的索引同时创建映射,并制定映射中各个字段的类型

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    ‘index‘ => ‘users‘,
    ‘body‘ => [
        ‘settings‘ => [
            ‘number_of_shards‘ => 3,
            ‘number_of_replicas‘ => 2
        ],
        ‘mappings‘ => [
            ‘_source‘ => [
                ‘enabled‘ => true
            ],
            ‘properties‘ => [
                ‘name‘ => [
                    ‘type‘ => ‘keyword‘
                ],
                ‘age‘ => [
                    ‘type‘ => ‘integer‘
                ],
                ‘mobile‘ => [
                    ‘type‘ => ‘text‘
                ],
                ‘email‘ => [
                    ‘type‘ => ‘text‘
                ],
                ‘birthday‘ => [
                    ‘type‘ => ‘date‘
                ],
                ‘address‘ => [
                    ‘type‘ => ‘text‘
                ]
            ]
        ]
    ]
];


// Create the index with mappings and settings now
$response = $client->indices()->create($params);
dump($response);

PHP操作Elasticsearch7.6

添加文档

当你要在 Elasticsearch 增加文档时,你就需要索引 JSON 文档。JSON 文档会映射 PHP 关联数组,因为 PHP 关联数组可以 encode 为 JSON 数据格式。

因此在 Elasticsearch-PHP 中你可以传递关联数组给客户端来索引文档。我们会概述几种方法来增加文档到 Elasticsearch。

单一文档索引

当索引一个文档时,你可以提供一个 ID 或者让 Elasticsearch 自动生成。

现在有如下数据,我们将其添加到users索引中

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    ‘index‘ => ‘users‘,
    ‘id‘    => 1,
    ‘body‘  => [
        ‘name‘     => ‘张三‘,
        ‘age‘      => 10,
        ‘email‘    => ‘‘,
        ‘birthday‘ => ‘1990-12-12‘,
        ‘address‘  => ‘北京‘
    ]
];
$client->index($params);

通过Kibana可以查看到已经成功添加到Elasticsearch中

PHP操作Elasticsearch7.6

批量(bulk)索引

Elasticsearch 也支持批量(bulk)索引文档。bulk API 要求提供 JSON 格式的 action/元数据 键值对。在 PHP 中构建批量文档数据也是相似的。你首先要创建一个 action 数组对象(如 index 对象),然后你还要创建一个 body 对象。而 PHP 程序则重复上述操作构建文档数据。

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$arr = [
    [‘name‘ => ‘张三‘, ‘age‘ => 10, ‘email‘ => ‘‘, ‘birthday‘ => ‘1990-12-12‘, ‘address‘ => ‘北京‘],
    [‘name‘ => ‘李四‘, ‘age‘ => 20, ‘email‘ => ‘‘, ‘birthday‘ => ‘1990-10-15‘, ‘address‘ => ‘河南‘],
    [‘name‘ => ‘白兮‘, ‘age‘ => 15, ‘email‘ => ‘‘, ‘birthday‘ => ‘1970-08-12‘, ‘address‘ => ‘杭州‘],
    [‘name‘ => ‘王五‘, ‘age‘ => 25, ‘email‘ => ‘‘, ‘birthday‘ => ‘1980-12-01‘, ‘address‘ => ‘四川‘],
];

foreach ($arr as $key => $document) {
    $params[‘body‘][] = [
        ‘index‘ => [
            ‘_index‘ => ‘users‘,
            ‘_id‘    => $key
        ]
    ];

    $params[‘body‘][] = [
        ‘name‘     => $document[‘name‘],
        ‘age‘      => $document[‘age‘],
        ‘email‘    => $document[‘email‘],
        ‘birthday‘ => $document[‘birthday‘],
        ‘address‘  => $document[‘address‘]
    ];
}
if (isset($params) && !empty($params)) {
    $client->bulk($params);
}

PHP操作Elasticsearch7.6

如果数据量不多可以用上面的方法,如果数据量很多的话,我们就可以考虑分次添加

获取文档

Elasticsearch 提供实时获取文档的方法。这意味着只要文档被索引且客户端收到消息确认后,你就可以立即在任何的分片中检索文档。Get 操作通过 index/type/id 方式请求一个文档信息:

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    ‘index‘ => ‘users‘,
    ‘id‘    => 1
];
$response = $client->get($params);
dump($response);

PHP操作Elasticsearch7.6

更新文档

部分更新

如果你要部分更新文档(如更改现存字段,或添加新字段),你可以在 body 参数中指定一个 doc 参数。这样 doc 参数内的字段会与现存字段进行合并。

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    ‘index‘ => ‘users‘,
    ‘id‘    => 1,
    ‘body‘  => [
        ‘doc‘ => [
            ‘mobile‘ => ‘17612345678‘
        ]
    ]
];
$response = $client->update($params);
dump($response);

PHP操作Elasticsearch7.6

PHP操作Elasticsearch7.6

script更新

有时你要执行一个脚本来进行更新操作,如对字段进行自增操作或添加新字段。为了执行一个脚本更新,你要提供脚本命令和一些参数:

例如:将李四的年龄增加5岁

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    ‘index‘ => ‘users‘,
    ‘id‘    => ‘1‘,
    ‘body‘  => [
        ‘script‘ => ‘ctx._source.age += 5‘,
    ]
];
$response = $client->update($params);
dump($response);

PHP操作Elasticsearch7.6

通过Kibana查看发现年龄已经增加了5岁

PHP操作Elasticsearch7.6

删除文档

通过指定文档的 /index/type/id 路径可以删除文档:

$hosts = [
    ‘127.0.0.1:9200‘, //IP+端口
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    ‘index‘ => ‘users‘,
    ‘id‘    => 2,
];
$response = $client->delete($params);
dump($response);

PHP操作Elasticsearch7.6

相关推荐