摘要:安裝地址有兩種模式模式支持映射非模式支持原生使用首先創(chuàng)建或地址端口索引名稱索引名稱然后使用對(duì)進(jìn)行操作搜索新增更新刪除使用首先構(gòu)建調(diào)用中的方法對(duì)進(jìn)行操作其他方法類似創(chuàng)建同步數(shù)據(jù)到
composer 安裝:composer require ethansmart/es-for-laravel github 地址:https://github.com/roancsu/es-for-laravel
ES for Laravel
Usage
EsBuilder 有兩種模式
ES ORM Client (ORM模式):支持Model映射
ES Client (非ORM模式):支持原生ES
使用 ES ORM Client
首先創(chuàng)建ORM Model
use EthansmartEsBuilderModelEsModel; /** * Class AtPerson * $host ES IP或URL地址 * $port ES 端口 * $index ES 索引名稱 * $type ES 索引 type名稱 * @package EthanEsBuilderModel */ class AtPerson extends EsModel { protected $host = "127.0.0.1"; protected $port = "32800"; protected $index = "accounts"; protected $type = "person"; }
然后使用Model對(duì)ES進(jìn)行CURD操作
搜索
try { $result = AtPerson::build() ->select("user") ->where("user","==","chengluo") ->where("title,desc","like","AI") ->where("create_time","<","2018-10-05") ->get(); } catch (Exception $e) { return ["code"=>-1, "msg"=>$e->getMessage()]; } return $result;
新增
try { $id = 5; $data = [ "id"=>$id, "params"=>[ "user"=>"Ethan Cheng", "title"=>"AI ".str_random(8), "desc"=>"AI ".str_random(12) ] ]; $result = AtPerson::build()->create($data); } catch (Exception $e) { return ["code"=>-1, "msg"=>$e->getMessage()]; } return $result;
更新
try { $id = 5; $data = [ "id"=>$id, "params"=>[ "user"=>"Ethan Cheng", "title"=>"AI ".str_random(8), "desc"=>"AI ".str_random(12) ] ]; $result = AtPerson::build()->update($data); } catch (Exception $e) { return ["code"=>-1, "msg"=>$e->getMessage()]; } return $result;
刪除
try { $id = 5; $result = AtPerson::build()->delete($id); } catch (Exception $e) { throw $e; } return $result;
使用 ES Client
首先構(gòu)建 Client
private $client ; public function __construct() { $host = "127.0.0.1"; $port = "32800"; $this->client = EsClientBuilder::create() ->setHosts($host) ->setPort($port) ->build(); }
調(diào)用Client中的方法對(duì)ES進(jìn)行CURD操作
$data = [ "index"=>"accounts", "type"=>"person", "body"=>[ "query"=>[ "bool"=>[ "must"=>[ "match"=>[ "user"=>"ethan" ] ] ] ] ], ]; try { $result = $this->client->search($data); } catch (Exception $e) { return ["code"=>-1, "msg"=>$e->getMessage()]; } return $result;
其他方法類似
創(chuàng)建Laravel Job 同步數(shù)據(jù)到 ES
use EthansmartEsBuilderBuilderEsClientBuilder; class EsService { private $client ; public function __construct() { $host = "127.0.0.1"; $port = "32800"; $this->client = EsClientBuilder::create() ->setHosts($host) ->setPort($port) ->build(); } public function create($id) { $data = [ "index"=>"accounts", "type"=>"person", "id"=>$id, "body"=>[ "user"=>str_random(6), "title"=>str_random(12), "desc"=>str_random(16), ] ]; try { $result = $this->client->create($data); } catch (Exception $e) { return ["code"=>-1, "msg"=>$e->getMessage()]; } return $result; } }
Q:
在使用 composer 安裝過程中會(huì)出現(xiàn) 如下異常:
[InvalidArgumentException]
Could not find a version of package ethansmart/es-for-laravel matching your minimum-stability (stable). Require it with an explicit version constraint allowing its desired stability.
解決方法:
在項(xiàng)目composer.json文件中加入:
"repositories": [ { "packagist.org": false }, { "type": "composer", "url": "https://packagist.org" } ],
將國(guó)內(nèi)的composer鏡像換成 packagist.org 就可以了。
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/29479.html
摘要:完全沒有基礎(chǔ)的人基本上很難安裝并使用成功。目錄用于存放的依賴包。開啟訪問即可看到有的字樣出現(xiàn),代表安裝配置成功。 最近開始開坑,玩php,而且laravel最近很熱門,所以就這樣了。小白php直接接觸框架真的不容易,以前python接觸的django框架看官網(wǎng)文檔覺得寫得勉強(qiáng)吧,不是太好看,直到看到laravel的文檔才知道什么叫變態(tài)。完全沒有基礎(chǔ)的人基本上很難安裝并使用成功。而且即使...
摘要:但也因?yàn)閼?yīng)有盡有使得框架的性能比其他高性能框架低了些,為此給了幾個(gè)解決方案路由緩存經(jīng)有關(guān)部門研究,路由緩存可有效加快訪問速度以上。有朋友說框架最重要的東西是路由,我倒認(rèn)為最重要的是框架中異于其他框架且能解決痛點(diǎn)的東西,如的。 showImg(https://segmentfault.com/img/remote/1460000006767764); 在程序界的遠(yuǎn)古時(shí)期,大神們手持鍵盤敲...
摘要:流程一新項(xiàng)目流程創(chuàng)建,并添加依賴到的擴(kuò)展包運(yùn)行,安裝擴(kuò)展包并生成提交到代碼版本控制器中,如流程二項(xiàng)目協(xié)作者安裝現(xiàn)有項(xiàng)目克隆項(xiàng)目后,根目錄下直接運(yùn)行從中安裝指定版本的擴(kuò)展包以及其依賴此流程適用于生產(chǎn)環(huán)境代碼的部署。 本文經(jīng)授權(quán)轉(zhuǎn)自 PHPHub 社區(qū) 問題說明 我們經(jīng)常要往現(xiàn)有的項(xiàng)目中添加擴(kuò)展包,有時(shí)候因?yàn)槲臋n的錯(cuò)誤引導(dǎo),如下圖來自 這個(gè)文檔 的: showImg(https://dn-...
摘要:原文發(fā)表在我的個(gè)人網(wǎng)站系列入門教程一最適合中國(guó)人的教程本教程示例代碼見大家在任何地方卡住,最快捷的解決方式就是去看我的示例代碼。在此我推薦一個(gè)全量中國(guó)鏡像。 原文發(fā)表在我的個(gè)人網(wǎng)站:Laravel 5 系列入門教程(一)【最適合中國(guó)人的 Laravel 教程】 本教程示例代碼見:https://github.com/johnlui/Learn-Laravel-5 大...
閱讀 3595·2023-04-26 02:05
閱讀 2039·2021-11-19 11:30
閱讀 4257·2021-09-30 09:59
閱讀 3203·2021-09-10 10:51
閱讀 2637·2021-09-01 10:30
閱讀 1530·2021-08-11 11:20
閱讀 2644·2019-08-30 15:54
閱讀 589·2019-08-30 10:49