Skip to content
This repository has been archived by the owner on Nov 4, 2021. It is now read-only.

Commit

Permalink
allow disabling database attribute hydration
Browse files Browse the repository at this point in the history
  • Loading branch information
IlmLV committed Nov 28, 2019
1 parent 3d6fb5c commit 93fd1b2
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions src/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,24 +295,7 @@ public function map(Builder $builder, $results, $model)
return Collection::make();
}

$scoutKeyName = $model->getScoutKeyName();

$columns = Arr::get($results, '_payload.body._source');

if (is_null($columns)) {
$columns = ['*'];
} else {
$columns[] = $scoutKeyName;
}

$ids = $this->mapIds($results)->all();

$query = $model::usesSoftDelete() ? $model->withTrashed() : $model->newQuery();

$models = $query
->whereIn($scoutKeyName, $ids)
->get($columns)
->keyBy($scoutKeyName);
$models = $this->hydrateModels($model, $results);

return Collection::make($results['hits']['hits'])
->map(function ($hit) use ($models) {
Expand All @@ -332,6 +315,49 @@ public function map(Builder $builder, $results, $model)
->values();
}

/**
* @param $model
* @param $results
* @return Collection
*/
public function hydrateModels($model, $results)
{
// Hydrate models from elastic index
if ($model->databaseHydrate === false) {
$hits = collect($results['hits']['hits']);
$className = get_class($model);
$models = new Collection();

$hits->each(function ($item, $key) use ($className, $model, $models) {
$attributes = $item['_source'];
$models->put($item['_id'], new $className($attributes));
});
}
// Hydrate models from database
else {
$scoutKeyName = $model->getScoutKeyName();

$columns = Arr::get($results, '_payload.body._source');

if (is_null($columns)) {
$columns = ['*'];
} else {
$columns[] = $scoutKeyName;
}

$ids = $this->mapIds($results)->all();

$query = $model::usesSoftDelete() ? $model->withTrashed() : $model->newQuery();

$models = $query
->whereIn($scoutKeyName, $ids)
->get($columns)
->keyBy($scoutKeyName);
}

return $models;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 93fd1b2

Please sign in to comment.