Skip to content

Commit 2194dfe

Browse files
committed
Merge pull request #56 from chuangbo/feature/index-settings
Support index settings defination
2 parents 46849f4 + 19ce8e9 commit 2194dfe

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
@@ -139,6 +139,16 @@ public function setMappingProperties(array $mapping = null)
139139
$this->mappingProperties = $mapping;
140140
}
141141

142+
/**
143+
* Get Index Settings
144+
*
145+
* @return array
146+
*/
147+
public function getIndexSettings()
148+
{
149+
return $this->indexSettings;
150+
}
151+
142152
/**
143153
* Is Elasticsearch Document
144154
*
@@ -529,6 +539,11 @@ public static function createIndex($shards = null, $replicas = null)
529539
'index' => $instance->getIndexName(),
530540
);
531541

542+
$settings = $instance->getIndexSettings();
543+
if (!is_null($settings)) {
544+
$index['body']['settings'] = $settings;
545+
}
546+
532547
if (!is_null($shards)) {
533548
$index['body']['settings']['number_of_shards'] = $shards;
534549
}
@@ -537,6 +552,14 @@ public static function createIndex($shards = null, $replicas = null)
537552
$index['body']['settings']['number_of_replicas'] = $replicas;
538553
}
539554

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

0 commit comments

Comments
 (0)