2017 © Pedro Peláez
 

library laravel-elasticsearch

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

image

designmynight/laravel-elasticsearch

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

  • Friday, July 27, 2018
  • by StevePorter92
  • Repository
  • 4 Watchers
  • 11 Stars
  • 250 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 52 Versions
  • 52 % Grown

The README.md

laravel-elasticsearch

Use Elasticsearch as a database in Laravel to retrieve Eloquent models and perform aggregations., (*1)

Build Elasticsearch queries as you're used to with Eloquent, and get Model instances in return, with some nice extras: - Use query, filter and postFilter query types - Perform geo searches - Build and perform complex aggregations on your data - Use the Elasticsearch scroll API to retrieve large numbers of results, (*2)

Versions

Depending on your version of Elasticsearch you can use the following version of this package, (*3)

Elasticsearch Laravel Elasticsearch
>= 7.0, < 7.1 6
>= 6.6, < 7.0 5
>= 5.0, < 6.0 4

Setup

Add elasticsearch connection configuration to database.php, (*4)

'elasticsearch' => [
    'driver'   => 'elasticsearch',
    'host'     => 'localhost',
    'port'     => 9200,
    'database' => 'your_es_index',
    'username' => 'optional_es_username',
    'password' => 'optional_es_username',
    'suffix'   => 'optional_es_index_suffix',
]

Create or update your base Model.php class to override newEloquentBuilder() and newBaseQueryBuilder():, (*5)

/**
 * Create a new Eloquent builder for the model.
 *
 * @param  \Illuminate\Database\Query\Builder  $query
 * @return \Illuminate\Database\Eloquent\Builder|static
 */
public function newEloquentBuilder($query)
{
    switch ($this->getConnectionName()) {
        case static::getElasticsearchConnectionName():
            $builder = new ElasticsearchEloquentBuilder($query);
            break;

        default:
            $builder = new Illuminate\Database\Eloquent\Builder($query);
    }

    return $builder;
}

/**
 * Get a new query builder instance for the connection.
 *
 * @return \Illuminate\Database\Query\Builder
 */
protected function newBaseQueryBuilder()
{
    $connection = $this->getConnection();

    switch ($this->getConnectionName()) {
        case static::getElasticsearchConnectionName():
            $builder = new ElasticsearchQueryBuilder($connection, $connection->getQueryGrammar(), $connection->getPostProcessor());
            break;

        default:
            $builder = new Illuminate\Database\Query\Builder($connection, $connection->getPostProcessor());
    }

    return $builder;
}

You're now ready to carry out searches on your data. The query will look for an Elasticsearch index with the same name as the database table that your models reside in., (*6)

$documents = MyModel::newElasticsearchQuery()
              ->where('date', '>', Carbon\Carbon::now())
              ->get();

Aggregations

Aggregations can be added to a query with an approach that's similar to querying Elasticsearch directly, using nested functions rather than nested arrays. The aggregation() method takes three or four arguments: 1. A key to be used for the aggregation 2. The type of aggregation, such as 'filter' or 'terms' 3. (Optional) A callback or array providing options for the aggregation 4. (Optional) A function allowing you to provide further sub-aggregations, (*7)

$myQuery = MyModel::newElasticsearchQuery()
             ->aggregation(
                 // The key of the aggregation (used in the Elasticsearch response)
                 'my_filter_aggregation',

                 // The type of the aggregation
                 'filter',

                 // A callback providing options to the aggregation, in this case adding filter criteria to a query builder
                 function ($query) {
                     $query->where('lost', '!=', true);
                     $query->where('concierge', true);
                 },

                 // A callback specifying a sub-aggregation
                 function ($builder) {
                     // A simpler aggregation, counting terms in the 'status' field
                     $builder->aggregation('my_terms_aggregation', 'terms', ['field' => 'status']);
                 }
             );

$results = $myQuery->get();
$aggregations = $myQuery->getQuery()->getAggregationResults();

Geo queries

You can filter search results by distance from a geo point or include only those results that fall within given bounds, passing arguments in the format you'd use if querying Elasticsearch directly., (*8)

$withinDistance = MyModel::newElasticsearchQuery()
                    ->whereGeoDistance('geo_field', [$lat, $lon], $distance);

$withinBounds= MyModel::newElasticsearchQuery()
                 ->whereGeoBoundsIn('geo_field', $boundingBox);

Scroll API

You can use a scroll search to retrieve large numbers of results. Rather than returning a Collection, you'll get a PHP Generator function that you can iterate over, where each value is a Model for a single result from Elasticsearch., (*9)

$documents = MyModel::newElasticsearchQuery()
               ->limit(100000)
               ->usingScroll()
               ->get();

// $documents is a Generator
foreach ($documents as $document){
  echo $document->id;
}

Console

This package ships with the following commands to be used as utilities or as part of your deployment process., (*10)

Command Arguments Options Description
make:mapping name: Name of the mapping. This name also determines the name of the index and the alias. --update: Whether the mapping should update an existing index. --template: Pass a pre-existing mapping filename to create your new mapping from. Creates a new mapping migration file.
migrate:mappings index-command: (Optional) Name of your local Artisan console command that performs the Elasticsearch indexing. If not given, command will be retrieved from laravel-elasticsearch config file. --index : Automatically index new mapping.--swap: Automatically update the alias after the indexing has finished. Migrates your mapping files and begins to create the index.
index:rollback Rollback to the previous index migration.
index:remove index: (Optional) Name of the index to remove from your Elasticsearch cluster. Removes an index from your Elasticsearch cluster.
index:swap alias: Name of alias to update. index: Name of index to update alias to. old-index: (Optional) Name of old index. --remove-old-index: Remove old index from your Elasticsearch cluster. Swap the index your alias points to.
index:list --alias: List active aliases. Pass "*" to view all. Other values filter the returned aliases. Display a list of all indexes in your Elasticsearch cluster.
index:copy from: index to copy from. to: the index to copy from Populate an index with all documents from another index

Mappings and Aliases

When creating a new index during a migrate:mappings the command will automatically create an alias based on the migration name by removing the date string. For example the migration 2018_08_03_095804_users.json will create the alias users., (*11)

During the first migration an index appears in the migrate:mappings command will also switch the alias to the latest index mapping. The above will only happen when the alias does not already exist., (*12)

Future migrations will require you to use the --swap option., (*13)

The Versions

27/07 2018

dev-master

9999999-dev https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

27/07 2018

2.0.4

2.0.4.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

27/07 2018

dev-feat-update-mapping-command

dev-feat-update-mapping-command https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

18/07 2018

2.0.3

2.0.3.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

05/07 2018

2.0.2

2.0.2.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

05/07 2018

2.0.1

2.0.1.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

05/07 2018

2.0.0

2.0.0.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

21/06 2018

1.1.1

1.1.1.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

25/05 2018

1.1.0

1.1.0.0 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

25/05 2018

dev-laravel-56

dev-laravel-56 https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

24/05 2018

dev-switch-mapping-console-command

dev-switch-mapping-console-command https://github.com/designmynight/laravel-elasticsearch

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

  Sources   Download

MIT

The Requires

 

The Development Requires

database laravel eloquent elasticsearch model

21/03 2018

dev-fix-query-timings

dev-fix-query-timings https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

05/03 2018

v1.0.17

1.0.17.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

05/03 2018

dev-date-range-aggregation

dev-date-range-aggregation https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

01/03 2018

dev-query-builder-additions

dev-query-builder-additions https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

27/02 2018

v1.0.16

1.0.16.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

27/02 2018

dev-function_score

dev-function_score https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

26/02 2018

v1.0.15

1.0.15.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

26/02 2018

dev-fix-boost

dev-fix-boost https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

22/02 2018

v1.0.14

1.0.14.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

22/02 2018

v1.0.13

1.0.13.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

21/02 2018

v1.0.11

1.0.11.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

21/02 2018

v1.0.12

1.0.12.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

21/02 2018

dev-fuzziness

dev-fuzziness https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

13/02 2018

v1.0.10

1.0.10.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

05/02 2018

dev-add-timeout-option

dev-add-timeout-option https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

02/02 2018

v1.0.9

1.0.9.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

02/02 2018

v1.0.8

1.0.8.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

02/02 2018

dev-fix-return-type

dev-fix-return-type https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

01/02 2018

v1.0.7

1.0.7.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

01/02 2018

dev-fix-scroll-limit

dev-fix-scroll-limit https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

30/01 2018

v1.0.6

1.0.6.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

29/01 2018

v1.0.5

1.0.5.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

26/01 2018

dev-21-min-doc-count

dev-21-min-doc-count https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

22/01 2018

v1.0.4

1.0.4.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

16/01 2018

v1.0.3

1.0.3.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

16/01 2018

dev-add-collection

dev-add-collection https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

13/01 2018

v1.0.2

1.0.2.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

12/01 2018

dev-docs-language

dev-docs-language https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

12/01 2018

v1.0.1

1.0.1.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

11/01 2018

v0.2.1-beta

0.2.1.0-beta https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

11/01 2018

v1.0.0

1.0.0.0 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

11/01 2018

dev-refactor-exists

dev-refactor-exists https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

10/01 2018

v0.2.0-beta

0.2.0.0-beta https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

10/01 2018

dev-searchable-trait

dev-searchable-trait https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-alpha.3

0.1.0.0-alpha3 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-beta

0.1.0.0-beta https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-alpha.2

0.1.0.0-alpha2 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-alpha.1

0.1.0.0-alpha1 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

dev-fix-geo-distance-order

dev-fix-geo-distance-order https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-alpha1

0.1.0.0-alpha1 https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model

03/01 2018

v0.1.0-alpha

0.1.0.0-alpha https://github.com/jenssegers/laravel-mongodb

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

  Sources   Download

MIT

The Requires

 

database laravel eloquent elasticsearch model