diff --git a/src/Collection.php b/src/Collection.php index 2644880..fd5712a 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -13,15 +13,17 @@ public function addToIndex() } $instance = $this->first(); + $instance->setConnection($instance->getElasticsearchConnectionName()); + $query = $this->first()->newQueryWithoutScopes(); - $docs = $this->map(function ($model) { + $docs = $this->map(function ($model, $i) { return $model->toSearchableArray(); }); - return $instance->onSearchConnection(function ($instance) use ($docs) { - $query = $instance->newQueryWithoutScopes(); + $success = $query->insert($docs->all()); - return $query->insert($docs->all()); - }, $instance); + unset($docs); + + return $success; } } diff --git a/src/Searchable.php b/src/Searchable.php index e3c694c..889c813 100644 --- a/src/Searchable.php +++ b/src/Searchable.php @@ -109,17 +109,28 @@ public function removeFromIndex() */ public function toSearchableArray() { + // Run this on the search connection if it's not the current connection + if ($this->getConnectionName() !== static::getElasticsearchConnectionName()) { + return $this->onSearchConnection(function ($model) { + return $model->toSearchableArray(); + }, $this); + } + $array = $this->toArray(); foreach ($this->getArrayableRelations() as $key => $relation) { $attributeName = snake_case($key); if (isset($array[$attributeName]) && method_exists($relation, 'toSearchableArray')) { - $array[$attributeName] = $relation->toSearchableArray($array[$attributeName]); + $array[$attributeName] = $relation->onSearchConnection(function ($model) { + return $model->toSearchableArray(); + }, $relation); } elseif (isset($array[$attributeName]) && $relation instanceof \Illuminate\Support\Collection) { - $array[$attributeName] = $relation->map(function ($item, $i) use ($array, $attributeName) { + $array[$attributeName] = $relation->map(function ($item, $i) { if (method_exists($item, 'toSearchableArray')) { - return $item->toSearchableArray($array[$attributeName][$i]); + return $item->onSearchConnection(function ($model) { + return $model->toSearchableArray(); + }, $item); } return $item; @@ -135,69 +146,15 @@ public function toSearchableArray() $subDocuments = $this->$field ?? []; foreach ($subDocuments as $subDocument) { - $array['child_documents'][] = $this->getSubDocumentIndexData($subDocument, $field); + $array['child_documents'][] = $this->onSearchConnection(function ($model) { + return $model->getSubDocumentIndexData($model); + }, $subDocument); } } - $array = $this->datesToSearchable($array); - return $array; } - /** - * Convert all dates to searchable format - * - * @param array $array - * @return array - */ - public function datesToSearchable(array $array): array - { - foreach ($this->getDates() as $dateField) { - if (isset($array[$dateField])) { - $array[$dateField] = $this->fromDateTimeSearchable($array[$dateField]); - } - } - - foreach ($this->getArrayableRelations() as $key => $relation) { - $attributeName = snake_case($key); - - if (isset($array[$attributeName]) && method_exists($relation, 'toSearchableArray')) { - $array[$attributeName] = $relation->datesToSearchable($array[$attributeName]); - } elseif (isset($array[$attributeName]) && $relation instanceof \Illuminate\Support\Collection) { - $array[$attributeName] = $relation->map(function ($item, $i) use ($array, $attributeName) { - if (method_exists($item, 'toSearchableArray')) { - return $item->datesToSearchable($array[$attributeName][$i]); - } - - return $item; - })->all(); - } - } - - return $array; - } - - /** - * Convert a DateTime to a string in ES format. - * - * @param \DateTime|int $value - * @return string - */ - public function fromDateTimeSearchable($value): string - { - return empty($value) ? $value : $this->asDateTime($value)->format($this->getSearchableDateFormat()); - } - - /** - * Return the format to be used for dates in Elasticsearch - * - * @return string - */ - public function getSearchableDateFormat(): string - { - return 'Y-m-d\TH:i:s'; - } - /** * Build index details for a sub document *