|
12 | 12 | */ |
13 | 13 | trait ElasticquentTrait |
14 | 14 | { |
15 | | - use ElasticquentClientTrait, ElasticquentMapping, EloquentMethods; |
| 15 | + use ElasticquentClientTrait, EloquentMethods; |
16 | 16 |
|
17 | 17 | /** |
18 | 18 | * Uses Timestamps In Index |
@@ -430,6 +430,94 @@ private function buildFieldsParameter($getSourceIfPossible, $getTimestampIfPossi |
430 | 430 | return $fieldsParam; |
431 | 431 | } |
432 | 432 |
|
| 433 | + /** |
| 434 | + * Mapping Exists |
| 435 | + * |
| 436 | + * @return bool |
| 437 | + */ |
| 438 | + public static function mappingExists() |
| 439 | + { |
| 440 | + $instance = new static; |
| 441 | + |
| 442 | + $mapping = $instance->getMapping(); |
| 443 | + |
| 444 | + return (empty($mapping)) ? false : true; |
| 445 | + } |
| 446 | + |
| 447 | + /** |
| 448 | + * Get Mapping |
| 449 | + * |
| 450 | + * @return void |
| 451 | + */ |
| 452 | + public static function getMapping() |
| 453 | + { |
| 454 | + $instance = new static; |
| 455 | + |
| 456 | + $params = $instance->getBasicEsParams(); |
| 457 | + |
| 458 | + return $instance->getElasticSearchClient()->indices()->getMapping($params); |
| 459 | + } |
| 460 | + |
| 461 | + /** |
| 462 | + * Put Mapping. |
| 463 | + * |
| 464 | + * @param bool $ignoreConflicts |
| 465 | + * |
| 466 | + * @return array |
| 467 | + */ |
| 468 | + public static function putMapping($ignoreConflicts = false) |
| 469 | + { |
| 470 | + $instance = new static; |
| 471 | + |
| 472 | + $mapping = $instance->getBasicEsParams(); |
| 473 | + |
| 474 | + $params = array( |
| 475 | + '_source' => array('enabled' => true), |
| 476 | + 'properties' => $instance->getMappingProperties(), |
| 477 | + ); |
| 478 | + |
| 479 | + $mapping['body'][$instance->getTypeName()] = $params; |
| 480 | + |
| 481 | + return $instance->getElasticSearchClient()->indices()->putMapping($mapping); |
| 482 | + } |
| 483 | + |
| 484 | + /** |
| 485 | + * Delete Mapping |
| 486 | + * |
| 487 | + * @return array |
| 488 | + */ |
| 489 | + public static function deleteMapping() |
| 490 | + { |
| 491 | + $instance = new static; |
| 492 | + |
| 493 | + $params = $instance->getBasicEsParams(); |
| 494 | + |
| 495 | + return $instance->getElasticSearchClient()->indices()->deleteMapping($params); |
| 496 | + } |
| 497 | + |
| 498 | + /** |
| 499 | + * Rebuild Mapping |
| 500 | + * |
| 501 | + * This will delete and then re-add |
| 502 | + * the mapping for this model. |
| 503 | + * |
| 504 | + * @return array |
| 505 | + */ |
| 506 | + public static function rebuildMapping() |
| 507 | + { |
| 508 | + $instance = new static; |
| 509 | + |
| 510 | + // If the mapping exists, let's delete it. |
| 511 | + if ($instance->mappingExists()) { |
| 512 | + $instance->deleteMapping(); |
| 513 | + } |
| 514 | + |
| 515 | + // Don't need ignore conflicts because if we |
| 516 | + // just removed the mapping there shouldn't |
| 517 | + // be any conflicts. |
| 518 | + return $instance->putMapping(); |
| 519 | + } |
| 520 | + |
433 | 521 | /** |
434 | 522 | * Create Index |
435 | 523 | * |
|
0 commit comments