|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Elasticquent; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | + |
| 7 | +class ElasticquentMultiSearch extends Model implements ElasticquentInterface |
| 8 | +{ |
| 9 | + use ElasticquentTrait { |
| 10 | + getBasicEsParams as TraitGetBasicEsParams; |
| 11 | + getIndexName as TraitGetIndexName; |
| 12 | + } |
| 13 | + |
| 14 | + protected $types = []; |
| 15 | + |
| 16 | + protected $indices = []; |
| 17 | + |
| 18 | + public function __construct(array $attributes = []) |
| 19 | + { |
| 20 | + parent::__construct($attributes); |
| 21 | + |
| 22 | + // initialize indices |
| 23 | + $this->indices = [$this->TraitGetIndexName()]; |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Set indcies to search from |
| 28 | + * @param mixed $indices |
| 29 | + */ |
| 30 | + public function setIndexName($indices) |
| 31 | + { |
| 32 | + $this->indices = $indices; |
| 33 | + } |
| 34 | + |
| 35 | + public function getIndexName() |
| 36 | + { |
| 37 | + return $this->indices; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set types to search from |
| 42 | + * @param mixed $types |
| 43 | + */ |
| 44 | + public function setTypeName($types) |
| 45 | + { |
| 46 | + $this->types = $types; |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public function getTypeName() |
| 51 | + { |
| 52 | + return $this->types; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Create a elacticquent result collection of models from plain arrays. |
| 57 | + * |
| 58 | + * Use _type to instantiate models |
| 59 | + * |
| 60 | + * @param array $items |
| 61 | + * @param array $meta |
| 62 | + * @return \Elasticquent\ElasticquentResultCollection |
| 63 | + */ |
| 64 | + public static function hydrateElasticquentResult(array $items, $meta = null) |
| 65 | + { |
| 66 | + // Cache instances |
| 67 | + $instances = []; |
| 68 | + |
| 69 | + $results = []; |
| 70 | + |
| 71 | + foreach ($items as $item) { |
| 72 | + $className = $item['_type']; |
| 73 | + if (!class_exists($className)) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + if (!isset($instances[$className])) { |
| 77 | + $instances[$className] = new $className; |
| 78 | + } |
| 79 | + $results[] = $instances[$className]->newFromHitBuilder($item); |
| 80 | + } |
| 81 | + |
| 82 | + return (new static)->newElasticquentResultCollection($results, $meta); |
| 83 | + } |
| 84 | +} |
0 commit comments