PHP使用elasticsearch-php操作Elasticsearch搜索引擎的方法【20210521】

发表于 2021-05-20 11:10:49
阅读 190

介绍

介绍

福哥今天带着大家学习一下使用PHP操作Elasticsearch搜索引擎的方法,虽然我们的TFLinux早就安装了Elasticsearch搜索引擎了,但是还没有应用用到Elasticsearch搜索引擎,福哥打算先给TFPHP框架增加操作Elasticsearch搜索引擎的支持。

从github上面搜了一下,发现官方提供了一个用于PHP操作Elasticsearch的驱动库elasticsearch-php。今天福哥就带着大家学习一下这个官方的elasticsearch-php驱动库的使用技巧~~

安装

安装

ES官方的PHP驱动库可以从github上面获取,地址如下:

https://github.com/elastic/elasticsearch-php

下载完elasticsearch-php-master.zip解开它,可以得到如下的内容。

home/topic/2021/0520/11/9c4ecdcc451cd2dd689f4991fbb09904.png

安装php-fig/log

elasticsearch-php需要php-fig/log库,我们也可以从github上面下载。

https://github.com/php-fig/log

下载完log-master.zip解开它,可以得到如下内容:

home/topic/2021/0520/16/725644bf411993148dad07737596bc86.png

安装guzzle/guzzle

elasticsearch-php需要guzzle库,我们也可以从github上面下载。

https://github.com/guzzle/guzzle

下载完guzzle-master.zip解开它,可以得到如下内容:

home/topic/2021/0520/16/1f55923b8cf3cbfe5e12dd4e76366b45.png

将库文件放入GuzzleHttp文件夹下面,名称不要弄错了!

安装guzzle/RingPHP

elasticsearch-php需要guzzle/RingPHP库,虽然guzzle新版本文档里说以后可能会抛弃RingPHP库,可以从github库下载。

https://github.com/guzzle/RingPHP

下载完RingPHP-master.php解开它,可以得到如下内容:

home/topic/2021/0520/16/ba13d885e807277e9234142c0eda2aaa.png

将库文件放入GuzzleHttp/Ring子文件夹下面,名称不要弄错了!

安装reactphp/promise

elasticsearch-php需要reactphp/promise库,可以从github库下载。最新版本和GuzzleHttp有兼容问题,所以福哥选择的是2.7.0版本。

https://github.com/reactphp/promise/tree/v2.7.0

下载完promise-2.7.0.zip解开它,可以看到如下内容:

home/topic/2021/0520/16/1bbc6ce791959fff2700f58c85732267.png

将库文件放入React/Promise文件夹下面,名称不要弄错了!

目录结构

最后的目录结构是这样的:

home/topic/2021/0520/17/2aeec0f93fa280c6615280c52a170567.png

使用

自动加载

因为Elasticsearch的驱动库是一套PHP代码,所以如果要使用里面众多的对象需要建立一个自动加载的机制,这里面福哥使用的是PHP的spl_autoload_register函数实现对象的自动加载的,代码如下:

include_once (FRAMEWORK_ROOT_PATH . "Extends/Driver/React/Promise/functions_include.php");

function Elasticsearch_Autoload(string $class):bool {
    if(substr($class,0,14) == "Elasticsearch\\") {
        $classPath = FRAMEWORK_ROOT_PATH . "Extends/Driver/" . str_replace("\\", "/", $class) . ".php";
        if (file_exists($classPath)) {
            include_once($classPath);

            return true;
        }
    }
    else{
        $classPath = FRAMEWORK_ROOT_PATH . "Extends/Driver/" . str_replace("\\", "/", $class) . ".php";
        if (file_exists($classPath)) {
            include_once($classPath);

            return true;
        }
    }

    return false;
}

spl_autoload_register("Elasticsearch_Autoload");

使用这个自动加载非常简单,只要包含这个PHP程序文件即可。

include_once (FRAMEWORK_ROOT_PATH . "Extends/Driver/Elasticsearch_Autoload.inc.php");

连接

因为elasticsearch-php是HTTP方式访问Elasticsearch搜索引擎的,所以不存在“连接”这个行为,每次的一个操作都是一次HTTP请求。

索引列表

福哥翻遍了elasticsearch-php的文档,各种尝试Client对象的方法,都没有一个方法可以得到索引列表。

创建索引

index

创建索引是通过Client::index方法实现的,传入参数是一个数组,必须包括一个默认文档的参数,也就是说得有id和body元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->index(array(
    'index'=>"test",
    'id'=>1,
    'body'=>array(
        'test'=>array(
            'name'=>"福哥",
            'languages'=>"PHP, Java, Python, JavaScript, C/C++"
        )
    )
));
print_r($resp);

home/topic/2021/0520/17/62cdb3c520259270de3387fdc2e5711f.png

create

使用Client::index方法创建的索引不能设置type参数,要想设置type参数需要使用Client::create方法,传入参数是一个数组,必须包括一个默认文档的参数,也就是说得有id和body元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->create(array(
    'index'=>"test",
    'type'=>"test",
    'id'=>1,
    'body'=>array(
        'name'=>"福哥",
        'languages'=>"PHP, Java, Python, JavaScript, C/C++"
    )
));
print_r($resp);

home/topic/2021/0520/17/a4e1ae68a93cff87827fe8d04468371c.png

插入数据

插入数据使用Client::create方法实现,传入参数是一个数组,必须包括index、id和body元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->create(array(
    'index'=>"test",
    'type'=>"test",
    'id'=>35,
    'body'=>array(
        'name'=>"鬼谷子叔叔",
        'languages'=>"Everything!!!"
    )
));
print_r($resp);

home/topic/2021/0520/17/b8aa6ad912b849b90ec937a662738e9b.png

更新数据

更新数据使用Client::update方法实现,传入参数是一个数组,必须包括index、id以及body元素。

bdoy元素下面的数据需要放到doc元素下面,这是告诉ES我们是更新部分文档内容。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->update(array(
    'index'=>"test",
    'type'=>"test",
    'id'=>35,
    'body'=>array(
        'doc'=>array(
            'age'=>35
        )
    )
));
print_r($resp);

home/topic/2021/0520/17/b6f73dc2ed5f47ec5b39f4cab03ce697.png

删除数据

删除数据通过Client::delete方法实现,传入参数是一个数组,必须包含index和id元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->delete(array(
    'index'=>"test",
    'type'=>"test",
    'id'=>35
));
print_r($resp);

home/topic/2021/0520/17/652d00aa39795911148d5e9802f191ad.png

获取数据

获取数据通过Client::get方法实现,传入参数就是文档ID。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->get(array(
    'index'=>"test",
    'id'=>35
));
print_r($resp);

home/topic/2021/0527/16/a78e8e437a8282116c31eb2354c54eb5.png

查询数据

Elasticsearch的查询技巧可以单独开一门课程了,今天福哥只是把使用elasticsearch-php库实现查询Elasticsearch搜索引擎的语法教给大家。

search

使用Client::search方法提交标准的Elasticsearch的query语句来查询数据。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->search(array(
    'index'=>"test",
    'body'=>array(
        'query'=>array(
            'match'=>array(
                'languages'=>"PHP"
            )
        )
    )
));
var_dump($resp);

home/topic/2021/0520/17/808b6958889ba04dbf2e62e89c15ac6b.png

sql

使用Client::sql方法查询数据需要安装“_sql”插件,福哥后面会单独开教程给大家讲解的。

删除数据

删除数据通过Client::delete方法实现,传入参数是一个数组,必须包含index和id元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->delete(array(
    'index'=>"test",
    'type'=>"test",
    'id'=>1
));
print_r($resp);

home/topic/2021/0520/18/8fd1e3f9cd63cb92d8d7774e90c8dbbe.png

删除索引

删除索引需要使用Client::indices方法的delete实现,传入参数是一个数组,必须包含index元素。

$builder = \Elasticsearch\ClientBuilder::create();
$builder->setHosts(array("127.0.0.1:9200"));
$client = $builder->build();

$resp = $client->indices()->delete(array(
    'index'=>"test"
));
print_r($resp);

home/topic/2021/0520/18/b1b590ac3aed321f77e7d86be01353d1.png

总结

今天福哥带着童鞋们系统地学习了elasticsearch-php驱动库的使用方法,elasticsearch-php驱动库是官方提供的用来操作Elasticsearch数据库的专用库。

elasticsearch-php驱动库的功能非常强大,我们今后会逐步将这些功能讲解给大家,敬请期待~~