rockbuzz/lara-elasticsearch

Laravel ElasticSearch

dev-main 2022-06-19 19:09 UTC

This package is auto-updated.

Last update: 2024-04-19 23:16:54 UTC


README

Laravel Elastic Search integration

badge.svg

Requirements

PHP >=7.3

Install

$ composer require rockbuzz/lara-elasticsearch

Usage

ELASTICSEARCH_ENABLED=true
ELASTICSEARCH_HOSTS="__elasticsearch__:9200"
ELASTICSEARCH_LOGS_INDEX="app.local.logs"
//config/services.php
<?php
return [

    /*
    |--------------------------------------------------------------------------
    | Third Party Services
    |--------------------------------------------------------------------------
    |
    | This file is for storing the credentials for third party services such
    | as Mailgun, Postmark, AWS and more. This file provides the de facto
    | location for this type of information, allowing packages to have
    | a conventional file to locate the various service credentials.
    |
    */

    'search' => [
        'enabled' => env('ELASTICSEARCH_ENABLED', false),
        'hosts' => explode(',', env('ELASTICSEARCH_HOSTS', '127.0.0.1:9200')),
    ],
    //....
];
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Rockbuzz\LaraElasticSearch\Contracts\Searchable;
use Rockbuzz\LaraElasticSearch\Traits\Searchable as TraitsSearchable;

class Article extends Model implements Searchable
{
    use TraitsSearchable;

    //Optionally you can override the getSearchBody method, by default $this->toArray() is used
    public function getSearchBody()
    {
        return [
            //
        ];
    }
}

To index the data referring to a model, run:

$ php artisan search:index Article

Logging

//config/logging.php
<?php
return [

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */
    'channels' => [
        'elasticsearch' => [
            'driver'         => 'monolog',
            'level'          => 'debug',
            'handler'        => \Monolog\Handler\ElasticsearchHandler::class,
            'formatter'      => \Monolog\Formatter\ElasticsearchFormatter::class,
            'formatter_with' => [
                'index' => env('ELASTICSEARCH_LOGS_INDEX'),
                'type'  => '_doc',
            ],
            'handler_with'   => [
                'client' => \Elasticsearch\ClientBuilder::create()
                    ->setHosts(explode(',', env('ELASTICSEARCH_HOSTS', '127.0.0.1:9200')))
                    ->build(),
            ],
        ],
        //...
    ],
    //...
];

License

The Lara Elastic Search is open-sourced software licensed under the MIT license.