如何在PHP中使用ElasticSearch实现搜索
发表于:2025-11-11 作者:千家信息网编辑
千家信息网最后更新 2025年11月11日,这篇"如何在PHP中使用ElasticSearch实现搜索"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面
千家信息网最后更新 2025年11月11日如何在PHP中使用ElasticSearch实现搜索
这篇"如何在PHP中使用ElasticSearch实现搜索"文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇"如何在PHP中使用ElasticSearch实现搜索"文章吧。
环境
php 7.2
elasticsearch 6.2 下载
elasticsearch-php 6 下载
安装 elasticsearch
下载源文件,解压,重新建一个用户,将目录的所属组修改为此用户,因为 elasticsearch 无法用 root 用户启动。
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gztar zxvf elasticsearch-6.2.3.tar.gzuseradd elasticsearchpassword elasticsearchchown elasticsearch:elasticsearch elasticsearch-6.2.3cd elasticsearch-6.2.3./bin/elasticsearch // 启动
安装 PHP 扩展
我这里使用的是 composer 安装 elasticsearch-php。在 composer.json 文件中加入 "elasticsearch/elasticsearch": "~6.0",执行 composer update。
{ "require": { // ... "elasticsearch/elasticsearch": "~6.0" // ... }}测试例子
创建表和测试数据
我这里准备了一张文章表来进行测试,首先是建表,其次写入测试数据,准备工作完毕之后,就开始编辑测试用例。
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 ('Laravel 测试1', 'Laravel 测试文章内容1'),('Laravel 测试2', 'Laravel 测试文章内容2'),('Laravel 测试3', 'Laravel 测试文章内容3');从 Mysql 读取数据
try { $db = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root'); $sql = 'select * from articles'; $query = $db->prepare($sql); $query->execute(); $lists = $query->fetchAll(); print_r($lists);} catch (Exception $e) { echo $e->getMessage();}实例化
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'] = 'Laravel';$res = $client->search($params);print_r($res);
以上就是关于"如何在PHP中使用ElasticSearch实现搜索"这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注行业资讯频道。
测试
内容
索引
文章
文档
搜索
数据
用户
知识
篇文章
准备
为此
价值
例子
动态
名词
名词解释
大部分
实例
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
部门网络安全责任清单
win8服务器怎么搭建
潮州网络安全活动
casia数据库b
王牌战争服务器名称包含敏感词语
牙医管家数据库表
海量图片数据库
普陀区生态网络技术供应
数据库个人自荐怎么写
自动还款软件开发公司
软件开发公司驻场自带哪些设备
多台物理服务器怎么虚拟化
盱眙品质软件开发服务合同
广东网信办网络安全协调处
数据库有嵌套循环吗
区块链有无数据库
工业控制网络技术简介
硬科技和互联网
一个文件分发服务器
数据 数据库 的联系
二层网络技术配置
新浪微博服务器ip
网络安全家园共育论文
mysql数据库分布式
在线编辑器如何保存数据库
未来网络安全问题
c 数据库连接可配置
口袋妖怪数据库怎么建
国外论文数据库
数据库有嵌套循环吗