Skip to content

Commit

Permalink
Ability to set mapping definition for extra fields as a callback func…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
Alban Jubert committed Mar 14, 2019
1 parent 87a7123 commit 26c5a7c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .craftplugin
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"pluginName":"Elasticsearch","pluginDescription":"Bring the power of Elasticsearch to you Craft 3 CMS project","pluginVersion":"1.1.0-beta.4,1.1.0-beta.3,1.1.0-beta.2,1.1.0-beta.1,1.0.0","pluginAuthorName":"La Haute Société","pluginVendorName":"la-haute-societe","pluginAuthorUrl":"https://www.lahautesociete.com","pluginAuthorGithub":"juban","codeComments":"codeComments","pluginComponents":["utilities","jobs","tasks","consolecommands","controllers","cpsection","models","records","services","settings","variables"],"consolecommandName":"Elasticsearch","controllerName":"Elasticsearch","cpsectionName":"Elasticsearch","modelName":"Elasticsearch","recordName":"Elasticsearch","serviceName":"Elasticsearch","utilityName":"elasticsearch","apiVersion":"api_version_3_0"}
{"pluginName":"Elasticsearch","pluginDescription":"Bring the power of Elasticsearch to you Craft 3 CMS project","pluginVersion":"1.0.0","pluginAuthorName":"La Haute Société","pluginVendorName":"la-haute-societe","pluginAuthorUrl":"https://www.lahautesociete.com","pluginAuthorGithub":"juban","codeComments":"codeComments","pluginComponents":["utilities","jobs","tasks","consolecommands","controllers","cpsection","models","records","services","settings","variables"],"consolecommandName":"Elasticsearch","controllerName":"Elasticsearch","cpsectionName":"Elasticsearch","modelName":"Elasticsearch","recordName":"Elasticsearch","serviceName":"Elasticsearch","utilityName":"elasticsearch","apiVersion":"api_version_3_0"}
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ Each entry consists of the following attributes:
- `highlights`: array of highlighted content matching the query terms
- `rawResult`: the ElasticsearchRecord raw result object

>Note: To add additional attributes, see [Index additional data](#indexing-of-additional-data) for more details.
> Note:
> * To add additional attributes, see [Index additional data](#indexing-of-additional-data) for more details.
> * To customize the Elasticsearch query, see [More complex way to get even more control](#more-complex-way-to-get-even-more-control)
## Auto indexing

Expand Down Expand Up @@ -308,14 +310,19 @@ Each field should be declared by using associative array with keys representing
to configure the field behavior:

* `mapping` (optional): an associative array providing the elasticsearch mapping definition for the field.
> Note: If not provided, the mapping will default to:
> ```php
> [
> 'type' => 'text',
> 'store' => true,
> 'analyzer' => 'english' // site analyzer based on Craft site language
> ];
> ```
For more complex mapping, you can also use a callback (`function (\lhs\elasticsearch\records\ElasticsearchRecord $esRecord)`) to return the associative array.
For example:
```php
...
'mapping' => function (\lhs\elasticsearch\records\ElasticsearchRecord $esRecord) {
return [
'type' => 'text',
'store' => true,
'analyzer' => $esRecord::siteAnalyzer()
];
}
...
```
* `highlighter` (optional): an object defining the elasticsearch highlighter behavior for the field.
To know more about that configuration, refer to the documentation [here](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-request-highlighting.html).
* `value`: either a string or a callable function taking one argument of `craft\base\Element` type and returning the value of the field.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "la-haute-societe/craft-elasticsearch",
"description": "Bring the power of Elasticsearch to your Craft CMS projects.",
"type": "craft-plugin",
"version": "1.1.0-beta.6",
"version": "1.1.0-beta.7",
"keywords": [
"craft",
"cms",
Expand Down Expand Up @@ -36,7 +36,7 @@
"extra": {
"name": "Elasticsearch",
"handle": "elasticsearch",
"schemaVersion": "1.1.0-beta.6",
"schemaVersion": "1.1.0-beta.7",
"changelogUrl": "https://raw.githubusercontent.com/la-haute-societe/craft-elasticsearch/master/CHANGELOG.md",
"components": {
"elasticsearch": "lhs\\elasticsearch\\services\\Elasticsearch"
Expand Down
14 changes: 7 additions & 7 deletions src/records/ElasticsearchRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,13 @@ public function createESIndex()
$extraFields = ElasticsearchPlugin::getInstance()->getSettings()->extraFields;
if (!empty($extraFields)) {
foreach ($extraFields as $fieldName => $fieldParams) {
$fieldMapping = ArrayHelper::getValue($fieldParams, 'mapping', []);
$defaultMapping = [
'type' => 'text',
'store' => true,
'analyzer' => self::siteAnalyzer()
];
$mapping[static::type()]['properties'][$fieldName] = ArrayHelper::merge($defaultMapping, $fieldMapping);
$fieldMapping = ArrayHelper::getValue($fieldParams, 'mapping');
if ($fieldMapping) {
if (is_callable($fieldMapping)) {
$fieldMapping = $fieldMapping($this);
}
$mapping[static::type()]['properties'][$fieldName] = $fieldMapping;
}
}
}
// Set the schema
Expand Down

0 comments on commit 26c5a7c

Please sign in to comment.