【实战】PHP如何使用 ElasticSearch 做搜索
2023-06-13 09:11:07 时间
在做搜索的时候想到了 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() 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);
相关文章
- php大小写转换
- PHP 中函数 isset(), empty(), is_null() 的区别
- MySQL 锁表与 PHP 数据交互实现(mysql锁表php)
- Linux下使用PHP开发软件的简易指南(linux下php开发)
- 实现使用PHP实现的Redis乐观锁(redis乐观锁php)
- 戳解锁Linux下PHP时间戳之谜(linuxphp时间)
- 数据快速检索MySQL中的数据:使用PHP(php检索mysql)
- 数据库快速学会:用PHP建立MySQL数据库(php建立mysql)
- PHP MySQL 手册:让您轻松打造数据库应用(phpmysql手册)
- Linux平台如何测试PHP环境(linux测试php环境)
- Linux无法解析PHP文件,如何解决(linux 不解析php)
- PHP管理MSSQL的方法探索(Php管理mssql)
- PHP实现从MSSQL中数据显示(php 显示 mssql)
- 让PHP连接MSSQL数据库的正确方式(php如何连mssql)
- PHP连接MSSQL数据库快速开启(php mssql 打开)
- PHP与MySQL:兼容性更好、更强大的灵活性(php与mysql 优点)
- PHP中PDO基础教程入门级
- 基于PHP导出Excel的小经验完美解决乱码问题
- php自定义apk安装包实例
- PHP网页游戏学习之Xnova(ogame)源码解读(二)