Skip to content

Commit 175904b

Browse files
chuangbotimgws
authored andcommitted
Respect the Collection interface (#59)
* Be respectful to the Collection interface, fix collection methods e.g. map, filter, each, pluck, etc. * Add backwards compatibility * Check metadata items are set, use defaults if not set.
1 parent db463e4 commit 175904b

File tree

3 files changed

+74
-45
lines changed

3 files changed

+74
-45
lines changed

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ $params = array(
424424

425425
$params['body']['query']['match']['title'] = 'Moby Dick';
426426

427-
$collection = new \Elasticquent\ElasticquentResultCollection($client->search($params), new Book);
427+
$collection = Book::hydrateElasticsearchResult($client->search($params));
428428

429429
```
430430

src/ElasticquentResultCollection.php

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,53 @@ class ElasticquentResultCollection extends \Illuminate\Database\Eloquent\Collect
99
protected $shards;
1010
protected $hits;
1111
protected $aggregations = null;
12-
protected $instance;
1312

1413
/**
1514
* Create a new instance containing Elasticsearch results
1615
*
17-
* @param $results elasticsearch results
18-
* @param $instance
16+
* @todo Remove backwards compatible detection at further point
17+
* @deprecated Initialize with params ($results, $instance) is deprecated,
18+
* please use Model::hydrateElasticsearchResult($results).
19+
*
20+
* @param mixed $items
21+
* @param array $meta
22+
* @return void
1923
*/
20-
public function __construct($results, $instance = null)
24+
public function __construct($items, $meta = null)
2125
{
22-
// Take our result data and map it
23-
// to some class properties.
24-
$this->took = $results['took'];
25-
$this->timed_out = $results['timed_out'];
26-
$this->shards = $results['_shards'];
27-
$this->hits = $results['hits'];
28-
$this->aggregations = isset($results['aggregations']) ? $results['aggregations'] : array();
29-
30-
// Save the instance we performed the search on.
31-
// This is only done when Elasticquent creates the collection at first.
32-
if ($instance !== null) {
33-
$this->instance = $instance;
26+
// Detect if arguments are old deprecated version ($results, $instance)
27+
if (isset($items['hits']) and $meta instanceof \Illuminate\Database\Eloquent\Model) {
28+
$instance = $meta;
29+
$meta = $items;
30+
$items = $instance::hydrateElasticsearchResult($meta);
3431
}
3532

36-
// Now we need to assign our hits to the
37-
// items in the collection.
38-
$this->items = $this->hitsToItems($instance);
33+
parent::__construct($items);
34+
35+
// Take our result meta and map it
36+
// to some class properties.
37+
if (is_array($meta)) {
38+
$this->setMeta($meta);
39+
}
3940
}
4041

4142
/**
42-
* Set the model instance we performed the search on.
43+
* Set the result meta.
4344
*
44-
* @param $instance
45+
* @param array $meta
4546
* @return $this
4647
*/
47-
public function setInstance($instance)
48+
public function setMeta(array $meta)
4849
{
49-
$this->instance = $instance;
50+
$this->took = isset($meta['took']) ? $meta['took'] : null;
51+
$this->timed_out = isset($meta['timed_out']) ? $meta['timed_out'] : null;
52+
$this->shards = isset($meta['_shards']) ? $meta['_shards'] : null;
53+
$this->hits = isset($meta['hits']) ? $meta['hits'] : null;
54+
$this->aggregations = isset($meta['aggregations']) ? $meta['aggregations'] : [];
5055

5156
return $this;
5257
}
5358

54-
/**
55-
* Hits To Items
56-
*
57-
* @param Eloquent model instance $instance
58-
*
59-
* @return array
60-
*/
61-
private function hitsToItems($instance)
62-
{
63-
$items = array();
64-
65-
foreach ($this->hits['hits'] as $hit) {
66-
$items[] = $instance->newFromHitBuilder($hit);
67-
}
68-
69-
return $items;
70-
}
71-
7259
/**
7360
* Total Hits
7461
*

src/ElasticquentTrait.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public static function searchByQuery($query = null, $aggregations = null, $sourc
264264

265265
$result = $instance->getElasticSearchClient()->search($params);
266266

267-
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
267+
return static::hydrateElasticsearchResult($result);
268268
}
269269

270270
/**
@@ -281,7 +281,7 @@ public static function complexSearch($params)
281281

282282
$result = $instance->getElasticSearchClient()->search($params);
283283

284-
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
284+
return static::hydrateElasticsearchResult($result);
285285
}
286286

287287
/**
@@ -303,7 +303,7 @@ public static function search($term = '')
303303

304304
$result = $instance->getElasticSearchClient()->search($params);
305305

306-
return new \Elasticquent\ElasticquentResultCollection($result, $instance = new static);
306+
return static::hydrateElasticsearchResult($result);
307307
}
308308

309309
/**
@@ -639,6 +639,36 @@ public function newFromHitBuilder($hit = array())
639639
return $instance;
640640
}
641641

642+
/**
643+
* Create a elacticquent result collection of models from plain elasticsearch result.
644+
*
645+
* @param array $result
646+
* @return \Elasticquent\ElasticquentResultCollection
647+
*/
648+
public static function hydrateElasticsearchResult(array $result)
649+
{
650+
$items = $result['hits']['hits'];
651+
return static::hydrateElasticquentResult($items, $meta = $result);
652+
}
653+
654+
/**
655+
* Create a elacticquent result collection of models from plain arrays.
656+
*
657+
* @param array $items
658+
* @param array $meta
659+
* @return \Elasticquent\ElasticquentResultCollection
660+
*/
661+
public static function hydrateElasticquentResult(array $items, $meta = null)
662+
{
663+
$instance = new static;
664+
665+
$items = array_map(function ($item) use ($instance) {
666+
return $instance->newFromHitBuilder($item);
667+
}, $items);
668+
669+
return $instance->newElasticquentResultCollection($items, $meta);
670+
}
671+
642672
/**
643673
* Create a new model instance that is existing recursive.
644674
*
@@ -732,6 +762,18 @@ public static function loadPivotAttribute(Model $model, Relation $parentRelation
732762
}
733763
}
734764

765+
/**
766+
* Create a new Elasticquent Result Collection instance.
767+
*
768+
* @param array $models
769+
* @param array $meta
770+
* @return \Elasticquent\ElasticquentResultCollection
771+
*/
772+
public function newElasticquentResultCollection(array $models = [], $meta = null)
773+
{
774+
return new ElasticquentResultCollection($models, $meta);
775+
}
776+
735777
/**
736778
* Check if an array is multi-level array like [[id], [id], [id]].
737779
*

0 commit comments

Comments
 (0)