isswp101 / elasticsearch-eloquent
提供Laravel Eloquent模型功能。
2.0.0
2021-03-27 16:31 UTC
Requires
- php: ^8.0
- elasticsearch/elasticsearch: ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2024-08-28 18:53:30 UTC
README
本包允许您像与Laravel中的Eloquent模型交互一样与Elasticsearch交互。
需求
- PHP >= 8.0
- Elasticsearch >= 7.0
安装
通过Composer
$ composer require isswp101/elasticsearch-eloquent
使用方法
创建新模型
您应该重写index
和type
属性来确定文档路径。
use Isswp101\Persimmon\Models\BaseElasticsearchModel; use Isswp101\Persimmon\Persistence\Persistence; use Isswp101\Persimmon\Contracts\PersistenceContract; class Product extends BaseElasticsearchModel { protected string $index = 'index'; protected string|null $type = 'type'; // optional // If you have a pre-configured Elasticsearch client you can pass it here (optional) public function createPersistence(): PersistenceContract { return new Persistence($client); } }
使用静态create()
方法在Elasticsearch中创建文档
$product = Product::create([ 'id' => 1, 'name' => 'Product', 'price' => 10 ]);
保存模型
$product = new Product(); $product->id = 1; $product->name = 'Product'; $product->price = 10; $product->save();
使用save()
方法将模型数据存储在Elasticsearch中。让我们看看它在Elasticsearch中的样子
{ "_index": "index", "_type": "type", "_id": "1", "_version": 1, "_source": { "id": 1, "name": "Product", "price": 10, "created_at": "2021-03-27T11:24:15+00:00", "updated_at": "2021-03-27T11:24:15+00:00" } }
created_at
和updated_at
字段已自动创建。
查找现有模型
$product = Product::find(1);
如果您在Elasticsearch中有大量数据,您可以指定某些字段进行检索
$product = Product::find(1, ['name']);
以下是一些方法
findOrFail()
如果在查询中没有找到结果,则返回ModelNotFoundException
异常。
缓存
当您使用find()
、findOrFail()
等方法时,会提供智能模型缓存。
$product = Product::find(1, ['name']); // from elasticsearch $product = Product::find(1, ['name']); // from cache $product = Product::find(1, ['price']); // from elasticsearch $product = Product::find(1, ['price']); // from cache $product = Product::find(1, ['name']); // from cache
$product = Product::find(1); // from elasticsearch $product = Product::find(1); // from cache $product = Product::find(1, ['name']); // from cache $product = Product::find(1, ['price']); // from cache
部分更新
您可以使用部分更新快速更新特定字段。
$product = Product::find(1, ['name']); $product->name = 'Name'; $product->save(['name']);
删除模型
$product = Product::find(1); $product->delete();
您可以使用静态方法
Product::destroy(1);
模型事件
默认情况下,您会得到事件的一个简单实现。
您可以重写以下方法来定义事件
saving()
在保存、更新、创建模型之前调用saved()
在保存、更新、创建模型之后调用deleting()
在删除模型之前调用deleted()
在删除模型之后调用searching()
在搜索模型之后调用searched()
在搜索模型之后调用
例如
use Isswp101\Persimmon\Models\BaseElasticsearchModel; class Product extends BaseElasticsearchModel { protected function saving(): bool { // Disable update if it's free return $this->price <= 0; } protected function deleting(): bool { if ($this->user_id != 1) { throw new DomainException('No permissions to delete this model'); } return true; } }
基本搜索
有一些辅助函数用于搜索文档
first($query)
方法根据查询返回第一个文档或null
。
$product = Product::first($query);
firstOrFail($query)
方法如果在first($query)
返回null
,则返回ModelNotFoundException
异常。
$product = Product::firstOrFail($query);
search($query)
方法根据查询返回文档。
$products = Product::search($query);
all($query)
方法根据查询返回所有文档(默认每次请求50个项)。
$products = Product::all($query);
如果没有传递$query
,查询将作为match_all
查询。
查询构建器
考虑使用以下包
测试
$ composer test
许可证
MIT许可证(MIT)。