summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Type/Mapping.php
blob: 920093fb69eefc118b0553a5749f369fc23c3c63 (plain)
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
<?php

namespace Elastica\Type;

use Elastica\Exception\InvalidException;
use Elastica\Request;
use Elastica\Type;

/**
 * Elastica Mapping object
 *
 * @category Xodoa
 * @package Elastica
 * @author Nicolas Ruflin <spam@ruflin.com>
 * @link http://www.elasticsearch.org/guide/reference/mapping/
 */
class Mapping
{
    /**
     * Mapping
     *
     * @var array Mapping
     */
    protected $_mapping = array();

    /**
     * Type
     *
     * @var \Elastica\Type Type object
     */
    protected $_type = null;

    /**
     * Construct Mapping
     *
     * @param \Elastica\Type $type       OPTIONAL Type object
     * @param array         $properties OPTIONAL Properties
     */
    public function __construct(Type $type = null, array $properties = array())
    {
        if ($type) {
            $this->setType($type);
        }

        if (!empty($properties)) {
            $this->setProperties($properties);
        }
    }

    /**
     * Sets the mapping type
     * Enter description here ...
     * @param  \Elastica\Type             $type Type object
     * @return \Elastica\Type\Mapping Current object
     */
    public function setType(Type $type)
    {
        $this->_type = $type;

        return $this;
    }

    /**
     * Sets the mapping properties
     *
     * @param  array                     $properties Properties
     * @return \Elastica\Type\Mapping Mapping object
     */
    public function setProperties(array $properties)
    {
        return $this->setParam('properties', $properties);
    }

    /**
     * Gets the mapping properties
     *
     * @return  array                     $properties Properties
     */
    public function getProperties()
    {
        return $this->getParam('properties');
    }

    /**
     * Sets the mapping _meta
     * @param array $meta metadata
     * @return \Elastica\Type\Mapping Mapping object
     * @link http://www.elasticsearch.org/guide/reference/mapping/meta.html
     */
    public function setMeta(array $meta)
    {
        return $this->setParam('_meta', $meta);
    }

    /**
     * Returns mapping type
     *
     * @return \Elastica\Type Type
     */
    public function getType()
    {
        return $this->_type;
    }

    /**
     * Sets source values
     *
     * To disable source, argument is
     * array('enabled' => false)
     *
     * @param  array                     $source Source array
     * @return \Elastica\Type\Mapping Current object
     * @link http://www.elasticsearch.org/guide/reference/mapping/source-field.html
     */
    public function setSource(array $source)
    {
        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 \Elastica\Type\Mapping Current object
     */
    public function disableSource($enabled = false)
    {
        return $this->setSource(array('enabled' => $enabled));
    }

    /**
     * Sets raw parameters
     *
     * Possible options:
     * _uid
     * _id
     * _type
     * _source
     * _all
     * _analyzer
     * _boost
     * _parent
     * _routing
     * _index
     * _size
     * properties
     *
     * @param  string                    $key   Key name
     * @param  mixed                     $value Key value
     * @return \Elastica\Type\Mapping Current object
     */
    public function setParam($key, $value)
    {
        $this->_mapping[$key] = $value;

        return $this;
    }

    /**
     * Get raw parameters
     *
     * @see setParam
     * @param  string                    $key   Key name
     * @return mixed                     $value Key value
     */
    public function getParam($key)
    {
        return isset($this->_mapping[$key])?$this->_mapping[$key]:null;
    }

    /**
     * Sets params for the "_all" field
     *
     * @param array                       $params _all Params (enabled, store, term_vector, analyzer)
     * @return \Elastica\Type\Mapping
     */
    public function setAllField(array $params)
    {
        return $this->setParam('_all', $params);
    }

    /**
     * Enables the "_all" field
     *
     * @param  bool                      $enabled OPTIONAL (default = true)
     * @return \Elastica\Type\Mapping
     */
    public function enableAllField($enabled = true)
    {
        return $this->setAllField(array('enabled' => $enabled));
    }

    /**
     * Set TTL
     *
     * @param  array                     $params TTL Params (enabled, default, ...)
     * @return \Elastica\Type\Mapping
     */
    public function setTtl(array $params)
    {
        return $this->setParam('_ttl', $params);

    }

    /**
     * Enables TTL for all documents in this type
     *
     * @param  bool                      $enabled OPTIONAL (default = true)
     * @return \Elastica\Type\Mapping
     */
    public function enableTtl($enabled = true)
    {
        return $this->setTTL(array('enabled' => $enabled));
    }

    /**
     * Set parent type
     *
     * @param string                     $type Parent type
     * @return \Elastica\Type\Mapping
     */
    public function setParent($type)
    {
        return $this->setParam('_parent', array('type' => $type));
    }

    /**
     * Converts the mapping to an array
     *
     * @throws \Elastica\Exception\InvalidException
     * @return array                               Mapping as array
     */
    public function toArray()
    {
        $type = $this->getType();

        if (empty($type)) {
            throw new InvalidException('Type has to be set');
        }

        return array($type->getName() => $this->_mapping);
    }

    /**
     * Submits the mapping and sends it to the server
     *
     * @return \Elastica\Response Response object
     */
    public function send()
    {
        $path = '_mapping';

        return $this->getType()->request($path, Request::PUT, $this->toArray());
    }

    /**
     * Creates a mapping object
     *
     * @param  array|\Elastica\Type\Mapping     $mapping Mapping object or properties array
     * @return \Elastica\Type\Mapping           Mapping object
     * @throws \Elastica\Exception\InvalidException If invalid type
     */
    public static function create($mapping)
    {
        if (is_array($mapping)) {
            $mappingObject = new Mapping();
            $mappingObject->setProperties($mapping);
        } else {
            $mappingObject = $mapping;
        }

        if (!$mappingObject instanceof Mapping) {
            throw new InvalidException('Invalid object type');
        }

        return $mappingObject;
    }
}