以TP5.1框架为准,ElasticSearch的依赖安装
在ElasticSearch的官网中进行composer的命令行复制即可。
这里需要注意的是,我下载的ElasticSearch版本是8.2.0版本,PHP的ElasticSearch依赖包对应的事7.11版本。如果用的不是该版本可能会出现ElasticSeearch无法正常与PHP链接。
安装步骤:
1、在 composer.json 文件中增加 elasticsearch-php。如果你是新建项目,那么把以下的代码复制粘贴到 composer.json 就行了。如果是在现有项目中添加 elasticsearch-php,那么把 elasticsearch-php 添加到其它的包名后面即可:
{
"require": {
"elasticsearch/elasticsearch": "^7.11"
"elasticsearch/elasticsearch": "~6.0"
}
}
2、然后 composer install 即可
3、最后加载 autoload.php。如果你现有项目是用 Composer 安装的,那么 autoload.php 也许已经在某处加载了,你就不必再加载。最后实例化一个客户端对象:(注意 Composer安装的可以忽略这一步)
require 'vendor/autoload.php';
$client = Elasticsearch\ClientBuilder::create()->build();
PHP 使用 ElasticSearch
1、创建一个类:
<?php
namespace join\utils; # 命名空间
use Elasticsearch\ClientBuilder; # 这里必须引入这个链接ES服务的类
class ElasticSearchss{}
2、制作链接ElasticSearch的自动运行类
public $api; # 用于普通调用
public $index_name; # 索引名称
public $index_type; # 操作的类型,默认应该是 '_doc'
public static $Client; # 用于静态调用
public function __construct($index_name, $index_type)
{
try {
//构建客户端对象
$hosts = ['192.168.0.100:9200']; # 你的ElasticSearch服务在本地的ip
self::$Client = $this->api = ClientBuilder::create()->setHosts($hosts)->build(); # 链接
$this->index_name = $index_name;
$this->index_type = $index_type;
} catch (\Exception $e) {
throw $e;
}
}
3、获取所有的文档
public static function getAll()
{
self::open('huaifeng');
$client = ClientBuilder::create()->build();
$params = [
"size" => 50, # 显示的数量,理论上将,可以输入10000
"type" => '_doc' # 在文档中搜索
"index" => "huaifeng", # 索引名称
"body" => [ # 搜索条件
"query" => [ # 搜索
"match_all" => new \stdClass() # 搜索全部
]
]
];
$response = $client->search($params); # 执行
dump($response); # 结果
}
4、 按照id进行排序,模糊搜索并进行分页
/**
* 根据关键字查询数据
* 多个字段查询:multi_match
* @param $keyword 搜索的管关键字
* @param $page 第几页,默认为1页
* @param $size 每页有几条数据,默认每页10条
* @return array|bool
* @author 陈鑫
* @date 2022-5-26
*/
public static function pageList($keyword, $page = 1, $size = 10)
{
$from = $page == 1 ? 0 : ($page * $size) - $size;
$params = [
'from' => $from, # 从第几条开始
'size' => $size, # 每页查询几条
'index' => 'huaifeng', # 索引名称
'body' => [ # 提交体
'query' => [ # 查询
'match' => [ # 分词模糊查询
'title' => $keyword # 以title字段查询关键字
],
],
'sort' => [ # 排序
'id' => [ # 根据id字段排序(注意,这里的id是你添加文档返回的id)
'order' => 'desc', # 倒序排序
]
],
]
];
$response = self::$Client->search($params);
$aData['data'] = array();
foreach ($response['hits']['hits'] as $k => $v) {
$aData['data'][] = $v['_source'];
}
$aData['total'] = $response['hits']['total']['value'];
return $aData;
}
5、添加一个文档
public static function addIndexDoc($id, $title, $table, $img)
{
# 需要添加的文档开始
$aData['id'] = $id;
$aData['title'] = $title;
$aData['type'] = $table;
$aData['img'] = $img;
# 需要添加的文档结束
$params = [
'index' => 'huaifeng', # 索引名称
'type' => '_doc', # 类型为添加文档
'body' => $aData # 要添加的数据
];
$response = self::$Client->index($params);
return $response['_id']; # 获取到添加文旦后的id值
}
6、根据文档id更新一个文档
public static function updataIndexDoc($eid, $id, $title, $table, $img)
{
$params = [
'index' => 'huaifeng',
'type' => '_doc',
'id' => $eid, # 添加文档后返回的id
'body' => [
'params' => [
'id' => $id,
'title' => $title,
'table' => $table,
'img' => $img
]
]
];
$response = self::$Client->update($params);
}