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
|
|
|
|
10
|
|
|
class ElasticsearchDAL implements IDAL |
11
|
|
|
{ |
12
|
|
|
protected $model; |
13
|
|
|
protected $client; |
14
|
|
|
protected $emitter; |
15
|
|
|
|
16
|
54 |
|
public function __construct(ElasticsearchModel $model, Client $client, DALMediator $emitter) |
17
|
|
|
{ |
18
|
54 |
|
$this->model = $model; |
19
|
54 |
|
$this->client = $client; |
20
|
54 |
|
$this->emitter = $emitter; |
21
|
54 |
|
} |
22
|
|
|
|
23
|
|
|
public function getModel() |
24
|
|
|
{ |
25
|
|
|
return $this->model; |
26
|
|
|
} |
27
|
|
|
|
28
|
14 |
|
public function get($id, array $options = []) |
29
|
|
|
{ |
30
|
14 |
|
$params = $this->model->getPath()->toArray(); |
31
|
|
|
|
32
|
14 |
|
if (!empty($options['columns'])) { |
33
|
2 |
|
$params['_source'] = $options['columns']; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
14 |
|
if (!empty($options['parent'])) { |
37
|
2 |
|
$params['parent'] = $options['parent']; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
14 |
|
$response = $this->client->get($params); |
41
|
|
|
|
42
|
10 |
|
return $this->model->fillByResponse($response); |
43
|
|
|
} |
44
|
|
|
|
45
|
24 |
|
public function put(array $columns = ['*']) |
46
|
|
|
{ |
47
|
24 |
|
$params = $this->getParams(); |
48
|
|
|
|
49
|
24 |
|
if (!$this->model->_exist || $columns == ['*']) { |
50
|
22 |
|
if (!$params['id']) { |
51
|
|
|
unset($params['id']); |
52
|
|
|
} |
53
|
|
|
|
54
|
22 |
|
$params['body'] = $this->model->toArray(); |
55
|
|
|
|
56
|
22 |
|
$response = $this->client->index($params); |
57
|
22 |
|
} else { |
58
|
2 |
|
$params['body'] = [ |
59
|
2 |
|
'doc' => array_only($this->model->toArray(), $columns) |
60
|
2 |
|
]; |
61
|
|
|
|
62
|
2 |
|
$response = $this->client->update($params); |
63
|
|
|
} |
64
|
|
|
|
65
|
24 |
|
$this->model->setId($response['_id']); |
66
|
|
|
|
67
|
24 |
|
return $this->model->getId(); |
68
|
|
|
} |
69
|
|
|
|
70
|
6 |
|
public function delete() |
71
|
|
|
{ |
72
|
6 |
|
return $this->client->delete($this->getParams()); |
73
|
|
|
} |
74
|
|
|
|
75
|
24 |
|
protected function getParams() |
76
|
|
|
{ |
77
|
24 |
|
$params = $this->model->getPath()->toArray(); |
78
|
|
|
|
79
|
24 |
|
if ($this->model->getParentId()) { |
80
|
4 |
|
$params['parent'] = $this->model->getParentId(); |
81
|
4 |
|
} |
82
|
|
|
|
83
|
24 |
|
return $params; |
84
|
|
|
} |
85
|
|
|
|
86
|
24 |
|
public function search(array $query) |
87
|
|
|
{ |
88
|
22 |
|
if (empty($query['body']['query']) && empty($query['body']['filter'])) { |
89
|
12 |
|
$query['body']['query'] = [ |
90
|
12 |
|
'match_all' => [] |
91
|
12 |
|
]; |
92
|
12 |
|
} |
93
|
|
|
|
94
|
|
|
$params = [ |
95
|
22 |
|
'index' => $this->model->getIndex(), |
96
|
22 |
|
'type' => $this->model->getType(), |
97
|
22 |
|
'from' => Arr::get($query, 'from', 0), |
98
|
22 |
|
'size' => Arr::get($query, 'size', 50), |
99
|
22 |
|
'body' => $query['body'] |
100
|
22 |
|
]; |
101
|
|
|
|
102
|
22 |
|
$collection = new ElasticsearchCollection(); |
103
|
|
|
|
104
|
22 |
|
$this->emitter->trigger(DALMediator::EVENT_BEFORE_SEARCH, $params); |
105
|
|
|
|
106
|
22 |
|
$response = $this->client->search($params); |
107
|
|
|
|
108
|
24 |
|
$this->emitter->trigger(DALMediator::EVENT_AFTER_SEARCH, $response); |
109
|
|
|
|
110
|
22 |
|
$collection->response($response); |
111
|
|
|
|
112
|
22 |
|
$from = (int)$params['from']; |
113
|
22 |
|
foreach ($response['hits']['hits'] as $hit) { |
114
|
20 |
|
$model = $this->model->createInstance(); |
115
|
20 |
|
$model->_score = $hit['_score']; |
116
|
20 |
|
$model->_position = $from++; |
117
|
20 |
|
$model->_exist = true; |
118
|
20 |
|
$model->fillByResponse($hit); |
119
|
20 |
|
$model->fillByInnerHits($hit); |
120
|
20 |
|
$collection->put($model->getId(), $model); |
121
|
22 |
|
} |
122
|
|
|
|
123
|
22 |
|
return $collection; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|