PHPackages                             mawebcoder/laravel-elasticsearch - 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. mawebcoder/laravel-elasticsearch

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

mawebcoder/laravel-elasticsearch
================================

laravel package for Elasticsearch ORM

v1.0.0(1y ago)02MIT

Since Apr 19Pushed 1y ago1 watchersCompare

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

READMEChangelogDependencies (4)Versions (3)Used By (0)

laravel-elasticsearch (Elastiquent-PHP)
=======================================

[](#laravel-elasticsearch-elastiquent-php)

By using an Eloquent-like query builder, you can tame the beast without having to worry about Elasticsearch's monstrous syntax.

You can use this package without needing to master Elasticsearch deeply and save your time by not having to memorize the higherarchical nested syntax.

Dependencies
------------

[](#dependencies)

Elasticsearch VersionPackage VersionPHP VersionLaravel7.17.92.6.3 and lower^8.1^9.0^8.0^3.0^8.1^9.0Config file and Elasticsearch migration log
===========================================

[](#config-file-and-elasticsearch-migration-log)

In order to be able to save logs related to migrations and customize your configurations, you need to run command below:

`php artisan vendor:publish --tag=elastic`

Then migrate your database :

`php artisan migrate`

Config
======

[](#config)

After publishing the config file, you will see the following content

```

return [
    'index_prefix' => env('APP_NAME', 'elasticsearch'),
    'host' => 'http://localhost',
    'port' => 9200,
    'reindex_migration_driver' => "sync", //sync or queue,
    "reindex_migration_queue_name" => 'default',
    'base_migrations_path' => app_path('Elasticsearch/Migrations'),
    'base_models_path' => app_path('Elasticsearch/Models'),
    "username" => env('ELASTICSEARCH_USERNAME', null),
    'password' => env('ELASTICSEARCH_PASSWORD', null),
    'bridges' => [
    'elastic-model-paths' => [
          'app/Elasticsearch/Models'
        ]
    ]
];

```

ORM
===

[](#orm)

This package has added `ORM` functionality to make it easier to work with documentation, just like what we see in Laravel.We will get to know more as we go forward.Let's dive into it

Models
======

[](#models)

to be able to have a more effective relationship with our documents, we need to have a model for each index. Models similar to what we see in Laravel greatly simplify the work of communicating with the database.

In order to create a Model:

`php artisan elastic:make-model `

By default, your models base path is in `app/Elasticsearch/Models` directory, But you can define your own base path in `config/elasticsearch.php` file.

All your models must inherit from the `BaseElasticsearchModel` class. This class is an abstract class that enforce you to implement the `getIndex` method that returns the index name of model.

```
public function getIndex():string

{
    return 'articles';
}

```

We use the return value of this method to create the index you want in migrations.

If you want to get your index name with the prefix that you defined in config file:

```
$model->getIndexWithPrefix();

```

Migrations
==========

[](#migrations)

As you may know, Elasticsearch uses mappings for the structure of its documents, which may seem a little difficult to create in raw form. In order to simplify this process, we use migrations to make this process easier. After defining the model, you have to create a migration to register your desired fields.All your migrations must inherit from the `BaseElasticMigration` abstract class.

To Create a new Migration:

`php artisan elastic:make-migration `

By default, your migrations base path is in `app/Elasticsearch/Migrations` directory, but you can define your own base path in `config/elasticsearch.php` file.

```
