首页后端开发PHPPHP基于ElasticSearch做搜索

PHP基于ElasticSearch做搜索

时间2024-02-02 01:38:02发布访客分类PHP浏览295
导读:收集整理的这篇文章主要介绍了PHP基于ElasticSearch做搜索,觉得挺不错的,现在分享给大家,也给大家做个参考。ElasticSeArch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTf...
收集整理的这篇文章主要介绍了PHP基于ElasticSearch做搜索,觉得挺不错的,现在分享给大家,也给大家做个参考。ElasticSeArch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

课程推荐→:《elasticsearch全文搜索实战》(实战视频)

来自课程《千万级数据并发解决方案(理论+实战)》

PHP基于ElasticSearch做搜索

在做搜索的时候想到了 ElasticSearch ,而且其也支持 PHP,所以就做了一个简单的例子做测试,感觉还不错,做下记录。

环境

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做搜索的详细内容,更多请关注其它相关文章!

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: PHP基于ElasticSearch做搜索
本文地址: https://pptw.com/jishu/596190.html
正则表达式中模式修正符作用详解 PHP查找一列有序数组是否包含某值(二分查找)

游客 回复需填写必要信息