PHP ElasticSearch做搜索實例講解
ElasticSearch是一個基于Lucene的搜索服務(wù)器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是當前流行的企業(yè)級搜索引擎。設(shè)計用于云計算中,能夠達到實時搜索,穩(wěn)定,可靠,快速,安裝使用方便。
PHP基于ElasticSearch做搜索
在做搜索的時候想到了 ElasticSearch ,而且其也支持 PHP,所以就做了一個簡單的例子做測試,感覺還不錯,做下記錄。
環(huán)境
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",執(zhí)行 composer update。
{ "require": { // ... "elasticsearch/elasticsearch": "~6.0" // ... } }
測試例子
創(chuàng)建表和測試數(shù)據(jù)
我這里準備了一張文章表來進行測試,首先是建表,其次寫入測試數(shù)據(jù),準備工作完畢之后,就開始編輯測試用例。
create table articles( id int not null primary key auto_increment, title varchar(200) not null comment '標題', content text comment '內(nèi)容' ); insert into articles(title, content) values ('Laravel 測試1', 'Laravel 測試文章內(nèi)容1'), ('Laravel 測試2', 'Laravel 測試文章內(nèi)容2'), ('Laravel 測試3', 'Laravel 測試文章內(nèi)容3');
從 Mysql 讀取數(shù)據(jù)
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 的動態(tài)性質(zhì),在添加第一個文檔的時候自動創(chuàng)建了索引和一些默認設(shè)置。
將文檔加入索引
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);
創(chuàng)建索引
$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做搜索的詳細內(nèi)容,希望腳本之家整理的內(nèi)容能夠幫助到大家。
相關(guān)文章
安裝PHP可能遇到的問題“無法載入mysql擴展” 的解決方法
安裝PHP可能遇到的問題“無法載入mysql擴展” 的解決方法...2007-04-04PHP函數(shù)篇之掌握ord()與chr()函數(shù)應(yīng)用
ord()函數(shù)把字符轉(zhuǎn)換為十進制數(shù)字,chr()函數(shù)把十進制數(shù)字轉(zhuǎn)化為字符,在二進制,八進制,十進制與十六進制之間充當橋梁的作用2011-12-12獲取用戶Ip地址通用方法與常見安全隱患(HTTP_X_FORWARDED_FOR)
這個來自一些項目中,獲取用戶Ip,進行用戶操作行為的記錄,是常見并且經(jīng)常使用的。 一般朋友,都會看到如下通用獲取IP地址方法2013-06-06php遞歸實現(xiàn)無限分類生成下拉列表的函數(shù)
php自定義函數(shù)之遞歸實現(xiàn)無限分類生成下拉列表,這樣可以提高效率,不用每次都從數(shù)據(jù)庫讀取數(shù)據(jù)。2010-08-08php5.5使用PHPMailer-5.2發(fā)送郵件的完整步驟
PHPMailer已經(jīng)更新了很多版本了,本教程只針對老版本。下面這篇文章主要給大家介紹了關(guān)于php5.5使用PHPMailer-5.2發(fā)送郵件的完整步驟,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-10-10