溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在PHP中使用ElasticSearch實現搜索

發布時間:2021-06-07 17:35:59 來源:億速云 閱讀:216 作者:Leah 欄目:開發技術

本篇文章給大家分享的是有關怎么在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 的動態性質,在添加第一個文檔的時候自動創建了索引和一些默認設置。

將文檔加入索引

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實現搜索,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女