es-for-Laravel: Composer 包安装, Laravel 最简单的方式操作 Elasticsearch
2023-09-27 14:26:43 时间
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 就可以了。
相关文章
- es备份和恢复
- es备份迁移到另外的新机器
- es学习:在windows中es 2.0.0的安装
- Es中查询数据存在某个字段或者数据的不存在某个字段(must_not,must的使用)
- ES 常用Linux查询命令汇总
- mysql 与elasticsearch实时同步常用插件及优缺点对比(ES与关系型数据库同步)
- 白日梦的Elasticsearch系列笔记(一)基础篇-- 快手上手ES (二)
- ES单字段支持的最大字符数
- Elasticsearch教程(31) es mapping参数doc_values enabled ignore_above norms store详解
- cocos2d-x于android在call to OpenGL ES API with no current context
- NDK OpenGL ES 3.0 开发(九):光照基础
- ES 基本操作
- Android OpenGL ES 学习(四) -- 正交投影
- Android OpenGL ES 学习(二) -- 图形渲染管线和GLSL
- ES优化总结
- 详细说说ES的source字段(含源码分析)
- ES系列之原来ES的聚合统计不准确啊