| 1 | <?php |
||
| 2 | namespace Triadev\Es\Mapping\Mapping; |
||
| 3 | |||
| 4 | use Illuminate\Support\Fluent; |
||
| 5 | use Triadev\Es\Mapping\Facade\ElasticMapping; |
||
| 6 | use Triadev\Es\Mapping\Mapping\Fluent\FluentBinary; |
||
| 7 | use Triadev\Es\Mapping\Mapping\Fluent\FluentBoolean; |
||
| 8 | use Triadev\Es\Mapping\Mapping\Fluent\FluentByte; |
||
| 9 | use Triadev\Es\Mapping\Mapping\Fluent\FluentCompletion; |
||
| 10 | use Triadev\Es\Mapping\Mapping\Fluent\FluentDate; |
||
| 11 | use Triadev\Es\Mapping\Mapping\Fluent\FluentDateRange; |
||
| 12 | use Triadev\Es\Mapping\Mapping\Fluent\FluentDouble; |
||
| 13 | use Triadev\Es\Mapping\Mapping\Fluent\FluentDoubleRange; |
||
| 14 | use Triadev\Es\Mapping\Mapping\Fluent\FluentFloat; |
||
| 15 | use Triadev\Es\Mapping\Mapping\Fluent\FluentFloatRange; |
||
| 16 | use Triadev\Es\Mapping\Mapping\Fluent\FluentGeoPoint; |
||
| 17 | use Triadev\Es\Mapping\Mapping\Fluent\FluentGeoShape; |
||
| 18 | use Triadev\Es\Mapping\Mapping\Fluent\FluentHalfFloat; |
||
| 19 | use Triadev\Es\Mapping\Mapping\Fluent\FluentInteger; |
||
| 20 | use Triadev\Es\Mapping\Mapping\Fluent\FluentIntegerRange; |
||
| 21 | use Triadev\Es\Mapping\Mapping\Fluent\FluentIp; |
||
| 22 | use Triadev\Es\Mapping\Mapping\Fluent\FluentIpRange; |
||
| 23 | use Triadev\Es\Mapping\Mapping\Fluent\FluentKeyword; |
||
| 24 | use Triadev\Es\Mapping\Mapping\Fluent\FluentLong; |
||
| 25 | use Triadev\Es\Mapping\Mapping\Fluent\FluentLongRange; |
||
| 26 | use Triadev\Es\Mapping\Mapping\Fluent\FluentNested; |
||
| 27 | use Triadev\Es\Mapping\Mapping\Fluent\FluentObject; |
||
| 28 | use Triadev\Es\Mapping\Mapping\Fluent\FluentScaledFloat; |
||
| 29 | use Triadev\Es\Mapping\Mapping\Fluent\FluentShort; |
||
| 30 | use Triadev\Es\Mapping\Mapping\Fluent\FluentText; |
||
| 31 | use Triadev\Es\Mapping\Mapping\Fluent\FluentTokenCount; |
||
| 32 | |||
| 33 | class Blueprint |
||
| 34 | { |
||
| 35 | /** @var Fluent[] */ |
||
| 36 | private $fields = []; |
||
| 37 | |||
| 38 | /** @var array|null */ |
||
| 39 | private $settings; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Blueprint constructor. |
||
| 43 | * @param \Closure|null $callback |
||
| 44 | */ |
||
| 45 | 7 | public function __construct(\Closure $callback = null) |
|
| 46 | { |
||
| 47 | 7 | if (!is_null($callback)) { |
|
| 48 | $callback($this); |
||
| 49 | } |
||
| 50 | 7 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Build a mapping |
||
| 54 | * |
||
| 55 | * @param string $esIndex |
||
| 56 | * @param string $esType |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | 2 | public function build(string $esIndex, string $esType) : array |
|
| 60 | { |
||
| 61 | 2 | if ($this->shouldCreateIndex($esIndex)) { |
|
| 62 | 1 | return $this->createIndex($esIndex, $esType); |
|
| 63 | } |
||
| 64 | |||
| 65 | 1 | return $this->updateIndex($esIndex, $esType); |
|
| 66 | } |
||
| 67 | |||
| 68 | 2 | private function shouldCreateIndex(string $esIndex) : bool |
|
| 69 | { |
||
| 70 | 2 | return !ElasticMapping::getEsClient()->indices()->exists(['index' => $esIndex]); |
|
| 71 | } |
||
| 72 | |||
| 73 | 1 | private function createIndex(string $index, string $type) : array |
|
| 74 | { |
||
| 75 | $body = [ |
||
| 76 | 'mappings' => [ |
||
| 77 | $type => [ |
||
| 78 | 1 | 'properties' => $this->toDsl() |
|
| 79 | ] |
||
| 80 | ] |
||
| 81 | ]; |
||
| 82 | |||
| 83 | 1 | if (is_array($this->settings)) { |
|
| 84 | 1 | $body['settings'] = $this->settings; |
|
| 85 | } |
||
| 86 | |||
| 87 | 1 | return ElasticMapping::getEsClient()->indices()->create([ |
|
| 88 | 1 | 'index' => $index, |
|
| 89 | 1 | 'body' => $body |
|
| 90 | ]); |
||
| 91 | } |
||
| 92 | |||
| 93 | 1 | private function updateIndex(string $index, string $type) : array |
|
| 94 | { |
||
| 95 | 1 | return ElasticMapping::getEsClient()->indices()->putMapping([ |
|
| 96 | 1 | 'index' => $index, |
|
| 97 | 1 | 'type' => $type, |
|
| 98 | 'body' => [ |
||
| 99 | $type => [ |
||
| 100 | '_source' => [ |
||
| 101 | 'enabled' => true |
||
| 102 | ], |
||
| 103 | 1 | 'properties' => $this->toDsl() |
|
| 104 | ] |
||
| 105 | ] |
||
| 106 | ]); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * To dsl |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 4 | public function toDsl() : array |
|
| 115 | { |
||
| 116 | 4 | return (new Compiler())->compileFields($this->fields); |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Get fields |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | 3 | public function getFields() : array |
|
| 125 | { |
||
| 126 | 3 | return $this->fields; |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Text |
||
| 131 | * |
||
| 132 | * @param string $field |
||
| 133 | * @param array $attributes |
||
| 134 | * @return FluentText |
||
| 135 | */ |
||
| 136 | 4 | public function text(string $field, array $attributes = []) : FluentText |
|
| 137 | { |
||
| 138 | 4 | return $this->addField('text', $field, $attributes); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Keyword |
||
| 143 | * |
||
| 144 | * @param string $field |
||
| 145 | * @param array $attributes |
||
| 146 | * @return FluentKeyword |
||
| 147 | */ |
||
| 148 | 5 | public function keyword(string $field, array $attributes = []) : FluentKeyword |
|
| 149 | { |
||
| 150 | 5 | return $this->addField('keyword', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Long |
||
| 155 | * |
||
| 156 | * @param string $field |
||
| 157 | * @param array $attributes |
||
| 158 | * @return FluentLong |
||
| 159 | */ |
||
| 160 | 1 | public function long(string $field, array $attributes = []) : FluentLong |
|
| 161 | { |
||
| 162 | 1 | return $this->addField('long', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Integer |
||
| 167 | * |
||
| 168 | * @param string $field |
||
| 169 | * @param array $attributes |
||
| 170 | * @return FluentInteger |
||
| 171 | */ |
||
| 172 | 3 | public function integer(string $field, array $attributes = []) : FluentInteger |
|
| 173 | { |
||
| 174 | 3 | return $this->addField('integer', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Short |
||
| 179 | * |
||
| 180 | * @param string $field |
||
| 181 | * @param array $attributes |
||
| 182 | * @return FluentShort |
||
| 183 | */ |
||
| 184 | 1 | public function short(string $field, array $attributes = []) : FluentShort |
|
| 185 | { |
||
| 186 | 1 | return $this->addField('short', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Byte |
||
| 191 | * |
||
| 192 | * @param string $field |
||
| 193 | * @param array $attributes |
||
| 194 | * @return FluentByte |
||
| 195 | */ |
||
| 196 | 1 | public function byte(string $field, array $attributes = []) : FluentByte |
|
| 197 | { |
||
| 198 | 1 | return $this->addField('byte', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Double |
||
| 203 | * |
||
| 204 | * @param string $field |
||
| 205 | * @param array $attributes |
||
| 206 | * @return FluentDouble |
||
| 207 | */ |
||
| 208 | 1 | public function double(string $field, array $attributes = []) : FluentDouble |
|
| 209 | { |
||
| 210 | 1 | return $this->addField('double', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Float |
||
| 215 | * |
||
| 216 | * @param string $field |
||
| 217 | * @param array $attributes |
||
| 218 | * @return FluentFloat |
||
| 219 | */ |
||
| 220 | 1 | public function float(string $field, array $attributes = []) : FluentFloat |
|
| 221 | { |
||
| 222 | 1 | return $this->addField('float', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Half float |
||
| 227 | * |
||
| 228 | * @param string $field |
||
| 229 | * @param array $attributes |
||
| 230 | * @return FluentHalfFloat |
||
| 231 | */ |
||
| 232 | 1 | public function halfFloat(string $field, array $attributes = []) : FluentHalfFloat |
|
| 233 | { |
||
| 234 | 1 | return $this->addField('half_float', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Scaled float |
||
| 239 | * |
||
| 240 | * @param string $field |
||
| 241 | * @param array $attributes |
||
| 242 | * @return FluentScaledFloat |
||
| 243 | */ |
||
| 244 | 1 | public function scaledFloat(string $field, array $attributes = []) : FluentScaledFloat |
|
| 245 | { |
||
| 246 | 1 | return $this->addField('scaled_float', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Date |
||
| 251 | * |
||
| 252 | * @param string $field |
||
| 253 | * @param array $attributes |
||
| 254 | * @return FluentDate |
||
| 255 | */ |
||
| 256 | 1 | public function date(string $field, array $attributes = []) : FluentDate |
|
| 257 | { |
||
| 258 | 1 | return $this->addField('date', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Boolean |
||
| 263 | * |
||
| 264 | * @param string $field |
||
| 265 | * @param array $attributes |
||
| 266 | * @return FluentBoolean |
||
| 267 | */ |
||
| 268 | 1 | public function boolean(string $field, array $attributes = []) : FluentBoolean |
|
| 269 | { |
||
| 270 | 1 | return $this->addField('boolean', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Binary |
||
| 275 | * |
||
| 276 | * @param string $field |
||
| 277 | * @param array $attributes |
||
| 278 | * @return FluentBinary |
||
| 279 | */ |
||
| 280 | 1 | public function binary(string $field, array $attributes = []) : FluentBinary |
|
| 281 | { |
||
| 282 | 1 | return $this->addField('binary', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Range |
||
| 287 | * |
||
| 288 | * @param string $field |
||
| 289 | * @param string $type |
||
| 290 | * @param array $attributes |
||
| 291 | * @return FluentIntegerRange|FluentFloatRange|FluentLongRange|FluentDoubleRange|FluentDateRange|FluentIpRange |
||
| 292 | * |
||
| 293 | * @throws \InvalidArgumentException |
||
| 294 | */ |
||
| 295 | 2 | public function range(string $field, string $type, array $attributes = []) |
|
| 296 | { |
||
| 297 | $validTypes = [ |
||
| 298 | 2 | 'integer', |
|
| 299 | 'float', |
||
| 300 | 'long', |
||
| 301 | 'double', |
||
| 302 | 'date', |
||
| 303 | 'ip' |
||
| 304 | ]; |
||
| 305 | |||
| 306 | 2 | if (!in_array($type, $validTypes)) { |
|
| 307 | 1 | throw new \InvalidArgumentException(); |
|
| 308 | } |
||
| 309 | |||
| 310 | 1 | return $this->addField(strtolower($type) . '_range', $field, $attributes); |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Nested |
||
| 315 | * |
||
| 316 | * @param string $field |
||
| 317 | * @param array $attributes |
||
| 318 | * @return FluentNested |
||
| 319 | */ |
||
| 320 | 1 | public function nested(string $field, array $attributes = []) : FluentNested |
|
| 321 | { |
||
| 322 | 1 | return $this->addField('nested', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Object |
||
| 327 | * |
||
| 328 | * @param string $field |
||
| 329 | * @param array $attributes |
||
| 330 | * @return FluentObject |
||
| 331 | */ |
||
| 332 | 1 | public function object(string $field, array $attributes = []) : FluentObject |
|
| 333 | { |
||
| 334 | 1 | return $this->addField('object', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Geo point |
||
| 339 | * |
||
| 340 | * @param string $field |
||
| 341 | * @param array $attributes |
||
| 342 | * @return FluentGeoPoint |
||
| 343 | */ |
||
| 344 | 1 | public function geoPoint(string $field, array $attributes = []) : FluentGeoPoint |
|
| 345 | { |
||
| 346 | 1 | return $this->addField('geo_point', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Geo shape |
||
| 351 | * |
||
| 352 | * @param string $field |
||
| 353 | * @param array $attributes |
||
| 354 | * @return FluentGeoShape |
||
| 355 | */ |
||
| 356 | 1 | public function geoShape(string $field, array $attributes = []) : FluentGeoShape |
|
| 357 | { |
||
| 358 | 1 | return $this->addField('geo_shape', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Ip |
||
| 363 | * |
||
| 364 | * @param string $field |
||
| 365 | * @param array $attributes |
||
| 366 | * @return FluentIp |
||
| 367 | */ |
||
| 368 | 1 | public function ip(string $field, array $attributes = []) : FluentIp |
|
| 369 | { |
||
| 370 | 1 | return $this->addField('ip', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Completion |
||
| 375 | * |
||
| 376 | * @param string $field |
||
| 377 | * @param array $attributes |
||
| 378 | * @return FluentCompletion |
||
| 379 | */ |
||
| 380 | 1 | public function completion(string $field, array $attributes = []) : FluentCompletion |
|
| 381 | { |
||
| 382 | 1 | return $this->addField('completion', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Token count |
||
| 387 | * |
||
| 388 | * @param string $field |
||
| 389 | * @param array $attributes |
||
| 390 | * @return FluentTokenCount |
||
| 391 | */ |
||
| 392 | 1 | public function tokenCount(string $field, array $attributes = []) : FluentTokenCount |
|
| 393 | { |
||
| 394 | 1 | return $this->addField('token_count', $field, $attributes); |
|
|
0 ignored issues
–
show
|
|||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Add field |
||
| 399 | * |
||
| 400 | * @param string $type |
||
| 401 | * @param string $name |
||
| 402 | * @param array $attributes |
||
| 403 | * @return mixed |
||
| 404 | */ |
||
| 405 | 6 | public function addField(string $type, string $name, array $attributes = []) : Fluent |
|
| 406 | { |
||
| 407 | 6 | $fluentClass = sprintf('\Triadev\Es\Mapping\Mapping\Fluent\Fluent%s', ucfirst(camel_case($type))); |
|
| 408 | |||
| 409 | 6 | $this->fields[] = $field = new $fluentClass( |
|
| 410 | 6 | array_merge( |
|
| 411 | 6 | compact( |
|
| 412 | 6 | 'type', |
|
| 413 | 6 | 'name' |
|
| 414 | ), |
||
| 415 | 6 | $attributes |
|
| 416 | ) |
||
| 417 | ); |
||
| 418 | |||
| 419 | 6 | return $field; |
|
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Settings |
||
| 424 | * |
||
| 425 | * @param array $settings |
||
| 426 | */ |
||
| 427 | 2 | public function settings(array $settings) |
|
| 428 | { |
||
| 429 | 2 | $this->settings = $settings; |
|
| 430 | 2 | } |
|
| 431 | } |
||
| 432 |