php操作ElasticSearch搜索引擎流程詳解
〝 古人學(xué)問(wèn)遺無(wú)力,少壯功夫老始成 〞
如果這篇文章能給你帶來(lái)一點(diǎn)幫助,希望給飛兔小哥哥一鍵三連,表示支持,謝謝各位小伙伴們。
一、安裝
通過(guò)composer安裝
composer require 'elasticsearch/elasticsearch'
二、使用
創(chuàng)建ES類
<?php require 'vendor/autoload.php'; //如果未設(shè)置密碼 $es = \Elasticsearch\ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build(); //如果es設(shè)置了密碼 $es = \Elasticsearch\ClientBuilder::create()->setHosts(['http://username:password@xxx.xxx.xxx.xxx:9200'])->build()
三、新建ES數(shù)據(jù)庫(kù)
index 對(duì)應(yīng)關(guān)系型數(shù)據(jù)(以下簡(jiǎn)稱MySQL)里面的數(shù)據(jù)庫(kù),而不是對(duì)應(yīng)MySQL里面的索引
<?php
$params = [
'index' => 'autofelix_db', #index的名字不能是大寫(xiě)和下劃線開(kāi)頭
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0
]
]
];
$es->indices()->create($params);
四、創(chuàng)建表
- 在MySQL里面,光有了數(shù)據(jù)庫(kù)還不行,還需要建立表,ES也是一樣的
- ES中的type對(duì)應(yīng)MySQL里面的表
- ES6以前,一個(gè)index有多個(gè)type,就像MySQL中一個(gè)數(shù)據(jù)庫(kù)有多個(gè)表一樣
- 但是ES6以后,每個(gè)index只允許一個(gè)type
- 在定義字段的時(shí)候,可以看出每個(gè)字段可以定義單獨(dú)的類型
- 在first_name中還自定義了 分詞器 ik,這是個(gè)插件,是需要單獨(dú)安裝的
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'mytype' => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$es->indices()->putMapping($params);
五、插入數(shù)據(jù)
- 現(xiàn)在數(shù)據(jù)庫(kù)和表都有了,可以往里面插入數(shù)據(jù)了
- 在ES里面的數(shù)據(jù)叫文檔
- 可以多插入一些數(shù)據(jù),等會(huì)可以模擬搜索功能
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
//'id' => 1, #可以手動(dòng)指定id,也可以不指定隨機(jī)生成
'body' => [
'first_name' => '飛',
'last_name' => '兔',
'age' => 26
]
];
$es->index($params);
六、 查詢所有數(shù)據(jù)
<?php $data = $es->search(); var_dump($data);
七、查詢單條數(shù)據(jù)
- 如果你在插入數(shù)據(jù)的時(shí)候指定了id,就可以查詢的時(shí)候加上id
- 如果你在插入的時(shí)候未指定id,系統(tǒng)將會(huì)自動(dòng)生成id,你可以通過(guò)查詢所有數(shù)據(jù)后查看其id
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'id' => //你插入數(shù)據(jù)時(shí)候的id
];
$data = $es->get($params);
八、搜索
ES精髓的地方就在于搜索
<?php
$params = [
'index' => 'autofelix_db',
'type' => 'autofelix_table',
'body' => [
'query' => [
'constant_score' => [ //非評(píng)分模式執(zhí)行
'filter' => [ //過(guò)濾器,不會(huì)計(jì)算相關(guān)度,速度快
'term' => [ //精確查找,不支持多個(gè)條件
'first_name' => '飛'
]
]
]
]
]
];
$data = $es->search($params);
var_dump($data);
九、測(cè)試代碼
基于Laravel環(huán)境,包含刪除數(shù)據(jù)庫(kù),刪除文檔等操作
<?php
use Elasticsearch\ClientBuilder;
use Faker\Generator as Faker;
/**
* ES 的 php 實(shí)測(cè)代碼
*/
class EsDemo
{
private $EsClient = null;
private $faker = null;
/**
* 為了簡(jiǎn)化測(cè)試,本測(cè)試默認(rèn)只操作一個(gè)Index,一個(gè)Type
*/
private $index = 'autofelix_db';
private $type = 'autofelix_table';
public function __construct(Faker $faker)
{
/**
* 實(shí)例化 ES 客戶端
*/
$this->EsClient = ClientBuilder::create()->setHosts(['xxx.xxx.xxx.xxx'])->build();
/**
* 這是一個(gè)數(shù)據(jù)生成庫(kù)
*/
$this->faker = $faker;
}
/**
* 批量生成文檔
* @param $num
*/
public function generateDoc($num = 100) {
foreach (range(1,$num) as $item) {
$this->putDoc([
'first_name' => $this->faker->name,
'last_name' => $this->faker->name,
'age' => $this->faker->numberBetween(20,80)
]);
}
}
/**
* 刪除一個(gè)文檔
* @param $id
* @return array
*/
public function delDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->delete($params);
}
/**
* 搜索文檔,query是查詢條件
* @param array $query
* @param int $from
* @param int $size
* @return array
*/
public function search($query = [], $from = 0, $size = 5) {
// $query = [
// 'query' => [
// 'bool' => [
// 'must' => [
// 'match' => [
// 'first_name' => 'Cronin',
// ]
// ],
// 'filter' => [
// 'range' => [
// 'age' => ['gt' => 76]
// ]
// ]
// ]
//
// ]
// ];
$params = [
'index' => $this->index,
// 'index' => 'm*', #index 和 type 是可以模糊匹配的,甚至這兩個(gè)參數(shù)都是可選的
'type' => $this->type,
'_source' => ['first_name','age'], // 請(qǐng)求指定的字段
'body' => array_merge([
'from' => $from,
'size' => $size
],$query)
];
return $this->EsClient->search($params);
}
/**
* 一次獲取多個(gè)文檔
* @param $ids
* @return array
*/
public function getDocs($ids) {
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => ['ids' => $ids]
];
return $this->EsClient->mget($params);
}
/**
* 獲取單個(gè)文檔
* @param $id
* @return array
*/
public function getDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id
];
return $this->EsClient->get($params);
}
/**
* 更新一個(gè)文檔
* @param $id
* @return array
*/
public function updateDoc($id) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' =>$id,
'body' => [
'doc' => [
'first_name' => '張',
'last_name' => '三',
'age' => 99
]
]
];
return $this->EsClient->update($params);
}
/**
* 添加一個(gè)文檔到 Index 的Type中
* @param array $body
* @return void
*/
public function putDoc($body = []) {
$params = [
'index' => $this->index,
'type' => $this->type,
// 'id' => 1, #可以手動(dòng)指定id,也可以不指定隨機(jī)生成
'body' => $body
];
$this->EsClient->index($params);
}
/**
* 刪除所有的 Index
*/
public function delAllIndex() {
$indexList = $this->esStatus()['indices'];
foreach ($indexList as $item => $index) {
$this->delIndex();
}
}
/**
* 獲取 ES 的狀態(tài)信息,包括index 列表
* @return array
*/
public function esStatus() {
return $this->EsClient->indices()->stats();
}
/**
* 創(chuàng)建一個(gè)索引 Index (非關(guān)系型數(shù)據(jù)庫(kù)里面那個(gè)索引,而是關(guān)系型數(shù)據(jù)里面的數(shù)據(jù)庫(kù)的意思)
* @return void
*/
public function createIndex() {
$this->delIndex();
$params = [
'index' => $this->index,
'body' => [
'settings' => [
'number_of_shards' => 2,
'number_of_replicas' => 0
]
]
];
$this->EsClient->indices()->create($params);
}
/**
* 檢查Index 是否存在
* @return bool
*/
public function checkIndexExists() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->exists($params);
}
/**
* 刪除一個(gè)Index
* @return void
*/
public function delIndex() {
$params = [
'index' => $this->index
];
if ($this->checkIndexExists()) {
$this->EsClient->indices()->delete($params);
}
}
/**
* 獲取Index的文檔模板信息
* @return array
*/
public function getMapping() {
$params = [
'index' => $this->index
];
return $this->EsClient->indices()->getMapping($params);
}
/**
* 創(chuàng)建文檔模板
* @return void
*/
public function createMapping() {
$this->createIndex();
$params = [
'index' => $this->index,
'type' => $this->type,
'body' => [
$this->type => [
'_source' => [
'enabled' => true
],
'properties' => [
'id' => [
'type' => 'integer'
],
'first_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'last_name' => [
'type' => 'text',
'analyzer' => 'ik_max_word'
],
'age' => [
'type' => 'integer'
]
]
]
]
];
$this->EsClient->indices()->putMapping($params);
$this->generateDoc();
}
}
到此這篇關(guān)于php操作ElasticSearch搜索引擎流程詳解的文章就介紹到這了,更多相關(guān)php ElasticSearch搜索引擎內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
php啟用sphinx全文搜索的實(shí)現(xiàn)方法
這篇文章主要介紹了php啟用sphinx全文搜索的實(shí)現(xiàn)方法,詳細(xì)講述了sphinx相關(guān)的配置與使用技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
php數(shù)字每三位加逗號(hào)的功能函數(shù)
這篇文章主要介紹了php數(shù)字每三位加逗號(hào)的功能函數(shù),想知道如何做到數(shù)字每三位加逗號(hào)的朋友可以參考下2015-10-10
PHP中call_user_func_array回調(diào)函數(shù)的用法示例
這篇文章主要給大家介紹了PHP中call_user_func_array回調(diào)函數(shù)的用法,文中給出了詳細(xì)的示例代碼,相信對(duì)大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11
PHP中根據(jù)IP地址判斷城市實(shí)現(xiàn)城市切換或跳轉(zhuǎn)代碼
先要獲取ip地址相當(dāng)簡(jiǎn)單,下面先介紹兩種獲取IP地址的代碼,后面需要利用QQIP庫(kù)來(lái)查找當(dāng)前IP是屬于那個(gè)IP段然后得出城市字段并返回2012-09-09
php中ob(Output Buffer 輸出緩沖)函數(shù)使用方法
php中ob(Output Buffer 輸出緩沖)函數(shù)使用方法...2007-07-07
PHP常見(jiàn)的6個(gè)錯(cuò)誤提示及解決方法
自學(xué)黨們?cè)谧詫W(xué)php的時(shí)候,經(jīng)常會(huì)發(fā)生一些小錯(cuò)誤,這篇文章主要介紹了PHP常見(jiàn)的6個(gè)錯(cuò)誤提示及解決方法,感興趣的小伙伴們可以參考一下2016-07-07
PHP 中關(guān)于ord($str)>0x80的詳細(xì)說(shuō)明
為了識(shí)別雙字節(jié)的字符,比如漢字或日文韓文等都是占兩字節(jié)的,每字節(jié)高位為1,而一般西文字符只有一個(gè)字節(jié),七位有效編碼,高位為0而0x80對(duì)應(yīng)的二進(jìn)制代碼為1000 0000,最高位為一,代表漢字.漢字編碼格式通稱為10格式. 一個(gè)漢字占2字節(jié),但只代表一個(gè)字符2012-09-09

