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.gz
tar zxvf elasticsearch-6.2.3.tar.gz
useradd elasticsearch
password elasticsearch
chown elasticsearch:elasticsearch elasticsearch-6.2.3
cd 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 的动态性质,在添加第一个文档的时候自动创建了索引和一些默认设置。

具体使用方法例子:

/引入es搜索类
//require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;


class Index
{
    public function index()
    {
        
        /*$client = ClientBuilder::create()->setHosts($hosts)->build();*/
        //实例化es类;在项目中引入自动加载文件,并且实例化一个客户端:
        $client = ClientBuilder::create()->build();

        try {
       //将文档加入索引
       //echo ClientBuilder::$aaa;
       $data = db::name('articles')->select();
       //查询出多条数据添加索引
       foreach ($data as $k => $v) {
               
               $params = [
                'index' => 'article_index',
                'type' => 'article_type',
                'id' => 'article_' . $v['id'],
                'body' => [
                            'id' => $v['id'],
                            'title' => $v['title'],
                            'content' => $v['content'],
                          ],
            ];
            $response = $client->index($params);

       }

       //从索引中获取文档
      $getparams = [
          'index' => 'article_index',
          'type' => 'article_type',
          'id' => 'article_1'
        ];
        $res = $client->get($getparams);

        //从索引中删除文档
        $delparams = [
          'index' => 'article_index',
          'type' => 'article_type',
          'id' => 'article_1'
        ];
        $res = $client->delete($delparams);

        //删除索引
        $params = [
            'index' => 'articles_index'
        ];
        $res = $client->indices()->delete($params);
        print_r($res);

        //搜索
        $serparams = [ 
          'index' => 'article_index',
          'type' => 'article_type',
        ];      

        $serparams['body']['query']['match']['content'] = '文章内容6';
        $resech = $client->search($serparams);

        pp($resech);
                


        // pp($data);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }

猜你喜欢

转载自www.cnblogs.com/yszr/p/10276432.html