【实战】PHP如何使用 ElasticSearch 做搜索
在做搜索的时候想到了 ElasticSearch ,而且其也支持 PHP,所以就做了一个简单的例子做测试,感觉还不错,做下记录。
环境
php 8.0
elasticsearch 8.2
elasticsearch-php 8.2
安装 elasticsearch
下载源文件,解压,重新建一个用户,将目录的所属组修改为此用户,因为 elasticsearch 无法用 root 用户启动。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.2.3.tar.gz
tar zxvf elasticsearch-8.2.3.tar.gz
useradd elasticsearch
password elasticsearch
chown elasticsearch:elasticsearch elasticsearch-8.2.3
cd elasticsearch-8.2.3
./bin/elasticsearch // 启动
安装 PHP 扩展
我这里使用的是 composer 安装 elasticsearch-php。在 composer.json 文件中加入 "elasticsearch/elasticsearch": "~8.2.3",执行 composer update。
{
"require": {
// ...
"elasticsearch/elasticsearch": "~8.2.3"
// ...
}
}
测试例子
我这里准备了一张文章表来进行测试,首先是建表,其次写入测试数据,准备工作完毕之后,就开始编辑测试用例。
ElasticSearch快速入门 ElasticSearch近实时搜索的实现
#创建articles表
create table articles(
id int not null primary key auto_increment,
title varchar(200) not null comment '标题',
content text comment '内容'
);
#插入文章内容
insert into articles(title, content)
values
('码农编程进阶笔记1', '码农编程进阶笔记——内容1'),
('码农编程进阶笔记2', '码农编程进阶笔记-内容2'),
('码农编程进阶笔记3', '码农编程进阶笔记-内容3');
从 Mysql 读取数据
try {
$db = new PDO('mysql:host=127.0.0.1;dbname=test', 'root','');
$sql = 'select * from articles';
$query = $db->prepare($sql);
$query->execute();
$lists = $query->fetchAll();
print_r($lists);
} catch (Exception $e) {
echo $e->getMessage();
}
使用ElasticSearch服务从MySQL同步数据实现搜索即时提示与全文搜索功能
实例化
require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
名词解释:索引相当于 MySQL 中的表,文档相当于 MySQL 中的行记录
elasticsearch 的动态性质,在添加第一个文档的时候自动创建了索引和一些默认设置。
将文档加入索引
foreach ($lists as $row) {
$params = [
'body' => [
'id' => $row['id'],
'title' => $row['title'],
'content' => $row['content']
],
'id' => 'article_' . $row['id'],
'index' => 'articles_index',
'type' => 'articles_type'
];
$client->index($params);
}
从索引中获取文档
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'articles_1'
];
$res = $client->get($params);
print_r($res);
从索引中删除文档
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
'id' => 'articles_1'
];
$res = $client->delete($params);
print_r($res);
删除索引
$params = [
'index' => 'articles_index'
];
$res = $client->indices()->delete($params);
print_r($res);
创建索引
$params['index'] = 'articles_index';
$params['body']['settings']['number_of_shards'] = 2;
$params['body']['settings']['number_of_replicas'] = 0;
$client->indices()->create($params);
搜索
$params = [
'index' => 'articles_index',
'type' => 'articles_type',
];
$params['body']['query']['match']['content'] = '码农编程进阶笔记';
$res = $client->search($params);
print_r($res);
赞赏码


非学,无以致疑;非问,无以广识
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2021-10-20 Centos 根据端口查看占用进程 根据进程查看占用端口
2021-10-20 Elasticsearch 之Mapping设置
2021-10-20 实战!聊聊PHP如何使用 ElasticSearch 做搜索
2018-10-20 最完美解决Nginx部署ThinkPHP项目的办法
2018-10-20 最完美解决Nginx部署ThinkPHP项目的办法
2018-10-20 nginx 80端口重定向到443端口
2018-10-20 nginx 80端口重定向到443端口