Skip to content

Commit a9a109f

Browse files
committed
Using multi indcies/types to search
1 parent 03aa15d commit a9a109f

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/ElasticquentMultiSearch.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/ElasticquentTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function newCollection(array $models = array())
7171
*/
7272
public function getTypeName()
7373
{
74-
return $this->getTable();
74+
return get_class($this);
7575
}
7676

7777
/**

0 commit comments

Comments
 (0)