thinkphp5 使用ElasticSearch 做搜索
一,安裝Java SE環(huán)境
在安裝Elasticsearch引擎之前,必須安裝ES需要的軟件環(huán)境,安裝Java JDK和配置JAVA_HOME環(huán)境變量:
1,從https://www.oracle.com/technetwork/java/javase/downloads/index.html下載和安裝Java SE開發(fā)包,我下載的是最新版本如圖:
ElasticSearch對(duì)JRE的版本是敏感的,錯(cuò)誤的版本,會(huì)導(dǎo)致ElasticSearch無(wú)法運(yùn)行,下載歸檔的JRE,請(qǐng)從Java Archive下載合適的版本。
2,Java SE開發(fā)包安裝完成之后,需要在服務(wù)器上創(chuàng)建JAVA_HOME環(huán)境變量
環(huán)境變量 JAVA_HOME,設(shè)置變量值是:C:\Program Files\Java\jdk-11.0.2
注釋:在Windows系統(tǒng)中,“%環(huán)境變量名%”用法的含義是獲取指定環(huán)境變量的值,創(chuàng)建JAVA_HOME環(huán)境變量的作用,是由于安裝ElasticSearch需要引用Java SE開發(fā)包。
二,安裝ElasticSearch服務(wù)
https://www.elastic.co/downloads/past-releases? 下載對(duì)應(yīng)的版本 我下載是5.4.0
?*?php使用的版本要與ElasticSearch服務(wù)對(duì)應(yīng)!
開啟ElasticSearch 服務(wù)
進(jìn)入?elasticsearch目錄的bin,雙擊執(zhí)行?elasticsearch.bat,該腳本文件執(zhí)行 ElasticSearch 安裝程序,稍等片刻,打開瀏覽器,輸入?http://localhost:9200?,顯式以下畫面,說(shuō)明ES安裝成功。
安裝中文分詞ik
下載 ik? https://github.com/medcl/elasticsearch-analysis-ik/releases
我的es版本是 5.4.0? ?ik 選擇 5.4.0? 必須找到對(duì)應(yīng)的版本!!!!!!
下載后解壓到一個(gè)文件夾下
并把解壓后的內(nèi)容放到 D:\elasticsearch-5.4.0\plugins\ik?文件夾下? 沒(méi)有ik文件夾自己建一個(gè)
成功后重啟ElasticSearch 服務(wù)? ok!!??ElasticSearch服務(wù)安裝成功!!!
項(xiàng)目?jī)?nèi)安裝ElasticSearch?
composer require elasticsearch/elasticsearch會(huì)自動(dòng)加載合適的版本!5.4.0的elasticsearch版本
?
?此時(shí)elasticsearch和php支持都安裝成功!
thinkphp5內(nèi)簡(jiǎn)單使用
先上代碼? ? 下面的方法是官方的一些樣例整合
<?php namespace app\index\controller; require '../vendor/autoload.php'; use Elasticsearch\ClientBuilder; class Search {private $client;// 構(gòu)造函數(shù)public function __construct(){$params = array('127.0.0.1:9200');$this->client = ClientBuilder::create()->setHosts($params)->build();}// 創(chuàng)建索引public function index() { // 只能創(chuàng)建一次$r = $this->delete_index();$r = $this->create_index(); //1.創(chuàng)建索引$r = $this->create_mappings(); //2.創(chuàng)建文檔模板$r = $this->get_mapping();$docs = [];$docs[] = ['id'=>1,'name'=>'小明','profile'=>'我做的ui界面強(qiáng)無(wú)敵。','age'=>23];$docs[] = ['id'=>2,'name'=>'小張','profile'=>'我的php代碼無(wú)懈可擊。','age'=>24];$docs[] = ['id'=>3,'name'=>'小王','profile'=>'C的生活,快樂(lè)每一天。','age'=>29];$docs[] = ['id'=>4,'name'=>'小趙','profile'=>'就沒(méi)有我做不出的前端頁(yè)面。','age'=>26];$docs[] = ['id'=>5,'name'=>'小吳','profile'=>'php是最好的語(yǔ)言。','job'=>21];$docs[] = ['id'=>6,'name'=>'小翁','profile'=>'別煩我,我正在敲bug呢!','age'=>25];$docs[] = ['id'=>7,'name'=>'小楊','profile'=>'為所欲為,不行就刪庫(kù)跑路','age'=>27];foreach ($docs as $k => $v) {$r = $this->add_doc($v['id'],$v); //3.添加文檔}$r = $this->search_doc("刪庫(kù) 別煩我"); //4.搜索結(jié)果}// 創(chuàng)建索引public function create_index($index_name = 'test_ik') { // 只能創(chuàng)建一次$params = ['index' => $index_name,'body' => ['settings' => ['number_of_shards' => 5,'number_of_replicas' => 0]]];try {return $this->client->indices()->create($params);} catch (Elasticsearch\Common\Exceptions\BadRequest400Exception $e) {$msg = $e->getMessage();$msg = json_decode($msg,true);return $msg;}}// 刪除索引public function delete_index($index_name = 'test_ik') {$params = ['index' => $index_name];$response = $this->client->indices()->delete($params);return $response;}// 創(chuàng)建文檔模板public function create_mappings($type_name = 'users',$index_name = 'test_ik') {$params = ['index' => $index_name,'type' => $type_name,'body' => [$type_name => ['_source' => ['enabled' => true],'properties' => ['id' => ['type' => 'integer', // 整型'index' => 'not_analyzed',],'name' => ['type' => 'string', // 字符串型'index' => 'analyzed', // 全文搜索'analyzer' => 'ik_max_word'],'profile' => ['type' => 'string','index' => 'analyzed','analyzer' => 'ik_max_word'],'age' => ['type' => 'integer',],]]]];$response = $this->client->indices()->putMapping($params);return $response;}// 查看映射public function get_mapping($type_name = 'users',$index_name = 'test_ik') {$params = ['index' => $index_name,'type' => $type_name];$response = $this->client->indices()->getMapping($params);return $response;}// 添加文檔public function add_doc($id,$doc,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name,'type' => $type_name,'id' => $id,'body' => $doc];$response = $this->client->index($params);return $response;}// 判斷文檔存在public function exists_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name,'type' => $type_name,'id' => $id];$response = $this->client->exists($params);return $response;}// 獲取文檔public function get_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name,'type' => $type_name,'id' => $id];$response = $this->client->get($params);return $response;}// 更新文檔public function update_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {// 可以靈活添加新字段,最好不要亂添加$params = ['index' => $index_name,'type' => $type_name,'id' => $id,'body' => ['doc' => ['name' => '大王']]];$response = $this->client->update($params);return $response;}// 刪除文檔public function delete_doc($id = 1,$index_name = 'test_ik',$type_name = 'users') {$params = ['index' => $index_name,'type' => $type_name,'id' => $id];$response = $this->client->delete($params);return $response;}// 查詢文檔 (分頁(yè),排序,權(quán)重,過(guò)濾)public function search_doc($keywords = "運(yùn)維",$index_name = "test_ik",$type_name = "users",$from = 0,$size = 2) {$params = ['index' => $index_name,'type' => $type_name,'body' => ['query' => ['bool' => ['should' => [[ 'match' => [ 'profile' => ['query' => $keywords,'boost' => 3, // 權(quán)重大]]],[ 'match' => [ 'name' => ['query' => $keywords,'boost' => 2,]]],],],],'sort' => ['age'=>['order'=>'desc']], 'from' => $from, 'size' => $size]];$results = $this->client->search($params);return $results;}}按1,2,3,4步驟進(jìn)行?,第三步就可以把數(shù)據(jù)庫(kù)數(shù)據(jù)導(dǎo)入。
?
搜索成功!?
總結(jié)
以上是生活随笔為你收集整理的thinkphp5 使用ElasticSearch 做搜索的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: oracle数据库数据合并,Oracle
- 下一篇: elastica php yii,Yii