Skip to content

Commit 0a212b5

Browse files
committed
Move mapping manipulation to a seperate trait.
1 parent 2e92b8f commit 0a212b5

File tree

2 files changed

+94
-89
lines changed

2 files changed

+94
-89
lines changed

src/ElasticquentMapping.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
namespace Elasticquent;
3+
4+
trait ElasticquentMapping
5+
{
6+
/**
7+
* Mapping Exists
8+
*
9+
* @return bool
10+
*/
11+
public static function mappingExists()
12+
{
13+
$instance = new static;
14+
15+
$mapping = $instance->getMapping();
16+
17+
return (empty($mapping)) ? false : true;
18+
}
19+
20+
/**
21+
* Get Mapping
22+
*
23+
* @return void
24+
*/
25+
public static function getMapping()
26+
{
27+
$instance = new static;
28+
29+
$params = $instance->getBasicEsParams();
30+
31+
return $instance->getElasticSearchClient()->indices()->getMapping($params);
32+
}
33+
34+
/**
35+
* Put Mapping.
36+
*
37+
* @param bool $ignoreConflicts
38+
*
39+
* @return array
40+
*/
41+
public static function putMapping($ignoreConflicts = false)
42+
{
43+
$instance = new static;
44+
45+
$mapping = $instance->getBasicEsParams();
46+
47+
$params = array(
48+
'_source' => array('enabled' => true),
49+
'properties' => $instance->getMappingProperties(),
50+
);
51+
52+
$mapping['body'][$instance->getTypeName()] = $params;
53+
54+
return $instance->getElasticSearchClient()->indices()->putMapping($mapping);
55+
}
56+
57+
/**
58+
* Delete Mapping
59+
*
60+
* @return array
61+
*/
62+
public static function deleteMapping()
63+
{
64+
$instance = new static;
65+
66+
$params = $instance->getBasicEsParams();
67+
68+
return $instance->getElasticSearchClient()->indices()->deleteMapping($params);
69+
}
70+
71+
/**
72+
* Rebuild Mapping
73+
*
74+
* This will delete and then re-add
75+
* the mapping for this model.
76+
*
77+
* @return array
78+
*/
79+
public static function rebuildMapping()
80+
{
81+
$instance = new static;
82+
83+
// If the mapping exists, let's delete it.
84+
if ($instance->mappingExists()) {
85+
$instance->deleteMapping();
86+
}
87+
88+
// Don't need ignore conflicts because if we
89+
// just removed the mapping there shouldn't
90+
// be any conflicts.
91+
return $instance->putMapping();
92+
}
93+
}

src/ElasticquentTrait.php

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
trait ElasticquentTrait
1414
{
15-
use ElasticquentClientTrait, EloquentMethods;
15+
use ElasticquentClientTrait, ElasticquentMapping, EloquentMethods;
1616

1717
/**
1818
* Uses Timestamps In Index
@@ -430,94 +430,6 @@ private function buildFieldsParameter($getSourceIfPossible, $getTimestampIfPossi
430430
return $fieldsParam;
431431
}
432432

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-
521433
/**
522434
* Create Index
523435
*

0 commit comments

Comments
 (0)