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
|
2 |
|
public function __construct(?OngrSearch $search = null, ?Model $model = null) |
29
|
|
|
{ |
30
|
2 |
|
$this->dsl = ElasticDsl::search($search); |
31
|
2 |
|
$this->model = $model; |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add model |
36
|
|
|
* |
37
|
|
|
* @param Model|Searchable $model |
38
|
|
|
* @return SearchDsl|\Triadev\Es\Dsl\Dsl\Search |
39
|
|
|
* |
40
|
|
|
* @throws \InvalidArgumentException |
41
|
|
|
*/ |
42
|
1 |
|
public function model(Model $model) : SearchDsl |
43
|
|
|
{ |
44
|
1 |
|
$this->isModelSearchable($model); |
45
|
|
|
|
46
|
1 |
|
$this->model = $model; |
47
|
|
|
|
48
|
1 |
|
if (is_string($index = $model->getDocumentIndex())) { |
49
|
1 |
|
$this->dsl->esIndex($index); |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
$this->dsl->esType($model->getDocumentType()); |
|
|
|
|
53
|
|
|
|
54
|
1 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get |
59
|
|
|
* |
60
|
|
|
* @param FillerContract|null $filler |
61
|
|
|
* @return SearchResult |
62
|
|
|
*/ |
63
|
2 |
|
public function get(?FillerContract $filler = null) : SearchResult |
64
|
|
|
{ |
65
|
2 |
|
$searchResult = $this->dsl->get(); |
66
|
|
|
|
67
|
2 |
|
if ($this->model) { |
68
|
1 |
|
$filler = $filler ?: new EloquentFiller(); |
69
|
1 |
|
$filler->fill($this->model, $searchResult); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return $searchResult; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Call methods in elasticsearch dsl |
77
|
|
|
* |
78
|
|
|
* @param $name |
79
|
|
|
* @param $arguments |
80
|
|
|
* @return SearchDsl |
81
|
|
|
*/ |
82
|
2 |
|
public function __call($name, $arguments) : SearchDsl |
83
|
|
|
{ |
84
|
2 |
|
call_user_func_array([$this->dsl, $name], $arguments); |
85
|
|
|
|
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|