1
|
|
|
<?php |
2
|
|
|
namespace Elastica; |
3
|
|
|
|
4
|
|
|
use Elastica\Bulk\Action; |
5
|
|
|
use Elastica\Exception\InvalidException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Single document stored in elastic search. |
9
|
|
|
* |
10
|
|
|
* @author Nicolas Ruflin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Document extends AbstractUpdateAction |
13
|
|
|
{ |
14
|
|
|
const OP_TYPE_CREATE = Action::OP_TYPE_CREATE; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Document data. |
18
|
|
|
* |
19
|
|
|
* @var array Document data |
20
|
|
|
*/ |
21
|
|
|
protected $_data = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Whether to use this document to upsert if the document does not exist. |
25
|
|
|
* |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $_docAsUpsert = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $_autoPopulate = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates a new document. |
37
|
|
|
* |
38
|
|
|
* @param int|string $id OPTIONAL $id Id is create if empty |
39
|
|
|
* @param array|string $data OPTIONAL Data array |
40
|
|
|
* @param Type|string $type OPTIONAL Type name |
41
|
|
|
* @param Index|string $index OPTIONAL Index name |
42
|
|
|
*/ |
43
|
|
|
public function __construct($id = '', $data = [], $type = '', $index = '') |
44
|
|
|
{ |
45
|
|
|
$this->setId($id); |
46
|
|
|
$this->setData($data); |
47
|
|
|
$this->setType($type); |
48
|
|
|
$this->setIndex($index); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $key |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public function __get($key) |
57
|
|
|
{ |
58
|
|
|
return $this->get($key); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $key |
63
|
|
|
* @param mixed $value |
64
|
|
|
*/ |
65
|
|
|
public function __set($key, $value) |
66
|
|
|
{ |
67
|
|
|
$this->set($key, $value); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $key |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function __isset($key) |
76
|
|
|
{ |
77
|
|
|
return $this->has($key) && null !== $this->get($key); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string $key |
82
|
|
|
*/ |
83
|
|
|
public function __unset($key) |
84
|
|
|
{ |
85
|
|
|
$this->remove($key); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $key |
90
|
|
|
* |
91
|
|
|
* @throws \Elastica\Exception\InvalidException |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function get($key) |
96
|
|
|
{ |
97
|
|
|
if (!$this->has($key)) { |
98
|
|
|
throw new InvalidException("Field {$key} does not exist"); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->_data[$key]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $key |
106
|
|
|
* @param mixed $value |
107
|
|
|
* |
108
|
|
|
* @throws \Elastica\Exception\InvalidException |
109
|
|
|
* |
110
|
|
|
* @return $this |
111
|
|
|
*/ |
112
|
|
|
public function set($key, $value) |
113
|
|
|
{ |
114
|
|
|
if (!is_array($this->_data)) { |
115
|
|
|
throw new InvalidException('Document data is serialized data. Data creation is forbidden.'); |
116
|
|
|
} |
117
|
|
|
$this->_data[$key] = $value; |
118
|
|
|
|
119
|
|
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $key |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function has($key) |
128
|
|
|
{ |
129
|
|
|
return is_array($this->_data) && array_key_exists($key, $this->_data); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $key |
134
|
|
|
* |
135
|
|
|
* @throws \Elastica\Exception\InvalidException |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
*/ |
139
|
|
|
public function remove($key) |
140
|
|
|
{ |
141
|
|
|
if (!$this->has($key)) { |
142
|
|
|
throw new InvalidException("Field {$key} does not exist"); |
143
|
|
|
} |
144
|
|
|
unset($this->_data[$key]); |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Adds a file to the index. |
151
|
|
|
* |
152
|
|
|
* To use this feature you have to call the following command in the |
153
|
|
|
* elasticsearch directory: |
154
|
|
|
* <code> |
155
|
|
|
* ./bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.6.0 |
156
|
|
|
* </code> |
157
|
|
|
* This installs the tika file analysis plugin. More infos about supported formats |
158
|
|
|
* can be found here: {@link http://tika.apache.org/0.7/formats.html} |
159
|
|
|
* |
160
|
|
|
* @param string $key Key to add the file to |
161
|
|
|
* @param string $filepath Path to add the file |
162
|
|
|
* @param string $mimeType OPTIONAL Header mime type |
163
|
|
|
* |
164
|
|
|
* @return $this |
165
|
|
|
*/ |
166
|
|
|
public function addFile($key, $filepath, $mimeType = '') |
167
|
|
|
{ |
168
|
|
|
$value = base64_encode(file_get_contents($filepath)); |
169
|
|
|
|
170
|
|
|
if (!empty($mimeType)) { |
171
|
|
|
$value = ['_content_type' => $mimeType, '_name' => $filepath, '_content' => $value]; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->set($key, $value); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Add file content. |
181
|
|
|
* |
182
|
|
|
* @param string $key Document key |
183
|
|
|
* @param string $content Raw file content |
184
|
|
|
* |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function addFileContent($key, $content) |
188
|
|
|
{ |
189
|
|
|
return $this->set($key, base64_encode($content)); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Adds a geopoint to the document. |
194
|
|
|
* |
195
|
|
|
* Geohashes are not yet supported |
196
|
|
|
* |
197
|
|
|
* @param string $key Field key |
198
|
|
|
* @param float $latitude Latitude value |
199
|
|
|
* @param float $longitude Longitude value |
200
|
|
|
* |
201
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-geo-point-type.html |
202
|
|
|
* |
203
|
|
|
* @return $this |
204
|
|
|
*/ |
205
|
|
|
public function addGeoPoint($key, $latitude, $longitude) |
206
|
|
|
{ |
207
|
|
|
$value = ['lat' => $latitude, 'lon' => $longitude]; |
208
|
|
|
|
209
|
|
|
$this->set($key, $value); |
210
|
|
|
|
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Overwrites the current document data with the given data. |
216
|
|
|
* |
217
|
|
|
* @param array|string $data Data array |
218
|
|
|
* |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function setData($data) |
222
|
|
|
{ |
223
|
|
|
$this->_data = $data; |
|
|
|
|
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Returns the document data. |
230
|
|
|
* |
231
|
|
|
* @return array|string Document data |
232
|
|
|
*/ |
233
|
|
|
public function getData() |
234
|
|
|
{ |
235
|
|
|
return $this->_data; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @param bool $value |
240
|
|
|
* |
241
|
|
|
* @return $this |
242
|
|
|
*/ |
243
|
|
|
public function setDocAsUpsert($value) |
244
|
|
|
{ |
245
|
|
|
$this->_docAsUpsert = (bool) $value; |
246
|
|
|
|
247
|
|
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return bool |
252
|
|
|
*/ |
253
|
|
|
public function getDocAsUpsert() |
254
|
|
|
{ |
255
|
|
|
return $this->_docAsUpsert; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param bool $autoPopulate |
260
|
|
|
* |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
public function setAutoPopulate($autoPopulate = true) |
264
|
|
|
{ |
265
|
|
|
$this->_autoPopulate = (bool) $autoPopulate; |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
public function isAutoPopulate() |
274
|
|
|
{ |
275
|
|
|
return $this->_autoPopulate; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Sets pipeline |
280
|
|
|
* |
281
|
|
|
* @param string $pipeline |
282
|
|
|
* |
283
|
|
|
* @return $this |
284
|
|
|
*/ |
285
|
|
|
public function setPipeline($pipeline) |
286
|
|
|
{ |
287
|
|
|
return $this->setParam('_pipeline', $pipeline); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
|
|
public function getPipeline() |
294
|
|
|
{ |
295
|
|
|
return $this->getParam('_pipeline'); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return bool |
300
|
|
|
*/ |
301
|
|
|
public function hasPipeline() |
302
|
|
|
{ |
303
|
|
|
return $this->hasParam('_pipeline'); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns the document as an array. |
308
|
|
|
* |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
|
|
public function toArray() |
312
|
|
|
{ |
313
|
|
|
$doc = $this->getParams(); |
314
|
|
|
$doc['_source'] = $this->getData(); |
315
|
|
|
|
316
|
|
|
return $doc; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param array|\Elastica\Document $data |
321
|
|
|
* |
322
|
|
|
* @throws \Elastica\Exception\InvalidException |
323
|
|
|
* |
324
|
|
|
* @return self |
325
|
|
|
*/ |
326
|
|
View Code Duplication |
public static function create($data) |
|
|
|
|
327
|
|
|
{ |
328
|
|
|
if ($data instanceof self) { |
329
|
|
|
return $data; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
if (is_array($data)) { |
333
|
|
|
return new self('', $data); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
throw new InvalidException('Failed to create document. Invalid data passed.'); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.