hamdallah90/elasticsearch-eloquent

Elasticsearch 功能如 Laravel Eloquent 模型。

dev-master 2023-03-04 11:35 UTC

This package is auto-updated.

Last update: 2024-09-04 15:07:55 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

此包允许您像在 Laravel 中与 Eloquent 模型交互一样与 Elasticsearch 交互。

要求

  • PHP >= 8.2
  • Elasticsearch >= 8.5

安装

通过 Composer

$ composer require hamdallah90/elasticsearch-eloquent

用法

创建一个新模型

您应该重写 indextype 属性以确定文档路径。

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_atupdated_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)。