@@ -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 *
0 commit comments