|
1
|
|
|
<?php |
|
2
|
|
|
namespace Triadev\Leopard\Business\Dsl; |
|
3
|
|
|
|
|
4
|
|
|
use ONGR\ElasticsearchDSL\Search as OngrSearch; |
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Triadev\Es\Dsl\Facade\ElasticDsl; |
|
7
|
|
|
use Triadev\Es\Dsl\Model\SearchResult; |
|
8
|
|
|
use Triadev\Leopard\Business\Filler\EloquentFiller; |
|
9
|
|
|
use Triadev\Leopard\Business\Helper\IsModelSearchable; |
|
10
|
|
|
use Triadev\Leopard\Contract\FillerContract; |
|
11
|
|
|
use Triadev\Leopard\Searchable; |
|
12
|
|
|
|
|
13
|
|
|
class SearchDsl |
|
14
|
|
|
{ |
|
15
|
|
|
use IsModelSearchable; |
|
16
|
|
|
|
|
17
|
|
|
/** @var \Triadev\Es\Dsl\Dsl\Search */ |
|
18
|
|
|
private $dsl; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Model|null */ |
|
21
|
|
|
public $model; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* SearchDsl constructor. |
|
25
|
|
|
* @param OngrSearch|null $search |
|
26
|
|
|
* @param Model|null $model |
|
27
|
|
|
*/ |
|
28
|
5 |
|
public function __construct(?OngrSearch $search = null, ?Model $model = null) |
|
29
|
|
|
{ |
|
30
|
5 |
|
$this->dsl = ElasticDsl::search($search); |
|
31
|
|
|
|
|
32
|
5 |
|
if ($model) { |
|
33
|
|
|
$this->setModel($model); |
|
34
|
|
|
} |
|
35
|
5 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Add model |
|
39
|
|
|
* |
|
40
|
|
|
* @param Model|Searchable $model |
|
41
|
|
|
* @return SearchDsl|\Triadev\Es\Dsl\Dsl\Search |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \InvalidArgumentException |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function model(Model $model) : SearchDsl |
|
46
|
|
|
{ |
|
47
|
1 |
|
$this->setModel($model); |
|
48
|
1 |
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Model|Searchable $model |
|
53
|
|
|
*/ |
|
54
|
1 |
|
private function setModel(Model $model) |
|
55
|
|
|
{ |
|
56
|
1 |
|
$this->isModelSearchable($model); |
|
57
|
|
|
|
|
58
|
1 |
|
$this->model = $model; |
|
59
|
|
|
|
|
60
|
1 |
|
if (is_string($index = $model->getDocumentIndex())) { |
|
61
|
1 |
|
$this->dsl->esIndex($index); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
$this->dsl->esType($model->getDocumentType()); |
|
|
|
|
|
|
65
|
1 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Get |
|
69
|
|
|
* |
|
70
|
|
|
* @param FillerContract|null $filler |
|
71
|
|
|
* @return SearchResult |
|
72
|
|
|
*/ |
|
73
|
2 |
|
public function get(?FillerContract $filler = null) : SearchResult |
|
74
|
|
|
{ |
|
75
|
2 |
|
$searchResult = $this->dsl->get(); |
|
76
|
|
|
|
|
77
|
2 |
|
if ($this->model) { |
|
78
|
1 |
|
$filler = $filler ?: new EloquentFiller(); |
|
79
|
1 |
|
$filler->fill($this->model, $searchResult); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return $searchResult; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Call methods in elasticsearch dsl |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @param array $arguments |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
*/ |
|
92
|
5 |
|
public function __call(string $name, array $arguments) |
|
93
|
|
|
{ |
|
94
|
5 |
|
$resultMethods = ['toDsl', 'getSearch', 'getQuery']; |
|
95
|
|
|
|
|
96
|
5 |
|
if (in_array($name, $resultMethods)) { |
|
97
|
3 |
|
return $this->dsl->$name(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
4 |
|
$this->dsl = call_user_func_array([$this->dsl, $name], $arguments); |
|
101
|
|
|
|
|
102
|
4 |
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|