PHPackages                             isswp101/elasticsearch-eloquent - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. isswp101/elasticsearch-eloquent

ActiveLibrary[Database &amp; ORM](/categories/database)

isswp101/elasticsearch-eloquent
===============================

Elasticsearch functionality like Laravel Eloquent models.

2.0.0(5y ago)11220.7k↓22%14[1 PRs](https://github.com/devemio/elasticsearch-eloquent/pulls)MITPHPPHP ^8.0CI passing

Since Jun 20Pushed 3mo ago5 watchersCompare

[ Source](https://github.com/devemio/elasticsearch-eloquent)[ Packagist](https://packagist.org/packages/isswp101/elasticsearch-eloquent)[ RSS](/packages/isswp101-elasticsearch-eloquent/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (6)Used By (0)

Elasticsearch Eloquent 2.x
==========================

[](#elasticsearch-eloquent-2x)

[![Latest Version on Packagist](https://camo.githubusercontent.com/64f6d8fb16a451469fb8610d615b25223d68f6a7a5f36a823bd82f442b712219/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f69737377703130312f656c61737469637365617263682d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/isswp101/elasticsearch-eloquent)![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)[![Build Status](https://camo.githubusercontent.com/37fece08adca818e1090801e725cc8ecb3c6468a7da9873d68b14eafdfeb0c98/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f646576656d696f2f656c61737469637365617263682d656c6f7175656e742f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/devemio/elasticsearch-eloquent)[![Coverage Status](https://camo.githubusercontent.com/ff9d91f2b0ec33ab913b19100e42badd8a1d4b709fd2c7d4173a6be80bc41f18/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f646576656d696f2f656c61737469637365617263682d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/devemio/elasticsearch-eloquent/code-structure)[![Quality Score](https://camo.githubusercontent.com/7c1da6e619e30eda4aee86b88a47ee9d30853240d190203e5500ae8372c87672/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f646576656d696f2f656c61737469637365617263682d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/devemio/elasticsearch-eloquent)[![Total Downloads](https://camo.githubusercontent.com/46bc30fbf106a564beff2d4448238aba6d307238c7752eb32f5095e7b1117da1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f69737377703130312f656c61737469637365617263682d656c6f7175656e742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/isswp101/elasticsearch-eloquent)

This package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel.

Requirements
------------

[](#requirements)

- PHP &gt;= 8.0
- Elasticsearch &gt;= 7.0

Install
-------

[](#install)

Via Composer

```
$ composer require isswp101/elasticsearch-eloquent
```

Usage
-----

[](#usage)

### Create a new model

[](#create-a-new-model)

You should override `index` and `type` properties to determine the document path.

```
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:

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

### Save the model

[](#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:

```
{
   "_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.

### Find existing model

[](#find-existing-model)

```
$product = Product::find(1);
```

If you have big data in Elasticsearch you can specify certain fields to retrieve:

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

There are the following methods:

- `findOrFail()` returns `ModelNotFoundException` exception if no result found.

### Cache

[](#cache)

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

```
$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

[](#partial-update)

You can use the partial update to update specific fields quickly.

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

### Delete models

[](#delete-models)

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

You can use the static method:

```
Product::destroy(1);
```

### Model events

[](#model-events)

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

- `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:

```
use Isswp101\Persimmon\Models\BaseElasticsearchModel;

class Product extends BaseElasticsearchModel
{
    protected function saving(): bool
    {
        // Disable update if it's free
        return $this->price user_id != 1) {
            throw new DomainException('No permissions to delete this model');
        }

        return true;
    }
}
```

### Basic search

[](#basic-search)

There are helpers to search documents:

The `first($query)` method returns the first document according to the query or `null`.

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

The `firstOrFail($query)` method returns `ModelNotFoundException` exception if `first($query)` returns `null`.

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

The `search($query)` method returns documents according to the query.

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

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

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

If `$query` is not passed the query will be as `match_all` query.

### Query Builder

[](#query-builder)

Consider using these packages:

- [ElasticsearchDSL](https://github.com/ongr-io/ElasticsearchDSL)

Testing
-------

[](#testing)

```
$ composer test
```

License
-------

[](#license)

The MIT License (MIT).

###  Health Score

50

—

FairBetter than 96% of packages

Maintenance53

Moderate activity, may be stable

Popularity43

Moderate usage in the ecosystem

Community15

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~580 days

Total

4

Last Release

1877d ago

Major Versions

1.1.0 → 2.0.02021-03-27

PHP version history (2 changes)1.0.0PHP ~5.6|~7.0

2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/60f5163da22a52a8b8769bb24147f499c657e9c13808c4bf00697f2964821474?d=identicon)[devemio](/maintainers/devemio)

---

Top Contributors

[![bagart](https://avatars.githubusercontent.com/u/2109022?v=4)](https://github.com/bagart "bagart (1 commits)")[![devemio](https://avatars.githubusercontent.com/u/5787193?v=4)](https://github.com/devemio "devemio (1 commits)")

---

Tags

elasticsearchphpelasticsearcheloquent

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/isswp101-elasticsearch-eloquent/health.svg)

```
[![Health](https://phpackages.com/badges/isswp101-elasticsearch-eloquent/health.svg)](https://phpackages.com/packages/isswp101-elasticsearch-eloquent)
```

###  Alternatives

[elasticquent/elasticquent

Maps Laravel Eloquent models to Elasticsearch types.

1.4k945.1k2](/packages/elasticquent-elasticquent)[pdphilip/elasticsearch

An Elasticsearch implementation of Laravel's Eloquent ORM

145360.2k4](/packages/pdphilip-elasticsearch)[sleimanx2/plastic

Plastic is an Elasticsearch ODM and mapper for Laravel. It renders the developer experience more enjoyable while using Elasticsearch by providing a fluent syntax for mapping , querying and storing eloquent models.

508141.9k1](/packages/sleimanx2-plastic)[iverberk/larasearch

Elasticsearch enabled Eloquent models

22415.4k](/packages/iverberk-larasearch)[matchory/elasticsearch

The missing elasticsearch ORM for Laravel!

3059.0k](/packages/matchory-elasticsearch)[designmynight/laravel-elasticsearch

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations.

3038.6k](/packages/designmynight-laravel-elasticsearch)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
