Add the following lines to your composer.json
"shift31/laravel-elasticsearch": "1.0.*@dev"
"elasticsearch/elasticsearch": "~1.0"
Follow the rest of the install instructions at https://github.com/shift31/laravel-elasticsearch#usage
For good measure, i am providing some starter boilerplate code for you to save your data using that library.
FYI, I use my environment to differentiate between indexes (production or testbed). You may use other methods, such as a config.php value.
Create mapping
$params = array();
$params['index'] = \App::environment();
//params' type and array body's 2nd element should be of the same name.
$params['type'] = 'car';
$params['body']['car'] = ['properties' =>
[
'id' => [
'type' => 'long'
],
'name' => [
'type' => 'string'
],
'engine' => [
'type' => 'string'
]
];
$client = new Elasticsearch\Client();
$client->indices()->putMapping($params);
Insert document
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$car = \CarModel::find($data['id']);
if(count($car))
{
$params['id'] = $car->id;
//Elasticsearch doesn't accept Carbon's default string value. Use the below function to convert it in an acceptable format.
$params['timestamp'] = $car->updated_at->toIso8601String();
// toArray will give you the attributes of the model AND its relations. This is the bothersome part where you will get more data than what you need.
$params['body']['car'] = $car->toArray();
\Es::index($params);
}
Update Document
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$car = \CarModel::find($data['id']);
if(count($car))
{
$params['id'] = $car->id;
$params['body']['doc']['car'] = $car->toArray();
\Es::update($params);
}
Delete Document
$params = array();
$params['index'] = \App::environment();
$params['type'] = 'car';
$params['id'] = 1;
$deleteResult = $client->delete($params);