2017 © Pedro Peláez
 

library elasticsearch-eloquent

Elasticsearch functionality like Laravel Eloquent models

image

isswp101/elasticsearch-eloquent

Elasticsearch functionality like Laravel Eloquent models

  • Monday, March 20, 2017
  • by isswp101
  • Repository
  • 6 Watchers
  • 95 Stars
  • 5,307 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 6 Open issues
  • 5 Versions
  • 9 % Grown

The README.md

Elasticsearch Eloquent 2.x

Latest Version on Packagist Software License ![Build Status][ico-travis] Coverage Status ![Quality Score][ico-code-quality] ![Total Downloads][ico-downloads], (*1)

This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel., (*2)

Requirements

  • PHP >= 8.0
  • Elasticsearch >= 7.0

Install

Via Composer, (*3)

$ composer require isswp101/elasticsearch-eloquent

Usage

Create a new model

You should override index and type properties to determine the document path., (*4)

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);
    }
}

Use the static create() method to create the document in Elasticsearch:, (*5)

$product = Product::create([
    'id' => 1, 
    'name' => 'Product',
    'price' => 10
]);

Save the model

$product = new Product();
$product->id = 1;
$product->name = 'Product';
$product->price = 10;
$product->save();

Use save() method to store model data in Elasticsearch. Let's see how this looks in Elasticsearch:, (*6)

{
   "_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"
   }
}

Fields created_at and updated_at were created automatically., (*7)

Find existing model

$product = Product::find(1);

If you have big data in Elasticsearch you can specify certain fields to retrieve:, (*8)

$product = Product::find(1, ['name']);

There are the following methods: * findOrFail() returns ModelNotFoundException exception if no result found., (*9)

Cache

There is a smart model cache when you use methods like find(), findOrFail() and so on., (*10)

$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

Partial update

You can use the partial update to update specific fields quickly., (*11)

$product = Product::find(1, ['name']);
$product->name = 'Name';
$product->save(['name']);

Delete models

$product = Product::find(1);
$product->delete();

You can use the static method:, (*12)

Product::destroy(1);

Model events

Out of the box you are provided with a simple implementation of events.
You can override the following methods to define events:, (*13)

  • saving() is called before saving, updating, creating the model
  • saved() is called after saving, updating, creating the model
  • deleting() is called before deleting the model
  • deleted() is called after deleting the model
  • searching() is called after searching models
  • searched() is called after searching models

For example:, (*14)

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;
    }
}

There are helpers to search documents:, (*15)

The first($query) method returns the first document according to the query or null., (*16)

$product = Product::first($query);

The firstOrFail($query) method returns ModelNotFoundException exception if first($query) returns null., (*17)

$product = Product::firstOrFail($query);

The search($query) method returns documents according to the query., (*18)

$products = Product::search($query);

The all($query) method returns all documents (default 50 items per request) according to the query., (*19)

$products = Product::all($query);

If $query is not passed the query will be as match_all query., (*20)

Query Builder

Consider using these packages:, (*21)

Testing

bash $ composer test, (*22)

License

The MIT License (MIT)., (*23)

The Versions

20/03 2017

dev-feature/update-infrastructure

dev-feature/update-infrastructure https://github.com/isswp101/elasticsearch-eloquent

Elasticsearch functionality like Laravel Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent elasticsearch

07/03 2017

dev-feature/5.0-elasticsearch

dev-feature/5.0-elasticsearch https://github.com/isswp101/elasticsearch-eloquent

Elasticsearch functionality like Laravel Eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

eloquent elasticsearch