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