Answer the question
In order to leave comments, you need to log in
Elasticsearch, laravel, artisan how to set up?
There is an AppServiceProvider:
public function register()
{
if ($this->app->environment() !== 'production') {
$this->app->register(\Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class);
}
$this->app->bind(Elastic::class, function ($app) {
//
return new Elastic(
ClientBuilder::create()
->setHosts(['http://elasticsearch:9200'])
->setLogger(ClientBuilder::defaultLogger(storage_path('logs/elastic.log')))
->build()
);
});
// ...
}
<?php
namespace App\Elastic;
use Elasticsearch\Client;
class Elastic
{
protected $client;
public function __construct(Client $client)
{
$this->client = $client;
}
/**
* Index a single item
*
* @param array $parameters [index, type, id, body]
*/
public function index(array $parameters)
{
return $this->client->index($parameters);
}
/**
* Delete a single item
*
* @param array $parameters
*/
public function delete(array $parameters)
{
return $this->client->delete($parameters);
}
/**
* Index multiple items
*
* This method normalises the 'bulk' method of the Elastic Search
* Client to have a signature more similar to 'index'
*
* @param array $collection [[index, type, id, body], [index, type, id, body]...]
*/
public function indexMany(array $collection)
{
$parameters = [];
foreach ($collection as $item) {
$parameters['body'][] = [
"index" => [
'_id' => $item['id'],
'_index' => $item['index'],
'_type' => $item['type'],
]
];
$parameters['body'][] = $item['body'];
}
return $this->client->bulk($parameters);
}
/**
* Delete Index
*
* This suppresses any exceptions thrown by trying
* to delete a non-existent index by first
* checking if it exists, then deleting.
*
* @param string $name
* @return bool
*/
public function deleteIndex($name)
{
if (! $this->indexExists($name)) {
return true;
}
return $this->client->indices()->delete([
'index' => $name
]);
}
public function indexExists($name)
{
return $this->client->indices()->exists(['index' => $name]);
}
public function search(array $parameters)
{
return $this->client->search($parameters);
}
public function getClient()
{
return $this->client;
}
}
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Services;
use App\Elastic\Elastic;
class ImportElastic extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:elastic';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$elastic = \App::make(Elastic::class);
Services::chunk(1000, function ($services) use ($elastic) {
foreach ($services as $service) {
$elastic->index([
'index' => 'services',
'type' => 'service',
'id' => $service->id,
'body' => $service->toArray()
]);
}
});
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question