|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Type; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
|
6
|
|
|
use Nimble\ElasticBundle\Document; |
|
7
|
|
|
use Nimble\ElasticBundle\Index\Index; |
|
8
|
|
|
|
|
9
|
|
|
class Type |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $name; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Index |
|
18
|
|
|
*/ |
|
19
|
|
|
private $index; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $mappings; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param Index $index |
|
29
|
|
|
* @param array $mappings |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($name, Index $index, array $mappings = null) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->name = $name; |
|
34
|
|
|
$this->index = $index; |
|
35
|
|
|
$this->mappings = $mappings; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getIndexName() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->index->getName(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function getIndexId() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->index->getId(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Creates client request params appending options that define the index and type. |
|
56
|
|
|
* |
|
57
|
|
|
* @param array $params |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function createRequestParams(array $params = []) |
|
61
|
|
|
{ |
|
62
|
|
|
return array_merge([ |
|
63
|
|
|
'index' => $this->getIndexName(), |
|
64
|
|
|
'type' => $this->getName(), |
|
65
|
|
|
], $params); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return Client |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getClient() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->index->getClient(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Checks if the index exists in ES. |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public function exists() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->getClient()->indices()->exists( |
|
84
|
|
|
$this->createRequestParams() |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates the type mapping in ES. |
|
90
|
|
|
*/ |
|
91
|
|
|
public function createMappings() |
|
92
|
|
|
{ |
|
93
|
|
|
if (empty($this->mappings)) { |
|
94
|
|
|
return; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->getClient()->indices()->putMapping( |
|
98
|
|
|
$this->createRequestParams([ |
|
99
|
|
|
'body' => [$this->name => $this->mappings] |
|
100
|
|
|
]) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Deletes the type mappings in ES. |
|
106
|
|
|
*/ |
|
107
|
|
|
public function deleteMappings() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->getClient()->indices()->deleteMapping( |
|
110
|
|
|
$this->createRequestParams() |
|
111
|
|
|
); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Resets the type mappings in ES. |
|
116
|
|
|
*/ |
|
117
|
|
|
public function reset() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->deleteMappings(); |
|
120
|
|
|
$this->createMappings(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getMappings() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->mappings; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getName() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->name; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return Index |
|
141
|
|
|
*/ |
|
142
|
|
|
public function getIndex() |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->index; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param Document $document |
|
149
|
|
|
*/ |
|
150
|
|
|
public function putDocument(Document $document) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->index->putDocument($this->name, $document); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param string|int $id |
|
157
|
|
|
*/ |
|
158
|
|
|
public function deleteDocument($id) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->index->deleteDocument($this->name, $id); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param array $documents |
|
165
|
|
|
*/ |
|
166
|
|
|
public function putDocuments(array $documents) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->index->putDocuments($this->name, $documents); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param array $ids |
|
173
|
|
|
*/ |
|
174
|
|
|
public function deleteDocuments(array $ids) |
|
175
|
|
|
{ |
|
176
|
|
|
$this->index->deleteDocuments($this->name, $ids); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param array|string $query Array that will be serialized or raw JSON. |
|
181
|
|
|
* @param array $options |
|
182
|
|
|
* @return array |
|
183
|
|
|
*/ |
|
184
|
|
|
public function search($query, array $options = []) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->index->search($query, $options, $this->name); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param array|string $query Array that will be serialized or raw JSON. |
|
191
|
|
|
* @param array $options |
|
192
|
|
|
* @return int|null |
|
193
|
|
|
*/ |
|
194
|
|
|
public function count($query, array $options = []) |
|
195
|
|
|
{ |
|
196
|
|
|
return $this->index->count($query, $options, $this->name); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @param array|string $query |
|
201
|
|
|
* @param array $options |
|
202
|
|
|
*/ |
|
203
|
|
|
public function deleteByQuery($query, array $options = []) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->index->deleteByQuery($query, $options, $this->name); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|