{"id":17461796,"url":"https://github.com/devemio/elasticsearch-eloquent","last_synced_at":"2025-04-09T13:08:13.141Z","repository":{"id":53269403,"uuid":"53619816","full_name":"devemio/elasticsearch-eloquent","owner":"devemio","description":"⚡️ Models like Eloquent for Elasticsearch.","archived":false,"fork":false,"pushed_at":"2021-03-28T11:30:32.000Z","size":162,"stargazers_count":111,"open_issues_count":0,"forks_count":14,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-02T06:53:08.852Z","etag":null,"topics":["elasticsearch","php"],"latest_commit_sha":null,"homepage":"https://devemio.github.io/elasticsearch-eloquent","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devemio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-10T21:55:18.000Z","updated_at":"2024-11-02T19:20:01.000Z","dependencies_parsed_at":"2022-08-21T13:20:35.270Z","dependency_job_id":null,"html_url":"https://github.com/devemio/elasticsearch-eloquent","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devemio%2Felasticsearch-eloquent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devemio%2Felasticsearch-eloquent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devemio%2Felasticsearch-eloquent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devemio%2Felasticsearch-eloquent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devemio","download_url":"https://codeload.github.com/devemio/elasticsearch-eloquent/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045233,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["elasticsearch","php"],"created_at":"2024-10-18T07:23:17.671Z","updated_at":"2025-04-09T13:08:13.117Z","avatar_url":"https://github.com/devemio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Elasticsearch Eloquent 2.x\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]]()\n[![Build Status][ico-travis]][link-travis]\n[![Coverage Status][ico-scrutinizer]][link-scrutinizer]\n[![Quality Score][ico-code-quality]][link-code-quality]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nThis package allows you to interact with Elasticsearch as you interact with Eloquent models in Laravel.\n\n## Requirements\n\n- PHP \u003e= 8.0\n- Elasticsearch \u003e= 7.0\n\n## Install\n\nVia Composer\n\n```bash\n$ composer require isswp101/elasticsearch-eloquent\n```\n\n## Usage\n\n### Create a new model\n\nYou should override `index` and `type` properties to determine the document path.\n\n```php\nuse Isswp101\\Persimmon\\Models\\BaseElasticsearchModel;\nuse Isswp101\\Persimmon\\Persistence\\Persistence;\nuse Isswp101\\Persimmon\\Contracts\\PersistenceContract;\n\nclass Product extends BaseElasticsearchModel\n{\n    protected string $index = 'index';\n    protected string|null $type = 'type'; // optional\n\n    // If you have a pre-configured Elasticsearch client you can pass it here (optional)\n    public function createPersistence(): PersistenceContract\n    {\n        return new Persistence($client);\n    }\n}\n```\n\nUse the static `create()` method to create the document in Elasticsearch:\n\n```php\n$product = Product::create([\n    'id' =\u003e 1, \n    'name' =\u003e 'Product',\n    'price' =\u003e 10\n]);\n```\n\n### Save the model\n\n```php\n$product = new Product();\n$product-\u003eid = 1;\n$product-\u003ename = 'Product';\n$product-\u003eprice = 10;\n$product-\u003esave();\n```\n\nUse `save()` method to store model data in Elasticsearch. Let's see how this looks in Elasticsearch:\n\n```json\n{\n   \"_index\": \"index\",\n   \"_type\": \"type\",\n   \"_id\": \"1\",\n   \"_version\": 1,\n   \"_source\": {\n      \"id\": 1,\n      \"name\": \"Product\",\n      \"price\": 10,\n      \"created_at\": \"2021-03-27T11:24:15+00:00\",\n      \"updated_at\": \"2021-03-27T11:24:15+00:00\"\n   }\n}\n```\n\nFields `created_at` and `updated_at` were created automatically.\n\n### Find existing model\n\n```php\n$product = Product::find(1);\n```\n\nIf you have big data in Elasticsearch you can specify certain fields to retrieve:\n\n```php\n$product = Product::find(1, ['name']);\n```\n\nThere are the following methods:\n* `findOrFail()` returns `ModelNotFoundException` exception if no result found.\n\n### Cache\n\nThere is a smart model cache when you use methods like `find()`, `findOrFail()` and so on.\n\n```php\n$product = Product::find(1, ['name']);  // from elasticsearch\n$product = Product::find(1, ['name']);  // from cache\n$product = Product::find(1, ['price']); // from elasticsearch\n$product = Product::find(1, ['price']); // from cache\n$product = Product::find(1, ['name']);  // from cache\n```\n\n```php\n$product = Product::find(1);            // from elasticsearch\n$product = Product::find(1);            // from cache\n$product = Product::find(1, ['name']);  // from cache\n$product = Product::find(1, ['price']); // from cache\n```\n\n### Partial update\n\nYou can use the partial update to update specific fields quickly.\n\n```php\n$product = Product::find(1, ['name']);\n$product-\u003ename = 'Name';\n$product-\u003esave(['name']);\n```\n\n### Delete models\n\n```php\n$product = Product::find(1);\n$product-\u003edelete();\n```\n\nYou can use the static method:\n\n```php\nProduct::destroy(1);\n```\n\n### Model events\n\nOut of the box you are provided with a simple implementation of events.  \nYou can override the following methods to define events:\n\n* `saving()` is called before saving, updating, creating the model\n* `saved()` is called after saving, updating, creating the model\n* `deleting()` is called before deleting the model\n* `deleted()` is called after deleting the model\n* `searching()` is called after searching models\n* `searched()` is called after searching models\n\nFor example:\n\n```php\nuse Isswp101\\Persimmon\\Models\\BaseElasticsearchModel;\n\nclass Product extends BaseElasticsearchModel\n{\n    protected function saving(): bool\n    {\n        // Disable update if it's free\n        return $this-\u003eprice \u003c= 0;\n    }\n\n    protected function deleting(): bool\n    {\n        if ($this-\u003euser_id != 1) {\n            throw new DomainException('No permissions to delete this model');\n        }\n\n        return true;\n    }\n}\n```\n\n### Basic search\n\nThere are helpers to search documents:\n\nThe `first($query)` method returns the first document according to the query or `null`.  \n\n```php\n$product = Product::first($query);\n```\n\nThe `firstOrFail($query)` method returns `ModelNotFoundException` exception if `first($query)` returns `null`.\n\n```php\n$product = Product::firstOrFail($query);\n```\n\nThe `search($query)` method returns documents according to the query.\n\n```php\n$products = Product::search($query);\n```\n\nThe `all($query)` method returns all documents (default 50 items per request) according to the query.\n\n```php\n$products = Product::all($query);\n```\n\nIf `$query` is not passed the query will be as `match_all` query.\n\n### Query Builder\n\nConsider using these packages:\n\n- [ElasticsearchDSL](https://github.com/ongr-io/ElasticsearchDSL)\n\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n\n## License\n\nThe MIT License (MIT).\n\n[ico-version]: https://img.shields.io/packagist/v/isswp101/elasticsearch-eloquent.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/isswp101/elasticsearch-eloquent.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-travis]: https://img.shields.io/travis/devemio/elasticsearch-eloquent/master.svg?style=flat-square\n[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/devemio/elasticsearch-eloquent.svg?style=flat-square\n[ico-code-quality]: https://img.shields.io/scrutinizer/g/devemio/elasticsearch-eloquent.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/isswp101/elasticsearch-eloquent\n[link-downloads]: https://packagist.org/packages/isswp101/elasticsearch-eloquent\n[link-travis]: https://travis-ci.org/devemio/elasticsearch-eloquent\n[link-scrutinizer]: https://scrutinizer-ci.com/g/devemio/elasticsearch-eloquent/code-structure\n[link-code-quality]: https://scrutinizer-ci.com/g/devemio/elasticsearch-eloquent\n[link-author]: https://github.com/devemio\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevemio%2Felasticsearch-eloquent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevemio%2Felasticsearch-eloquent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevemio%2Felasticsearch-eloquent/lists"}