PHP code example of alexsaab / laravel-elasticsearch

1. Go to this page and download the library: Download alexsaab/laravel-elasticsearch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alexsaab / laravel-elasticsearch example snippets


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

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

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

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

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

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

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

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

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

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

$response = Elasticsearch::indices()->delete(['index' => 'my_index']);
sh
php artisan vendor:publish --provider="Alexsaab\LaravelElasticsearch\ServiceProvider"
ini
ELASTICSEARCH_HOST=localhost
ELASTICSEARCH_PORT=9200
ELASTICSEARCH_SCHEME=http
ELASTICSEARCH_USER=
ELASTICSEARCH_PASS=