Elasticsearch-PHP 中文文档 快速上手

topacio · 2021-05-20 12:35:46 · 热度: 81

快速开始

这一节会概述一下客户端的安装,还有一些主要方法的使用规则。

安装

  • composer.json 文件中引入 elasticsearch-php:

    {
        "require": {
            "elasticsearch/elasticsearch": "~6.0"
        }
    }
    
  • 安装 Composer:

    curl -s http://getcomposer.org/installer | php
    php composer.phar install --no-dev
    
  • 在项目中引入自动加载文件,并且实例化一个客户端:

     require 'vendor/autoload.php';
    
     use Elasticsearch\ClientBuilder;
    
     $client = ClientBuilder::create()->build();
    

索引一个文档

在 elasticsearch-php 中,几乎一切操作都是用关联数组来配置,其中包括 REST 路径(endpoint)、文档和可选参数等。

为了索引一个文档,我们要指定4部分信息:index,type,id 和一个文档的 body。构建一个键值对的关联数组就可以完成上面的内容。body 一般是键值对的数组内容,格式与原文档的数据保持一致即可:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
    'body' => ['testField' => 'abc']
];

$response = $client->index($params);
print_r($response);

收到的响应数据表明,你指定的索引中已经创建好了文档。响应数据是一个关联数组,里面的内容是 Elasticsearch 返回的 decoded JSON 数组格式的数据:

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [created] => 1
)

获取一个文档

现在获取刚才索引的文档:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->get($params);
print_r($response);

响应数据包含一些元数据(如 index,type 等)和 _source 字段, 这是我们上面发送给 Elasticsearch 的原始文档数据:

Array
(
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 1
    [found] => 1
    [_source] => Array
        (
            [testField] => abc
        )

)

搜索一个文档

搜索是 elasticsearch 的主推功能,接下来我们试一下执行一个搜索。我们准备用 Match 查询来作为示范:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'match' => [
                'testField' => 'abc'
            ]
        ]
    ]
];

$response = $client->search($params);
print_r($response);

这个响应数据与前面例子的响应数据有所不同。新增了一些元数据(如 took, timed_out 等)和一个 hits 的数组,这代表了你的搜索结果。而 hits 内部也有一个 hits 数组,内部的 hits 包含单个的搜索结果:

Array
(
    [took] => 1
    [timed_out] =>
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 1
            [max_score] => 0.30685282
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => my_index
                            [_type] => my_type
                            [_id] => my_id
                            [_score] => 0.30685282
                            [_source] => Array
                                (
                                    [testField] => abc
                                )
                        )
                )
        )
)

删除一个文档

好的,继续。现在删除之前添加的文档:

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id'
];

$response = $client->delete($params);
print_r($response);

你会注意到这是与get语法相同的语法。唯一的区别在于操作:delete替换了get。得到的响应证实了文档确实被删除:

Array
(
    [found] => 1
    [_index] => my_index
    [_type] => my_type
    [_id] => my_id
    [_version] => 2
)

删除索引

由于 Elasticsearch 的动态特性, 我们添加的第一个文档自动构建了一个带有一些默认设置的索引。因为稍后我们要自己指定一些设置,所以现在可以先把这个索引删除:

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

打印的结果:

Array
(
    [acknowledged] => 1
)

创建索引

现在我们重新开始(没有任何数据和索引),我们来添加一个自定义设置的新索引:

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

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

Elasticsearch 会用你自定义的设置创建新索引,并返回一个确认结果:

Array
(
    [acknowledged] => 1
)

总结

这只是对客户端及其语法的快速概述。如果您熟悉 elasticsearch, 您将会注意到这些方法的名称与 REST 端点一样。

您还将注意到客户端的配置方法有助于 IDE 轻松发现。所有核心操作到都可以在 $client 对象下可用 (索引, 搜索, 获取, 等)。索引和集群管理都分别位于 $client→indices()$client→cluster() 对象下。

查看其余文档,了解客户端的工作原理。

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