Skip to content

Instantly share code, notes, and snippets.

@jerronimo
Created August 26, 2017 11:28
Show Gist options
  • Save jerronimo/b866c7858ab675821ea4574df546b3ac to your computer and use it in GitHub Desktop.
Save jerronimo/b866c7858ab675821ea4574df546b3ac to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use App\Models\Apartment;
use Illuminate\Console\Command;
use Elasticsearch\ClientBuilder;
class ElasticReindexCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:elastic:reindex';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create elastic search mapping';
/**
* @var string
*/
private $index = 'hello_pad';
/**
* @var string
*/
private $type = 'apartment';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$host = [
env('ELASTIC_HOST')
];
$clientBuilder = ClientBuilder::create();
$clientBuilder->setHosts($host);
$client = $clientBuilder->build();
$this->mapping($client);
$this->reindex($client);
}
/**
* @param $client
*/
private function mapping($client)
{
$params = [
'index' => $this->index
];
if ($client->indices()->exists($params)) {
$client->indices()->delete($params);
}
$params = [
'index' => $this->index,
'body' => [
'mappings' => [
'apartment' => [
'properties' => [
'id' => [
'type' => 'integer',
'include_in_all' => FALSE
],
'location'=> [
'type' => 'geo_point',
],
'street_name' => [
'type' => 'string',
'analyzer' => 'standard'
],
'city' => [
'type' => 'string',
'analyzer' => 'standard'
],
'street_number' => [
'type' => 'string',
'analyzer' => 'standard'
],
'unit' => [
'type' => 'integer',
],
'hide_address' => [
'type' => 'boolean',
],
'zip_code' => [
'type' => 'string',
'analyzer' => 'standard'
],
'rent' => [
'type' => 'scaled_float',
'scaling_factor' => '100'
],
'square_feet' => [
'type' => 'integer',
],
'bedrooms' => [
'type' => 'integer',
],
'bathrooms' => [
'type' => 'integer',
],
'partial_bathrooms' => [
'type' => 'integer',
],
'cats' => [
'type' => 'integer',
],
'dogs' => [
'type' => 'integer',
],
'leasing_fees' => [
'type' => 'boolean',
],
'income_restricted' => [
'type' => 'boolean',
],
'section_eight' => [
'type' => 'boolean',
],
'lease_terms' => [
'type' => 'string',
],
'description' => [
'type' => 'string',
],
'notes' => [
'type' => 'string',
],
'type' => [
'type' => 'integer',
],
'user' => [
'type' => 'string',
'analyzer' => 'standard'
],
'created_at' => [
'type' => 'date'
],
'duration' => [
'type' => 'boolean'
],
'updated_at' => [
'type' => 'date'
],
'building_amenities' => [
'type' => 'nested',
'properties' => [
'id' => [
'type' => 'integer',
'store' => 'yes'
],
'name' => [
'type' => 'string'
],
'type' => [
'type' => 'integer'
]
]
],
'amenities' => [
'type' => 'nested',
'properties' => [
'id' => [
'type' => 'integer',
'store' => 'yes'
],
'name' => [
'type' => 'string'
],
'type' => [
'type' => 'integer'
]
]
],
'media' => [
'type' => 'nested',
]
]
]
]
]
];
$client->indices()->create($params);
}
/**
* Reindex all Apartment
* @param $client
*/
private function reindex($client)
{
$apartments = Apartment::with(['medias', 'options'])->get();
foreach ($apartments as $apartment) {
$params = [
'index' => $this->index,
'type' => $this->type,
'id' => $apartment->id,
'body' => $this->formedData($apartment)
];
$client->index($params);
}
}
/**
* @param Apartment $apartment
* @return array
*/
private function formedData(Apartment $apartment)
{
$user = $apartment->user()->first();
return [
'location'=> [
'lat' => $apartment->geo_lat,
'lon' => $apartment->geo_lot,
],
'street_name' => $apartment->street_name,
'city' => $apartment->city,
'street_number' => $apartment->street_number,
'unit' => $apartment->unit,
'hide_address' => $apartment->hide_address,
'zip_code' => $apartment->zip_code,
'rent' => $apartment->rent,
'square_feet' => $apartment->square_feet,
'bedrooms' => $apartment->bedrooms,
'bathrooms' => $apartment->bathrooms,
'partial_bathrooms' => $apartment->partial_bathrooms,
'cats' => $apartment->cats ? $apartment->cats : 0,
'dogs' => $apartment->dogs ?$apartment->dogs: 0,
'leasing_fees' => $apartment->leasing_fees,
'income_restricted' => $apartment->income_restricted,
'section_eight' => $apartment->section_eight,
'lease_terms' => $apartment->lease_terms,
'description' => $apartment->description,
'notes' => $apartment->notes,
'duration' => $apartment->long_term_period,
'type' => $apartment->property->id,
'user' => $user->last_name .' ' . $user->first_name,
'created_at' => $apartment->created_at->format('Y-m-d'),
'updated_at' => $apartment->updated_at->format('Y-m-d'),
'media' => $apartment->medias()->get(),
'amenities' => $apartment->amenities()->get()->toArray(),
'building_amenities' => $apartment->buildingAmenities()->get()->toArray()
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment