Skip to content

Instantly share code, notes, and snippets.

@factoryhr
Created September 6, 2018 09:09
Show Gist options
  • Save factoryhr/6e253f25df4beb5d8c498b40549a2bf3 to your computer and use it in GitHub Desktop.
Save factoryhr/6e253f25df4beb5d8c498b40549a2bf3 to your computer and use it in GitHub Desktop.
$movies = Movie::all();
foreach ($movies as $movie) {
// Add index and type data to array
// Movie::ELASTIC_INDEX => movies
// Movie::ELASTIC_TYPE => movie
$data['body'][] = [
'index' => [
'_index' => Movie::ELASTIC_INDEX,
'_type' => Movie::ELASTIC_TYPE
]
];
// Movie data that will be required for later search
$data['body'][] = [
'id' => $movie->id,
'name' => $movie->name,
'year' => $movie->year,
'description' => $movie->description,
'rating' => $movie->rating,
'actors' => implode(',', $movie->movie_actors->pluck('name')->toArray())
];
}
// Execute Elasticsearch bulk command for indexing multiple data
$response = $this->client->bulk($data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment