佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > PHP ElasticSearch做搜索實(shí)例講解

PHP ElasticSearch做搜索實(shí)例講解

熱門(mén)標(biāo)簽:服務(wù)器配置 阿里云 Linux服務(wù)器 銀行業(yè)務(wù) 團(tuán)購(gòu)網(wǎng)站 科大訊飛語(yǔ)音識(shí)別系統(tǒng) Mysql連接數(shù)設(shè)置 電子圍欄

ElasticSearch是一個(gè)基于Lucene的搜索服務(wù)器。它提供了一個(gè)分布式多用戶能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java開(kāi)發(fā)的,并作為Apache許可條款下的開(kāi)放源碼發(fā)布,是當(dāng)前流行的企業(yè)級(jí)搜索引擎。設(shè)計(jì)用于云計(jì)算中,能夠達(dá)到實(shí)時(shí)搜索,穩(wěn)定,可靠,快速,安裝使用方便。

PHP基于ElasticSearch做搜索

在做搜索的時(shí)候想到了 ElasticSearch ,而且其也支持 PHP,所以就做了一個(gè)簡(jiǎn)單的例子做測(cè)試,感覺(jué)還不錯(cuò),做下記錄。

環(huán)境

php 7.2

elasticsearch 6.2 下載

elasticsearch-php 6 下載

安裝 elasticsearch

下載源文件,解壓,重新建一個(gè)用戶,將目錄的所屬組修改為此用戶,因?yàn)?elasticsearch 無(wú)法用 root 用戶啟動(dòng)。

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 // 啟動(dòng)

安裝 PHP 擴(kuò)展

我這里使用的是 composer 安裝 elasticsearch-php。在 composer.json 文件中加入 "elasticsearch/elasticsearch": "~6.0",執(zhí)行 composer update。

{

 "require": {

  // ...

  "elasticsearch/elasticsearch": "~6.0"

  // ...

 }

}

測(cè)試?yán)?/p>

創(chuàng)建表和測(cè)試數(shù)據(jù)

我這里準(zhǔn)備了一張文章表來(lái)進(jìn)行測(cè)試,首先是建表,其次寫(xiě)入測(cè)試數(shù)據(jù),準(zhǔn)備工作完畢之后,就開(kāi)始編輯測(cè)試用例。

create table articles(

 id int not null primary key auto_increment,

 title varchar(200) not null comment '標(biāo)題',

 content text comment '內(nèi)容'

);

insert into articles(title, content) values ('Laravel 測(cè)試1', 'Laravel 測(cè)試文章內(nèi)容1'),

('Laravel 測(cè)試2', 'Laravel 測(cè)試文章內(nèi)容2'),

('Laravel 測(cè)試3', 'Laravel 測(cè)試文章內(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();

}

實(shí)例化

require './vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

名詞解釋:索引相當(dāng)于 MySQL 中的表,文檔相當(dāng)于 MySQL 中的行記錄

elasticsearch 的動(dòng)態(tài)性質(zhì),在添加第一個(gè)文檔的時(shí)候自動(dòng)創(chuàng)建了索引和一些默認(rèn)設(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做搜索的詳細(xì)內(nèi)容,希望腳本之家整理的內(nèi)容能夠幫助到大家。

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

標(biāo)簽:衢州 蚌埠 江蘇 棗莊 廣元 衡水 大理 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP ElasticSearch做搜索實(shí)例講解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    阜新| 吉安县| 利辛县| 加查县| 盐城市| 莱芜市| 德州市| 贵阳市| 明光市| 稷山县| 阿拉尔市| 肥西县| 宣武区| 沙湾县| 建平县| 五家渠市| 武宁县| 凤台县| 株洲县| 锡林郭勒盟| 垣曲县| 竹溪县| 清新县| 什邡市| 嘉祥县| 四平市| 泗阳县| 丹巴县| 芦山县| 惠来县| 恩平市| 平武县| 常德市| 洞头县| 固阳县| 巴楚县| 梅州市| 柳林县| 万盛区| 房山区| 莱芜市|