All Projects → cviebrock → Laravel Elasticsearch

cviebrock / Laravel Elasticsearch

Licence: mit
An easy way to use the official Elastic Search client in your Laravel applications.

Projects that are alternatives of or similar to Laravel Elasticsearch

Elasticsearch
The missing elasticsearch ORM for Laravel, Lumen and Native php applications
Stars: ✭ 375 (-47.7%)
Mutual labels:  elasticsearch, laravel, lumen
Lumen Generators
A collection of generators for Lumen and Laravel 5.
Stars: ✭ 339 (-52.72%)
Mutual labels:  laravel, lumen
Candy Api
GetCandy E-Commerce API
Stars: ✭ 339 (-52.72%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elastic Demo
笑来搜原型 Laravel Scout & ElasticSearch ik
Stars: ✭ 403 (-43.79%)
Mutual labels:  elasticsearch, laravel
Laravel5 Jsonapi
Laravel 5 JSON API Transformer Package
Stars: ✭ 313 (-56.35%)
Mutual labels:  laravel, lumen
Laravel S
LaravelS is an out-of-the-box adapter between Swoole and Laravel/Lumen.
Stars: ✭ 3,479 (+385.22%)
Mutual labels:  laravel, lumen
Laravel Swoole
High performance HTTP server based on Swoole. Speed up your Laravel or Lumen applications.
Stars: ✭ 3,726 (+419.67%)
Mutual labels:  laravel, lumen
Elasticsearch Eloquent
⚡️ Eloquent models for Elasticsearch.
Stars: ✭ 100 (-86.05%)
Mutual labels:  elasticsearch, laravel
Laravel Scout Elasticsearch
Search among multiple models with ElasticSearch and Laravel Scout
Stars: ✭ 423 (-41%)
Mutual labels:  elasticsearch, laravel
Rest Api With Lumen
Rest API boilerplate for Lumen micro-framework.
Stars: ✭ 464 (-35.29%)
Mutual labels:  laravel, lumen
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.
Stars: ✭ 494 (-31.1%)
Mutual labels:  elasticsearch, laravel
Laravel Swap
💵 Currency exchange rates for Laravel and Lumen
Stars: ✭ 296 (-58.72%)
Mutual labels:  laravel, lumen
Laravel Job Status
Add ability to track Job progress, status and result dispatched to Queue.
Stars: ✭ 279 (-61.09%)
Mutual labels:  laravel, lumen
Jwt Auth Guard
JWT Auth Guard for Laravel and Lumen Frameworks.
Stars: ✭ 319 (-55.51%)
Mutual labels:  laravel, lumen
Laravel Postal Code Validation
Worldwide postal code validation for Laravel and Lumen
Stars: ✭ 278 (-61.23%)
Mutual labels:  laravel, lumen
Lumen Generator
A Lumen Generator You Are Missing
Stars: ✭ 578 (-19.39%)
Mutual labels:  laravel, lumen
Elastic Scout Driver Plus
Extension for Elastic Scout Driver
Stars: ✭ 90 (-87.45%)
Mutual labels:  elasticsearch, laravel
Elasticsearch
Use SQL statements to query elasticsearch
Stars: ✭ 98 (-86.33%)
Mutual labels:  elasticsearch, laravel
Laravel Migrations Generator
Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
Stars: ✭ 417 (-41.84%)
Mutual labels:  laravel, lumen
Laravel Soap
A soap client wrapper for Laravel
Stars: ✭ 559 (-22.04%)
Mutual labels:  laravel, lumen

Laravel-Elasticsearch

An easy way to use the official Elastic Search client in your Laravel or Lumen applications.

Build Status Total Downloads Latest Stable Version Latest Stable Version License

Installation and Configuration

Install the current version of the cviebrock/laravel-elasticsearch package via composer:

composer require cviebrock/laravel-elasticsearch

If you are using ElasticSearch version 5, then install version 2 of this package:

composer require cviebrock/laravel-elasticsearch:^2

Laravel

The package's service provider will automatically register its service provider.

Publish the configuration file:

php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider"
Alternative configuration method via .env file

After you publish the configuration file as suggested above, you may configure ElasticSearch by adding the following to your application's .env file (with appropriate values):

ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=
ELASTICSEARCH_PASS=
Connecting to AWS Elasticsearch Service

If you are connecting to ElasticSearch instances on Amazon AWS, then you'll also need to composer require aws/aws-sdk-php:^3.80 and add the following to your .env file:

AWS_ELASTICSEARCH_ENABLED=true
AWS_REGION=...
AWS_ACCESS_KEY_ID=...
AWS_SECRET_ACCESS_KEY=...

If you have to use another authentication method having custom credentials (i.e. instanceProfile()), you have to publish the configuration file and use the aws_credentials:

<?php
// config/elasticsearch.php

$provider = \Aws\Credentials\CredentialProvider::instanceProfile();
$memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider);
$credentials = $memoizedProvider()->wait();

...

'hosts' => [
    [
        'host'            => env('ELASTICSEARCH_HOST', 'localhost'),
        'port'            => env('ELASTICSEARCH_PORT', 9200),
        'scheme'          => env('ELASTICSEARCH_SCHEME', null),
        'user'            => env('ELASTICSEARCH_USER', null),
        'pass'            => env('ELASTICSEARCH_PASS', null),

        // If you are connecting to an Elasticsearch instance on AWS, you will need these values as well
        'aws'             => env('AWS_ELASTICSEARCH_ENABLED', false),
        'aws_region'      => env('AWS_REGION', ''),
        'aws_key'         => env('AWS_ACCESS_KEY_ID', ''),
        'aws_secret'      => env('AWS_SECRET_ACCESS_KEY', '')
        'aws_credentials' => $credentials
    ],
],

If you have a job that runs in supervisor, you have to use the Closure. This way the credentials will be renewed at runtime.

<?php
// config/elasticsearch.php

$provider = \Aws\Credentials\CredentialProvider::instanceProfile();
$memoizedProvider = \Aws\Credentials\CredentialProvider::memoize($provider);

...

'hosts' => [
    [
        ...
        'aws_credentials' => $memoizedProvider
    ],
],

If you are using php artisan config:cache, you cannot have the Closure in your config file, call it like this:

<?php
// config/elasticsearch.php

...

'hosts' => [
    [
        ...
        'aws_credentials' => [\Aws\Credentials\CredentialProvider::class, 'defaultProvider'],
    ],
],

Lumen

If you work with Lumen, please register the service provider and configuration in bootstrap/app.php:

$app->register(Cviebrock\LaravelElasticsearch\ServiceProvider::class);
$app->configure('elasticsearch');

Manually copy the configuration file to your application.

Usage

The Elasticsearch facade is just an entry point into the ES client, so previously you might have used:

use Elasticsearch\ClientBuilder;

$data = [
    'body' => [
        'testField' => 'abc'
    ],
    'index' => 'my_index',
    'type' => 'my_type',
    'id' => 'my_id',
];

$client = ClientBuilder::create()->build();
$return = $client->index($data);

You can now replace those last two lines with simply:

use Elasticsearch;

$return = Elasticsearch::index($data);

That will run the command on the default connection. You can run a command on any connection (see the defaultConnection setting and connections array in the configuration file).

$return = Elasticsearch::connection('connectionName')->index($data);

Lumen users who wish to use Facades can do so by editing the bootstrap/app.php file to include the following:

$app->withFacades(true, [
    ...
    Cviebrock\LaravelElasticsearch\Facade::class => 'Elasticsearch',
    ...
]);

Lumen users who aren't using facades will need to use dependency injection or the application container in order to get the ES service object:

// using injection:
public function handle(\Cviebrock\LaravelElasticsearch\Manager $elasticsearch)
{
    $elasticsearch->ping();
}

// using application container:
$elasticSearch = $this->app('elasticsearch');

Of course, dependency injection and the application container work for Laravel applications as well.

Advanced Usage

Because the package is a wrapper around the official Elastic client, you can do pretty much anything with this package. Not only can you perform standard CRUD operations, but you can monitor the health of your Elastic cluster programmatically, back it up, or make changes to it. Some of these operations are done through "namespaced" commands, which this package happily supports.

To grab statistics for an index:

$stats = Elasticsearch::indices()->stats(['index' => 'my_index']);
$stats = Elasticsearch::nodes()->stats();
$stats = Elasticsearch::cluster()->stats();

To create and restore snapshots (read the Elastic docs about creating repository paths and plugins first):

$response = Elasticsearch::snapshots()->create($params);
$response = Elasticsearch::snapshots()->restore($params);

To delete whole indices (be careful!):

$response = Elasticsearch::indices()->delete(['index' => 'my_index']);

Please remember that this package is a thin wrapper around a large number of very sophisticated and well-documented Elastic features. Information about those features and the methods and parameters used to call them can be found in the Elastic documentation. Help with using them is available via the Elastic forums and on sites like Stack Overflow.

Console commands

This package also provides some useful console commands.

Check if an index exists:

php artisan laravel-elasticsearch:utils:index-exists <your_elasticsearch_index_name>

Create an index:

php artisan laravel-elasticsearch:utils:index-create <your_elasticsearch_index_name>

Delete an index:

php artisan laravel-elasticsearch:utils:index-delete <your_elasticsearch_index_name>

Create or update index mapping:
Note: The index mapping file must contain a valid JSON mapping definition as Elasticsearch expects, for example:

{
    "body": {
        "_source": {
            "enabled": true
        },
        "properties": {
            "id": {
                "type": "keyword"
            },
            "property_1": {
                "type": "text"
            },
            "property_2": {
                "type": "text"
            }
        }
    }
}
php artisan laravel-elasticsearch:utils:index-create-or-update-mapping <your_elasticsearch_index_name> <json_mapping_absolute_file_path>

Creates an alias:

php artisan laravel-elasticsearch:utils:alias-create <your_elasticsearch_index_name> <your_elasticsearch_alias_name>

Remove index from an alias:

php artisan laravel-elasticsearch:utils:alias-remove-index <your_elasticsearch_index_name> <your_elasticsearch_alias_name>

Switch index on alias (useful for zero-downtime release of the new index):

php artisan laravel-elasticsearch:utils:alias-switch-index <your_NEW_elasticsearch_index_name> <your_OLD_elasticsearch_index_name> <your_elasticsearch_alias_name>

Bugs, Suggestions, Contributions and Support

Thanks to everyone who has contributed to this project!

Special thanks to JetBrains for their Open Source License Program ... and the excellent PHPStorm IDE, of course!

JetBrains

Please use Github for reporting bugs, and making comments or suggestions.

See CONTRIBUTING.md for how to contribute changes.

Copyright and License

laravel-elasticsearch was written by Colin Viebrock and is released under the MIT License.

Copyright (c) 2015 Colin Viebrock

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].