PHP操做Elasticsearch7.6

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

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

安装操做Elasticsearch的PHP库

咱们使用TP5来做为示例shell

首先须要安装操做Elasticsearch的PHP客户端库,咱们打开https://packagist.org/,搜索Elasticsearch。编程

这里有个Elasticsearch-PHP和Elasticsearch版本的对照表,咱们须要根据咱们本身使用的Elasticsearch的版本下载对应的Elasticsearch-PHPapi

因为个人Elasticsearch版本是7.6.2,因此这里咱们能够下载最新的Elasticsearch-PHP版本为7.8.0数组

咱们进入到本身的项目目录里安装Elasticsearch-PHPapp

composer require elasticsearch/elasticsearch=7.8.*

PHP链接Elasticsearch

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

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

建立索引和映射

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

$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);

添加文档

当你要在 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'    => 'zs@gmail.com',
        'birthday' => '1990-12-12',
        'address'  => '北京'
    ]
];
$client->index($params);

经过Kibana能够查看到已经成功添加到Elasticsearch中

批量(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' => 'zs@gmail.com', 'birthday' => '1990-12-12', 'address' => '北京'],
    ['name' => '李四', 'age' => 20, 'email' => 'ls@gmail.com', 'birthday' => '1990-10-15', 'address' => '河南'],
    ['name' => '白兮', 'age' => 15, 'email' => 'bx@gmail.com', 'birthday' => '1970-08-12', 'address' => '杭州'],
    ['name' => '王五', 'age' => 25, 'email' => 'ww@gmail.com', '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);
}

若是数据量很少能够用上面的方法,若是数据量不少的话,咱们就能够考虑分次添加

获取文档

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);

更新文档

部分更新

若是你要部分更新文档(如更改现存字段,或添加新字段),你能够在 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);

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);

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

删除文档

经过指定文档的 /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);

若是该文章对您有帮助,请您点个推荐,感谢。

相关文章
相关标签/搜索