首頁  >  文章  >  後端開發  >  PHP基於ElasticSearch做搜尋

PHP基於ElasticSearch做搜尋

angryTom
angryTom轉載
2020-02-04 17:20:503976瀏覽

ElasticSearch是一個基於Lucene的搜尋伺服器。它提供了一個分散式多用戶能力的全文搜尋引擎,基於RESTful web介面。 Elasticsearch是用Java開發的,並作為Apache授權條款下的開放原始碼發布,是目前流行的企業級搜尋引擎。設計用於雲端運算中,能夠達到即時搜索,穩定,可靠,快速,安裝使用方便。

PHP基於ElasticSearch做搜尋

課程推薦→:《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.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 的動態性質,在新增第一個文件的時候自動建立了索引和一些預設值。

將文件加入索引

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做搜尋的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除