|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\DAL; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
use Isswp101\Persimmon\Collection\ElasticsearchCollection; |
|
8
|
|
|
use Isswp101\Persimmon\ElasticsearchModel; |
|
9
|
|
|
use Isswp101\Persimmon\Event\EventEmitter; |
|
10
|
|
|
use Isswp101\Persimmon\Model; |
|
11
|
|
|
|
|
12
|
|
|
class ElasticsearchDAL implements IDAL |
|
13
|
|
|
{ |
|
14
|
|
|
protected $model; |
|
15
|
|
|
protected $client; |
|
16
|
|
|
protected $emitter; |
|
17
|
|
|
|
|
18
|
27 |
|
public function __construct(ElasticsearchModel $model, Client $client, EventEmitter $emitter) |
|
19
|
|
|
{ |
|
20
|
27 |
|
$this->model = $model; |
|
21
|
27 |
|
$this->client = $client; |
|
22
|
27 |
|
$this->emitter = $emitter; |
|
23
|
27 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function setModel(Model $model) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->model = $model; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function getModel() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->model; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getEventEmitter() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->emitter; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
7 |
|
public function get($id, array $options = []) |
|
41
|
|
|
{ |
|
42
|
7 |
|
$params = $this->model->getPath()->toArray(); |
|
43
|
|
|
|
|
44
|
7 |
|
if (!empty($options['columns'])) { |
|
45
|
1 |
|
$params['_source'] = $options['columns']; |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
7 |
|
if (!empty($options['parent'])) { |
|
49
|
1 |
|
$params['parent'] = $options['parent']; |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
7 |
|
$response = $this->client->get($params); |
|
53
|
|
|
|
|
54
|
5 |
|
$this->model->fillByResponse($response); |
|
55
|
|
|
|
|
56
|
5 |
|
if (isset($params['parent'])) { |
|
57
|
1 |
|
$this->model->setParentId($params['parent']); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
return $this->model; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
12 |
|
public function put(array $columns = ['*']) |
|
64
|
|
|
{ |
|
65
|
12 |
|
$params = $this->getParams(); |
|
66
|
|
|
|
|
67
|
12 |
|
if (!$this->model->_exist || $columns == ['*']) { |
|
68
|
11 |
|
if (!$params['id']) { |
|
69
|
|
|
unset($params['id']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
11 |
|
$params['body'] = $this->model->toArray(); |
|
73
|
|
|
|
|
74
|
11 |
|
$response = $this->client->index($params); |
|
75
|
11 |
|
} else { |
|
76
|
1 |
|
$params['body'] = [ |
|
77
|
1 |
|
'doc' => array_only($this->model->toArray(), $columns) |
|
78
|
1 |
|
]; |
|
79
|
|
|
|
|
80
|
1 |
|
$response = $this->client->update($params); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
$this->model->setId($response['_id']); |
|
84
|
|
|
|
|
85
|
12 |
|
return $this->model->getId(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
public function delete() |
|
89
|
|
|
{ |
|
90
|
3 |
|
return $this->client->delete($this->getParams()); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
protected function getParams() |
|
94
|
|
|
{ |
|
95
|
12 |
|
$params = $this->model->getPath()->toArray(); |
|
96
|
|
|
|
|
97
|
12 |
|
if ($this->model->getParentId()) { |
|
98
|
2 |
|
$params['parent'] = $this->model->getParentId(); |
|
99
|
2 |
|
} |
|
100
|
|
|
|
|
101
|
12 |
|
return $params; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
12 |
|
public function search(array $query) |
|
105
|
|
|
{ |
|
106
|
11 |
|
if (empty($query['body']['query']) && empty($query['body']['filter'])) { |
|
107
|
6 |
|
$query['body']['query'] = [ |
|
108
|
7 |
|
'match_all' => [] |
|
109
|
6 |
|
]; |
|
110
|
6 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
$params = [ |
|
113
|
11 |
|
'index' => $this->model->getIndex(), |
|
114
|
11 |
|
'type' => $this->model->getType(), |
|
115
|
11 |
|
'from' => Arr::get($query, 'from', 0), |
|
116
|
11 |
|
'size' => Arr::get($query, 'size', 50), |
|
117
|
11 |
|
'body' => $query['body'] |
|
118
|
11 |
|
]; |
|
119
|
|
|
|
|
120
|
11 |
|
$collection = new ElasticsearchCollection(); |
|
121
|
|
|
|
|
122
|
11 |
|
$this->emitter->trigger(DALEvents::BEFORE_SEARCH, $params); |
|
123
|
|
|
|
|
124
|
11 |
|
$response = $this->client->search($params); |
|
125
|
|
|
|
|
126
|
11 |
|
$this->emitter->trigger(DALEvents::AFTER_SEARCH, $response); |
|
127
|
|
|
|
|
128
|
12 |
|
$collection->response($response); |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
11 |
|
$from = (int)$params['from']; |
|
131
|
11 |
|
foreach ($response['hits']['hits'] as $hit) { |
|
132
|
10 |
|
$model = $this->model->createInstance(); |
|
133
|
10 |
|
$model->_score = $hit['_score']; |
|
134
|
10 |
|
$model->_position = $from++; |
|
135
|
10 |
|
$model->_exist = true; |
|
136
|
10 |
|
$model->fillByResponse($hit); |
|
137
|
10 |
|
$model->fillByInnerHits($hit); |
|
138
|
10 |
|
$collection->put($model->getId(), $model); |
|
139
|
11 |
|
} |
|
140
|
|
|
|
|
141
|
11 |
|
return $collection; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: