Skip to content

Commit 19ce8e9

Browse files
committed
Support index settings defination
`ElasticquentTrait@createIndex` now creating with custom index settings and mappings if exists.
1 parent 0a668d3 commit 19ce8e9

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

readme.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,55 @@ If you want a simple way to create indexes, Elasticquent models have a function
138138

139139
Book::createIndex($shards = null, $replicas = null);
140140

141+
For custom analyzer, you can set an `indexSettings` property in your model and define the analyzers from there:
142+
143+
```php
144+
/**
145+
* The elasticsearch settings.
146+
*
147+
* @var array
148+
*/
149+
protected $indexSettings = [
150+
'analysis' => [
151+
'char_filter' => [
152+
'replace' => [
153+
'type' => 'mapping',
154+
'mappings' => [
155+
'&=> and '
156+
],
157+
],
158+
],
159+
'filter' => [
160+
'word_delimiter' => [
161+
'type' => 'word_delimiter',
162+
'split_on_numerics' => false,
163+
'split_on_case_change' => true,
164+
'generate_word_parts' => true,
165+
'generate_number_parts' => true,
166+
'catenate_all' => true,
167+
'preserve_original' => true,
168+
'catenate_numbers' => true,
169+
]
170+
],
171+
'analyzer' => [
172+
'default' => [
173+
'type' => 'custom',
174+
'char_filter' => [
175+
'html_strip',
176+
'replace',
177+
],
178+
'tokenizer' => 'whitespace',
179+
'filter' => [
180+
'lowercase',
181+
'word_delimiter',
182+
],
183+
],
184+
],
185+
],
186+
];
187+
188+
```
189+
141190
For mapping, you can set a `mappingProperties` property in your model and use some mapping functions from there:
142191

143192
```php

src/ElasticquentTrait.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ public function setMappingProperties(array $mapping = null)
137137
$this->mappingProperties = $mapping;
138138
}
139139

140+
/**
141+
* Get Index Settings
142+
*
143+
* @return array
144+
*/
145+
public function getIndexSettings()
146+
{
147+
return $this->indexSettings;
148+
}
149+
140150
/**
141151
* Is Elasticsearch Document
142152
*
@@ -527,6 +537,11 @@ public static function createIndex($shards = null, $replicas = null)
527537
'index' => $instance->getIndexName(),
528538
);
529539

540+
$settings = $instance->getIndexSettings();
541+
if (!is_null($settings)) {
542+
$index['body']['settings'] = $settings;
543+
}
544+
530545
if (!is_null($shards)) {
531546
$index['body']['settings']['number_of_shards'] = $shards;
532547
}
@@ -535,6 +550,14 @@ public static function createIndex($shards = null, $replicas = null)
535550
$index['body']['settings']['number_of_replicas'] = $replicas;
536551
}
537552

553+
$mappingProperties = $instance->getMappingProperties();
554+
if (!is_null($mappingProperties)) {
555+
$index['body']['mappings'][$instance->getTypeName()] = [
556+
'_source' => array('enabled' => true),
557+
'properties' => $mappingProperties,
558+
];
559+
}
560+
538561
return $client->indices()->create($index);
539562
}
540563

0 commit comments

Comments
 (0)