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
首先建立ORM Model
use Ethansmart\EsBuilder\Model\EsModel;
/**
* Class AtPerson
* $host ES IP或URL地址
* $port ES 埠
* $index ES 索引名稱
* $type ES 索引 type名稱
* @package Ethan\EsBuilder\Model
*/
class AtPerson extends EsModel
{
protected $host = "127.0.0.1";
protected $port = "32800";
protected $index = "accounts";
protected $type = "person";
}
然後使用Model對ES進行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
首先構建 Client
private $client ;
public function __construct()
{
$host = "127.0.0.1";
$port = "32800";
$this->client = EsClientBuilder::create()
->setHosts($host)
->setPort($port)
->build();
}
呼叫Client中的方法對ES進行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;
其他方法類似
建立Laravel Job 同步資料到 ES
use Ethansmart\EsBuilder\Builder\EsClientBuilder;
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 安裝過程中會出現 如下異常:
[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.
解決方法:
在專案composer.json檔案中加入:
"repositories": [
{
"packagist.org": false
},
{
"type": "composer",
"url": "https://packagist.org"
}
],
將國內的composer映象換成 packagist.org 就可以了。
本作品採用《CC 協議》,轉載必須註明作者和本文連結