forked from jreinke/magento-elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDocument.php
344 lines (305 loc) · 6.76 KB
/
Document.php
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Single document stored in elastic search
*
* @category Xodoa
* @package Elastica
* @author Nicolas Ruflin <spam@ruflin.com>
*/
class Elastica_Document {
/**
* Document id
*
* @var string|int Document id
*/
protected $_id = '';
/**
* Document data
*
* @var array Document data
*/
protected $_data = array();
/**
* Document type name
*
* @var string Document type name
*/
protected $_type = '';
/**
* Document index name
*
* @var string Document index name
*/
protected $_index = '';
/**
* Document version
*
* @var string Document version
*/
protected $_version = '';
/**
* Parent document id
*
* @var string|int Parent document id
*/
protected $_parent = '';
/**
* Optype
*
* @var string Optype
*/
protected $_optype = '';
/**
* Percolate
*
* @var string Percolate
*/
protected $_percolate = '';
/**
* Creates a new document
*
* @param int $id OPTIONAL $id Id is create if empty
* @param array $data OPTIONAL Data array
* @param string $type OPTIONAL Type name
* @param string $index OPTIONAL Index name
*/
public function __construct($id = '', array $data = array(), $type = '', $index = '') {
$this->_id = $id;
$this->setData($data);
$this->setType($type);
$this->setIndex($index);
}
/**
* Returns document id
*
* @return string|int Document id
*/
public function getId() {
return $this->_id;
}
/**
* Adds the given key/value pair to the document
*
* @param string $key Document entry key
* @param mixed $value Document entry value
* @return Elastica_Document
*/
public function add($key, $value) {
$this->_data[$key] = $value;
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.2.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 OPTIONAL Header mime type
* @return Elastica_Document
*/
public function addFile($key, $filepath, $mimeType = '') {
$value = base64_encode(file_get_contents($filepath));
if (!empty($mimeType)) {
$value = array('_content_type' => $mimeType, '_name' => $filepath, 'content' => $value,);
}
$this->add($key, $value);
return $this;
}
/**
* Add file content
*
* @param string $key Document key
* @param string $content Raw file content
* @return Elastica_Document
*/
public function addFileContent($key, $content) {
return $this->add($key, base64_encode($content));
}
/**
* Adds a geopoint to the document
*
* Geohashes re not yet supported
*
* @param string $key Field key
* @param float $latitude Latitud value
* @param float $longitude Longitude value
* @link http://www.elasticsearch.com/docs/elasticsearch/mapping/geo_point/
* @return Elastica_Document
*/
public function addGeoPoint($key, $latitude, $longitude) {
$value = array('lat' => $latitude, 'lon' => $longitude,);
$this->add($key, $value);
return $this;
}
/**
* Overwrites the curent document data with the given data
*
* @param array $data Data array
* @return Elastica_Document
*/
public function setData(array $data) {
$this->_data = $data;
return $this;
}
/**
* Sets lifetime of document
*
* @param string $ttl
* @return Elastica_Document
*/
public function setTTL($ttl) {
return $this->add('_ttl', $ttl);
}
/**
* Returns the document data
*
* @return array Document data
*/
public function getData() {
return $this->_data;
}
/**
* Sets the document type name
*
* @param string $type Type name
* @return Elastica_Document Current object
*/
public function setType($type) {
$this->_type = $type;
return $this;
}
/**
* Return document type name
*
* @return string Document type name
* @throws Elastica_Exception_Invalid
*/
public function getType() {
$type = $this->_type;
if (empty($type)) {
throw new Elastica_Exception_Invalid('Type not set');
}
return $type;
}
/**
* Sets the document index name
*
* @param string $index Index name
* @return Elastica_Document Current object
*/
public function setIndex($index) {
$this->_index = $index;
return $this;
}
/**
* Get the document index name
*
* @return string Index name
* @throws Elastica_Exception_Invalid
*/
public function getIndex() {
$index = $this->_index;
if (empty($index)) {
throw new Elastica_Exception_Invalid('Index not set');
}
return $index;
}
/**
* Sets the version of a document for use with optimistic concurrency control
*
* @param int $version Document version
* @return Elastica_Document Current object
* @link http://www.elasticsearch.org/blog/2011/02/08/versioning.html
*/
public function setVersion($version) {
if ($version !== '') {
$this->_version = (int) $version;
}
return $this;
}
/**
* Returns document version
*
* @return string|int Document version
*/
public function getVersion() {
return $this->_version;
}
/**
* Sets parent document id
*
* @param string|int $parent Parent document id
* @return Elastica_Document Current object
* @link http://www.elasticsearch.org/guide/reference/mapping/parent-field.html
*/
public function setParent($parent) {
$this->_parent = $parent;
return $this;
}
/**
* Returns the parent document id
*
* @return string|int Parent document id
*/
public function getParent() {
return $this->_parent;
}
/**
* Set operation type
*
* @param string $optype Only accept create
* @return Elastica_Document Current object
*/
public function setOpType($optype) {
$this->_optype = $optype;
return $this;
}
/**
* Get operation type
*/
public function getOpType() {
return $this->_optype;
}
/**
* Set percolate query param
*
* @param string $value percolator filter
* @return Elastica_Document
*/
public function setPercolate($value = '*') {
$this->_percolate = $value;
return $this;
}
/**
* Get percolate parameter
*
* @return string
*/
public function getPercolate() {
return $this->_percolate;
}
/**
* Returns the document as an array
* @return array
*/
public function toArray() {
$index = array('_index' => $this->getIndex(), '_type' => $this->getType(), '_id' => $this->getId());
$version = $this->getVersion();
if (!empty($version)) {
$index['_version'] = $version;
}
$parent = $this->getParent();
if (!empty($parent)) {
$index['_parent'] = $parent;
}
$params[] = $action;
$params[] = $doc->getData();
}
}