PHPackages                             aviationcode/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. [Search &amp; Filtering](/categories/search)
4. /
5. aviationcode/elasticsearch

ActiveLibrary[Search &amp; Filtering](/categories/search)

aviationcode/elasticsearch
==========================

Laravel elasticsearch and eloquent integration

v0.15.0(2y ago)526.2k3[6 issues](https://github.com/AviationCode/elasticsearch/issues)MITPHP

Since Jan 16Pushed 2y ago3 watchersCompare

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

READMEChangelog (10)Dependencies (9)Versions (21)Used By (0)

Elasticsearch
=============

[](#elasticsearch)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5ade3ae3e3332d2a8145005a29cd574108f01980d77f1571cff1e3892dc7b546/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6176696174696f6e636f64652f656c61737469637365617263682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aviationcode/elasticsearch)[![Total Downloads](https://camo.githubusercontent.com/3b120100eb4602307dbcb1e9b100fbd6dec4d684c2eab1b8b1ac40c64284f05a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6176696174696f6e636f64652f656c61737469637365617263682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aviationcode/elasticsearch)[![Build Status](https://camo.githubusercontent.com/22f35a01c4f315fcf037a9d0a11568fdb18ccb7eb82bc3894d7c211d35ca6fc8/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6176696174696f6e636f64652f656c61737469637365617263682f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/aviationcode/elasticsearch)

This package wraps the `elasticsearch/elasticsearch` composer package with laravel integration. Adding support to easily use your eloquent models with elastic search.

Installation
------------

[](#installation)

Via Composer

```
$ composer require aviationcode/elasticsearch
```

Configuration
-------------

[](#configuration)

By default, we use `localhost:9200` to search your elasticsearch instance. If this is the case no configuration is required at all. You can use the following `.env` settings to configure how we connect to your elasticsearch instance.

NameTypeDefaultDescriptionELASTIC\_HOST`string``localhost`The IP or host to connect toELASTIC\_PORT`integer`9200The port used to connectELASTIC\_SCHEME`string``http`Use of HTTP or HTTPSELASTIC\_USER`string``null`The Basic auth usernameELASTIC\_PASSWORD`string``null`The Basic auth passwordUsage
-----

[](#usage)

Configure a model to use elasticsearch by using the `ElasticSearchable` trait or extend using `ElasticsearchModel`.

```
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ElasticSearchable;
}
```

### Custom mapping properties

[](#custom-mapping-properties)

We attempt to detect the [elasticsearch mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) fields from your `$dates` array and primary key. We are unable to correctly detect other fields.

Elasticsearch will in this case attempt to guess the mapping field automatically however it is recommended to explicitly define these fields as the correct type.

```
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ElasticSearchable;

    public $mapping = [
        'category' => ['type' => 'keyword'],
        'properties' => ['type' => 'object', 'dynamic' => true],
        'ip' => ['type' => 'ip'],
    ];
}
```

Use the elasticsearch documentation to find all options available.

### Non numeric keys

[](#non-numeric-keys)

When using UUID's or other non numeric keys make sure you configure your model correctly. This will make sure we use the correct mapping inside your model mapping.

```
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ElasticSearchable;

    protected $keyType = 'string';
}
```

### Custom index name

[](#custom-index-name)

You may want to use a custom name or use existing index name with your eloquent model. Just like you can define the database table used you can also define the index named used.

```
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ElasticSearchable;

    public $indexName = 'my_custom_index_name';
}
```

**Note**: You can still use `$indexVersion` to add `vX` at the end of your index.

### Versioned index

[](#versioned-index)

If you like to version your index names you can use the `$indexVersion` name. This will add `_vX` at the end of your index name where X is the index version.

```
use AviationCode\Elasticsearch\Model\ElasticSearchable;
use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    use ElasticSearchable;

    public $indexVersion = 2;
}
```

### Query

[](#query)

```
namespace App\Http\Controllers;

use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Filter;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Must;
use Illuminate\Http\Request;

class ArticleController
{
    public function index(Request $request)
    {
        return Elasticsearch::forModel(Article::class)
            ->query()
            ->filter(function (Filter $filter) use ($request) {
                if ($user = $request->query('user')) {
                    $filter->term('user', $user);
                }
            })
            ->must(function (Must $must) use ($request) {
                if ($query = $request->query('q')) {
                    $must->queryString($query);
                }
            })
            ->get();
    }
}
```

Without an eloquent model.

```
namespace App\Http\Controllers;

use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Filter;
use AviationCode\Elasticsearch\Query\Dsl\Boolean\Must;
use Illuminate\Http\Request;

class ArticleController
{
    public function index(Request $request)
    {
        return Elasticsearch::query('article')
            ->filter(function (Filter $filter) use ($request) {
                if ($user = $request->query('user')) {
                    $filter->term('user', $user);
                }
            })
            ->must(function (Must $must) use ($request) {
                if ($query = $request->query('q')) {
                    $must->queryString($query);
                }
            })
            ->get();
    }
}
```

### Aggregations

[](#aggregations)

Using aggregations with model.

```
namespace App\Http\Controllers;

use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;

class ArticlesPerUserPerDayController
{
    public function index()
    {
        $qb = Elasticsearch::forModel(Article::class)->query();

        $qb->aggregations()
            ->dateHistogram('date', 'created_at', '1d')
            ->terms('date.users', 'user');

        return $qb->get()->aggregations;
    }
}
```

Using aggregations without an eloquent model.

```
namespace App\Http\Controllers;

use App\Article;
use AviationCode\Elasticsearch\Facades\Elasticsearch;

class ArticlesPerUserPerDayController
{
    public function index()
    {
        $qb = Elasticsearch::query('article');

        $qb->aggregations()
            ->dateHistogram('date', 'created_at', '1d')
            ->terms('date.users', 'user');

        return $qb->get()->aggregations;
    }
}
```

Console Commands
----------------

[](#console-commands)

### `elastic:create-index` Creating elasticsearch index

[](#elasticcreate-index-creating-elasticsearch-index)

Change log
----------

[](#change-log)

Please see the [changelog](changelog.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [contributing.md](contributing.md) for details and a todolist.

Security
--------

[](#security)

If you discover a security vulnerability within elasticsearch package, please send an e-mail to Ken Andries at . All security vulnerabilities will be promptly addressed.

Credits
-------

[](#credits)

- [Ken Andries](https://github.com/douglasdc3)
- [All Contributors](../../contributors)

License
-------

[](#license)

license. Please see the [license file](license.md) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity31

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 84.6% 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 ~88 days

Recently: every ~302 days

Total

19

Last Release

732d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/913c49cf2a71127998eb986a68d8dd81f3a9a96e5a4f9b063066a2b5c2a99a28?d=identicon)[Douglasdc3](/maintainers/Douglasdc3)

---

Top Contributors

[![Douglasdc3](https://avatars.githubusercontent.com/u/5066883?v=4)](https://github.com/Douglasdc3 "Douglasdc3 (226 commits)")[![hamzahejja](https://avatars.githubusercontent.com/u/49674728?v=4)](https://github.com/hamzahejja "hamzahejja (22 commits)")[![arnordhaenens](https://avatars.githubusercontent.com/u/31385240?v=4)](https://github.com/arnordhaenens "arnordhaenens (10 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (5 commits)")[![astalpaert](https://avatars.githubusercontent.com/u/5843790?v=4)](https://github.com/astalpaert "astalpaert (4 commits)")

---

Tags

elasticsearchhacktoberfestlaravelpackagelaravelelasticsearch

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[mailerlite/laravel-elasticsearch

An easy way to use the official PHP ElasticSearch client in your Laravel applications.

934529.3k2](/packages/mailerlite-laravel-elasticsearch)[jeroen-g/explorer

Next-gen Elasticsearch driver for Laravel Scout.

397612.3k](/packages/jeroen-g-explorer)[baijunyao/laravel-scout-elasticsearch

Elasticsearch Driver for Laravel Scout

8023.7k1](/packages/baijunyao-laravel-scout-elasticsearch)

PHPackages © 2026

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