PHP使用elasticsearch-php操作Elasticsearch搜索引擎的方法
1. 介绍
1.1 介绍
福哥今天带着大家学习一下使用PHP操作Elasticsearch搜索引擎的方法,虽然我们的TFLinux早就安装了Elasticsearch搜索引擎了,但是还没有应用用到Elasticsearch搜索引擎,福哥打算先给TFPHP框架增加操作Elasticsearch搜索引擎的支持。
从github上面搜了一下,发现官方提供了一个用于PHP操作Elasticsearch的驱动库elasticsearch-php。今天福哥就带着大家学习一下这个官方的elasticsearch-php驱动库的使用技巧~~
2. 安装
2.1 安装
ES官方的PHP驱动库可以从github上面获取,地址如下:
https://github.com/elastic/elasticsearch-php
下载完elasticsearch-php-master.zip解开它,可以得到如下的内容。

2.2 安装php-fig/log
elasticsearch-php需要php-fig/log库,我们也可以从github上面下载。
https://github.com/php-fig/log
下载完log-master.zip解开它,可以得到如下内容:

2.3 安装guzzle/guzzle
elasticsearch-php需要guzzle库,我们也可以从github上面下载。
https://github.com/guzzle/guzzle
下载完guzzle-master.zip解开它,可以得到如下内容:

将库文件放入GuzzleHttp文件夹下面,名称不要弄错了!
2.4 安装guzzle/RingPHP
elasticsearch-php需要guzzle/RingPHP库,虽然guzzle新版本文档里说以后可能会抛弃RingPHP库,可以从github库下载。
https://github.com/guzzle/RingPHP
下载完RingPHP-master.php解开它,可以得到如下内容:

将库文件放入GuzzleHttp/Ring子文件夹下面,名称不要弄错了!
2.5 安装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解开它,可以看到如下内容:

将库文件放入React/Promise文件夹下面,名称不要弄错了!
2.6 目录结构
最后的目录结构是这样的:

3. 使用
3.1 自动加载
因为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");
3.2 连接
因为elasticsearch-php是HTTP方式访问Elasticsearch搜索引擎的,所以不存在“连接”这个行为,每次的一个操作都是一次HTTP请求。
3.3 索引列表
福哥翻遍了elasticsearch-php的文档,各种尝试Client对象的方法,都没有一个方法可以得到索引列表。
3.4 创建索引
3.4.1 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);

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

3.5 插入数据
插入数据使用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);

3.6 更新数据
更新数据使用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);

3.7 删除数据
删除数据通过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);

3.8 获取数据
获取数据通过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);

3.9 查询数据
Elasticsearch的查询技巧可以单独开一门课程了,今天福哥只是把使用elasticsearch-php库实现查询Elasticsearch搜索引擎的语法教给大家。
3.9.1 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);

3.9.2 sql
使用Client::sql方法查询数据需要安装“_sql”插件,福哥后面会单独开教程给大家讲解的。
3.10 删除数据
删除数据通过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);

3.11 删除索引
删除索引需要使用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);

4. 总结
今天带着童鞋们系统地学习了elasticsearch-php驱动库的使用方法,elasticsearch-php驱动库是官方提供的用来操作Elasticsearch数据库的专用库。
elasticsearch-php驱动库的功能非常强大,我们今后会逐步将这些功能讲解给大家,敬请期待~~