Spatie\SearchIndex\SearchIndexHandlers\Elasticsearch::upsertToIndex PHP Method

upsertToIndex() public method

Add or update the given searchable subject or array of subjects or Traversable object containing subjects.
public upsertToIndex ( Spatie\SearchIndex\Searchable | array | Traversabl\Traversable $subject )
$subject Spatie\SearchIndex\Searchable | array | Traversabl\Traversable
    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');
    }