Skip to content

Commit

Permalink
Merge pull request #40 from spira/hotfix/elastic-indexing
Browse files Browse the repository at this point in the history
When indexing nested entities we only index properties as defined by …
  • Loading branch information
Jeremy Sik committed Feb 1, 2016
2 parents 9062bb6 + 6a2df82 commit 5ab86aa
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions Model/Model/IndexedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ public function countIndex()
return $instance->getElasticSearchClient()->count($params);
}

/**
* Get entity data which is to be entered into search index.
*
* Note: Every entity, including it's nested entities, requires mappingProperties to be defined in it's model
* or it will not be indexed correctly.
*
* @return array
*/
public function getIndexDocumentData()
{
$relations = [];
Expand All @@ -113,12 +121,16 @@ public function getIndexDocumentData()
// We have to do this because we don't know if the relation is one to or one to many. If it is one to one
// we don't want to strip out the keys.
foreach ($this->indexRelations as $nestedModelName) {
/** @var IndexedModel|IndexedCollection $results */
$results = $this->$nestedModelName()->getResults();

if ($results instanceof Collection) {
$relations[snake_case($nestedModelName)] = array_values($results->toArray());
$nestedData = $results->map(function (IndexedModel $result) {
return array_intersect_key($result->attributesToArray(), $result->mappingProperties);
});

$relations[snake_case($nestedModelName)] = $nestedData;
} else {
$relations[snake_case($nestedModelName)] = $results->toArray();
$relations[snake_case($nestedModelName)] = array_intersect_key($results->attributesToArray(), $results->mappingProperties);
}
}
}
Expand Down

0 comments on commit 5ab86aa

Please sign in to comment.