-
Notifications
You must be signed in to change notification settings - Fork 730
Expand file tree
/
Copy pathDocument.php
More file actions
277 lines (232 loc) · 6.34 KB
/
Document.php
File metadata and controls
277 lines (232 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
namespace Elastica;
use Elastica\Bulk\Action;
use Elastica\Exception\InvalidException;
/**
* Single document stored in elastic search.
*
* @author Nicolas Ruflin <spam@ruflin.com>
*/
class Document extends AbstractUpdateAction
{
public const OP_TYPE_CREATE = Action::OP_TYPE_CREATE;
/**
* Document data.
*
* @var array Document data
*/
protected $_data = [];
/**
* Whether to use this document to upsert if the document does not exist.
*
* @var bool
*/
protected $_docAsUpsert = false;
/**
* @var bool
*/
protected $_autoPopulate = false;
/**
* Creates a new document.
*
* @param string|null $id The document ID, if null it will be created
* @param array|string $data Data array
* @param Index|string $index Index name
*/
public function __construct(?string $id = null, $data = [], $index = '')
{
$this->setId($id);
$this->setData($data);
$this->setIndex($index);
}
public function __get(string $key)
{
return $this->get($key);
}
public function __set(string $key, $value): void
{
$this->set($key, $value);
}
public function __isset(string $key): bool
{
return $this->has($key) && null !== $this->get($key);
}
public function __unset(string $key): void
{
$this->remove($key);
}
/**
* Get the value of the given field.
*
* @throws InvalidException If the given field does not exist
*/
public function get($key)
{
if (!$this->has($key)) {
throw new InvalidException("Field {$key} does not exist");
}
return $this->_data[$key];
}
/**
* Set the value of the given field.
*
* @throws InvalidException if the current document is a serialized data
*/
public function set(string $key, $value): self
{
if (!\is_array($this->_data)) {
throw new InvalidException('Document data is serialized data. Data creation is forbidden.');
}
$this->_data[$key] = $value;
return $this;
}
/**
* Returns if the current document has the given field.
*/
public function has(string $key): bool
{
return \is_array($this->_data) && \array_key_exists($key, $this->_data);
}
/**
* Removes a field from the document, by the given key.
*
* @throws InvalidException if the given field does not exist
*/
public function remove(string $key): self
{
if (!$this->has($key)) {
throw new InvalidException("Field {$key} does not exist");
}
unset($this->_data[$key]);
return $this;
}
/**
* Adds a file to the index.
*
* To use this feature you have to call the following command in the
* elasticsearch directory:
* <code>
* ./bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/1.6.0
* </code>
* This installs the tika file analysis plugin. More infos about supported formats
* can be found here: {@link http://tika.apache.org/0.7/formats.html}
*
* @param string $key Key to add the file to
* @param string $filepath Path to add the file
* @param string $mimeType Header mime type
*/
public function addFile(string $key, string $filepath, string $mimeType = ''): self
{
$value = \base64_encode(\file_get_contents($filepath));
if ('' !== $mimeType) {
$value = ['_content_type' => $mimeType, '_name' => $filepath, '_content' => $value];
}
$this->set($key, $value);
return $this;
}
/**
* Add file content.
*/
public function addFileContent(string $key, string $content): self
{
return $this->set($key, \base64_encode($content));
}
/**
* Adds a geopoint field to the document.
*
* @param string $key Field key
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html
*/
public function addGeoPoint(string $key, float $latitude, float $longitude): self
{
$value = ['lat' => $latitude, 'lon' => $longitude];
$this->set($key, $value);
return $this;
}
/**
* Overwrites the current document data with the given data.
*
* @param array|string $data Data array
*/
public function setData($data): self
{
$this->_data = $data;
return $this;
}
/**
* Returns the document data.
*
* @return array|string Document data
*/
public function getData()
{
return $this->_data;
}
public function setDocAsUpsert(bool $value): self
{
$this->_docAsUpsert = $value;
return $this;
}
public function getDocAsUpsert(): bool
{
return $this->_docAsUpsert;
}
public function setAutoPopulate(bool $autoPopulate = true): self
{
$this->_autoPopulate = $autoPopulate;
return $this;
}
public function isAutoPopulate(): bool
{
return $this->_autoPopulate;
}
public function setPipeline(string $pipeline): self
{
return $this->setParam('pipeline', $pipeline);
}
public function getPipeline(): string
{
return $this->getParam('pipeline');
}
public function hasPipeline(): bool
{
return $this->hasParam('pipeline');
}
/**
* @see \Elastica\Query::setSearchAfter()
*
* Returns the sort position for this document, which can be used as starting offset to search next hits page.
*
* @return array
*/
public function getSort(): array
{
return $this->getParam('sort');
}
/**
* Returns the document as an array.
*/
public function toArray(): array
{
$doc = $this->getParams();
$doc['_source'] = $this->getData();
return $doc;
}
/**
* @param array|Document $data
*
* @throws InvalidException If invalid data has been provided
*/
public static function create($data): self
{
if ($data instanceof self) {
return $data;
}
if (\is_array($data)) {
return new static('', $data);
}
throw new InvalidException('Failed to create document. Invalid data passed.');
}
}