forked from ruflin/Elastica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mapping.php
197 lines (178 loc) · 4.33 KB
/
Mapping.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
<?php
namespace Elastica;
use Elastica\Exception\InvalidException;
use Elasticsearch\Endpoints\Indices\Mapping\Put;
use Elasticsearch\Endpoints\Indices\PutMapping;
/**
* Elastica Mapping object.
*
* @author Nicolas Ruflin <[email protected]>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
*/
class Mapping
{
/**
* Mapping.
*
* @var array Mapping
*/
protected $_mapping = [];
/**
* Construct Mapping.
*
* @param array $properties OPTIONAL Properties
*/
public function __construct(array $properties = [])
{
if ($properties) {
$this->setProperties($properties);
}
}
/**
* Sets the mapping properties.
*
* @param array $properties Properties
*
* @return $this
*/
public function setProperties(array $properties): Mapping
{
return $this->setParam('properties', $properties);
}
/**
* Gets the mapping properties.
*
* @return array $properties Properties
*/
public function getProperties(): array
{
return $this->getParam('properties');
}
/**
* Sets the mapping _meta.
*
* @param array $meta metadata
*
* @return $this
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-meta.html
*/
public function setMeta(array $meta): Mapping
{
return $this->setParam('_meta', $meta);
}
/**
* Sets source values.
*
* To disable source, argument is
* array('enabled' => false)
*
* @param array $source Source array
*
* @return $this
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-source-field.html
*/
public function setSource(array $source): Mapping
{
return $this->setParam('_source', $source);
}
/**
* Disables the source in the index.
*
* Param can be set to true to enable again
*
* @param bool $enabled OPTIONAL (default = false)
*
* @return $this
*/
public function disableSource(bool $enabled = false): Mapping
{
return $this->setSource(['enabled' => $enabled]);
}
/**
* Sets raw parameters.
*
* Possible options:
* _uid
* _id
* _type
* _source
* _analyzer
* _boost
* _routing
* _index
* _size
* properties
*
* @param string $key Key name
* @param mixed $value Key value
*
* @return $this
*/
public function setParam(string $key, $value): Mapping
{
$this->_mapping[$key] = $value;
return $this;
}
/**
* Get raw parameters.
*
* @see setParam
*
* @param string $key Key name
*
* @return mixed $value Key value
*/
public function getParam(string $key)
{
return $this->_mapping[$key] ?? null;
}
/**
* Converts the mapping to an array.
*
* @throws InvalidException
*
* @return array Mapping as array
*/
public function toArray(): array
{
return $this->_mapping;
}
/**
* Submits the mapping and sends it to the server.
*
* @param Index $index the index to send the mappings to
* @param array $query Query string parameters to send with mapping
*
* @return Response Response object
*/
public function send(Index $index, array $query = []): Response
{
// TODO: Use only PutMapping when dropping support for elasticsearch/elasticsearch 7.x
$endpoint = \class_exists(PutMapping::class) ? new PutMapping() : new Put();
$endpoint->setBody($this->toArray());
$endpoint->setParams($query);
return $index->requestEndpoint($endpoint);
}
/**
* Creates a mapping object.
*
* @param array|Mapping $mapping Mapping object or properties array
*
* @throws InvalidException If invalid type
*/
public static function create($mapping): Mapping
{
if (\is_array($mapping)) {
$mappingObject = new static();
$mappingObject->setProperties($mapping);
return $mappingObject;
}
if ($mapping instanceof self) {
return $mapping;
}
throw new InvalidException('Invalid object type');
}
}