public function upsertToIndex($subject)
{
if ($subject instanceof Searchable) {
$subject = [$subject];
}
if (is_array($subject) || $subject instanceof Traversable) {
$searchableItems = collect($subject)->each(function ($item) {
if (!$item instanceof Searchable) {
throw new InvalidArgumentException();
}
})->flatMap(function ($item) {
return [['index' => ['_id' => $item->getSearchableId(), '_index' => $this->indexName, '_type' => $item->getSearchableType()]], $item->getSearchableBody()];
})->toArray();
$payload['body'] = $searchableItems;
$this->elasticsearch->bulk($payload);
return;
}
throw new InvalidArgumentException('Subject must be a searchable or array of searchables');
}