PHP 使用 ElasticSearch 作搜索

lasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并做为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,可以达到实时搜索,稳定,可靠,快速,安装使用方便。php

在作搜索的时候想到了 ElasticSearch ,并且其也支持 PHP,因此就作了一个简单的例子作测试,感受还不错,作下记录。mysql

环境
php 7.2
elasticsearch 6.2 下载
elasticsearch-php 6 下载web

安装 elasticsearch
下载源文件,解压,从新建一个用户,将目录的所属组修改成此用户,由于 elasticsearch 没法用 root 用户启动。sql

1
2
3
4
5
6
7
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。json

1
2
3
4
5
6
7
{
  "require": {
    // ...
    "elasticsearch/elasticsearch": "~6.0"
    // ...
  }
}
测试例子
建立表和测试数据
我这里准备了一张文章表来进行测试,首先是建表,其次写入测试数据,准备工做完毕以后,就开始编辑测试用例。服务器

1
2
3
4
5
6
7
8
9
create table articles(
  id int not null primary key auto_increment,
  title varchar(200) not null comment '标题',
  content text comment '内容'
);composer

insert into articles(title, content) values ('Laravel 测试1', 'Laravel 测试文章内容1'),
('Laravel 测试2', 'Laravel 测试文章内容2'),
('Laravel 测试3', 'Laravel 测试文章内容3');
从 Mysql 读取数据
1
2
3
4
5
6
7
8
9
10
11
12
try {
  $db = new PDO('mysql:host=127.0.0.1;dbname=test', 'root', 'root');elasticsearch

  $sql = 'select * from articles';分布式

  $query = $db->prepare($sql);
  $query->execute();
  $lists = $query->fetchAll();
  print_r($lists);
} catch (Exception $e) {
  echo $e->getMessage();
}
实例化
1
2
3
require './vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
名词解释:索引至关于 MySQL 中的表,文档至关于 MySQL 中的行记录测试

elasticsearch 的动态性质,在添加第一个文档的时候自动建立了索引和一些默认设置。

将文档加入索引
1
2
3
4
5
6
7
8
9
10
11
12
13
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);
}
从索引中获取文档
1
2
3
4
5
6
7
$params = [
  'index' => 'articles_index',
  'type' => 'articles_type',
  'id' => 'articles_1'
];
$res = $client->get($params);
print_r($res);
从索引中删除文档
1
2
3
4
5
6
7
$params = [
  'index' => 'articles_index',
  'type' => 'articles_type',
  'id' => 'articles_1'
];
$res = $client->delete($params);
print_r($res);
删除索引
1
2
3
4
5
$params = [
    'index' => 'articles_index'
];
$res = $client->indices()->delete($params);
print_r($res);
建立索引
1
2
3
4
$params['index'] = 'articles_index';  
$params['body']['settings']['number_of_shards'] = 2;  
$params['body']['settings']['number_of_replicas'] = 0;  
$client->indices()->create($params);
搜索
1
2
3
4
5
6
7
8
$params = [ 
  'index' => 'articles_index',
  'type' => 'articles_type',
];      

$params['body']['query']['match']['content'] = 'Laravel'; $res = $client->search($params); print_r($res);

相关文章
相关标签/搜索