濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP ElasticSearch做搜索實例講解

PHP ElasticSearch做搜索實例講解

熱門標簽:百應電話機器人服務 青島語音外呼系統(tǒng)招商 揚州地圖標注app 騰訊外呼管理系統(tǒng) 昭通辦理400電話 岳陽外呼型呼叫中心系統(tǒng)在哪里 山西探意電話機器人 河南電銷卡外呼系統(tǒng)哪家強 山西回撥外呼系統(tǒng)

ElasticSearch是一個基于Lucene的搜索服務器。它提供了一個分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是當前流行的企業(yè)級搜索引擎。設計用于云計算中,能夠達到實時搜索,穩(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)建表和測試數據

我這里準備了一張文章表來進行測試,首先是建表,其次寫入測試數據,準備工作完畢之后,就開始編輯測試用例。

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 的動態(tài)性質,在添加第一個文檔的時候自動創(chuàng)建了索引和一些默認設置。

將文檔加入索引

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做搜索的詳細內容,希望腳本之家整理的內容能夠幫助到大家。

您可能感興趣的文章:
  • Elasticsearch工具cerebro的安裝與使用教程
  • docker鏡像訪問本地elasticsearch端口操作
  • Django利用elasticsearch(搜索引擎)實現搜索功能
  • docker 啟動elasticsearch鏡像,掛載目錄后報錯的解決
  • PHP中使用ElasticSearch最新實例講解

標簽:宜賓 南陽 寶雞 銅川 湛江 婁底 鎮(zhèn)江 黃南

巨人網絡通訊聲明:本文標題《PHP ElasticSearch做搜索實例講解》,本文關鍵詞  PHP,ElasticSearch,做,搜索,實例,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP ElasticSearch做搜索實例講解》相關的同類信息!
  • 本頁收集關于PHP ElasticSearch做搜索實例講解的相關信息資訊供網民參考!
  • 推薦文章
    琼海市| 凉山| 滕州市| 广宗县| 云林县| 沙湾县| 佛教| 彰化县| 龙江县| 介休市| 池州市| 台北市| 绍兴市| 甘孜县| 刚察县| 新干县| 祁东县| 长海县| 柳林县| 达拉特旗| 浏阳市| 长沙市| 威信县| 德钦县| 广水市| 胶州市| 闽侯县| 淮北市| 斗六市| 平谷区| 上思县| 万年县| 彭州市| 济宁市| 错那县| 汶上县| 弥勒县| 霍林郭勒市| 天全县| 康乐县| 福清市|