Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a7a40e
commit daeafca
Showing
4 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
Mapping & Indexing | ||
================== | ||
|
||
## Creating index and mapping | ||
|
||
Since it is not always possible to update ElasticSearch mappings incrementally, it is a good idea to create several static methods in your model that deal with index creation and updates. Here is one example of how this can be done. | ||
|
||
```php | ||
Class Book extends yii\elasticsearch\ActiveRecord | ||
{ | ||
// Other class attributes and methods go here | ||
// ... | ||
|
||
/** | ||
* @return array This model's mapping | ||
*/ | ||
public static function mapping() | ||
{ | ||
return [ | ||
static::type() => [ | ||
'properties' => [ | ||
'name' => ['type' => 'string'], | ||
'author_name' => ['type' => 'string'], | ||
'publisher_name' => ['type' => 'string'], | ||
'created_at' => ['type' => 'long'], | ||
'updated_at' => ['type' => 'long'], | ||
'status' => ['type' => 'long'], | ||
] | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Set (update) mappings for this model | ||
*/ | ||
public static function updateMapping() | ||
{ | ||
$db = static::getDb(); | ||
$command = $db->createCommand(); | ||
$command->setMapping(static::index(), static::type(), static::mapping()); | ||
} | ||
|
||
/** | ||
* Create this model's index | ||
*/ | ||
public static function createIndex() | ||
{ | ||
$db = static::getDb(); | ||
$command = $db->createCommand(); | ||
$command->createIndex(static::index(), [ | ||
'settings' => [ /* ... */ ], | ||
'mappings' => static::mapping(), | ||
//'warmers' => [ /* ... */ ], | ||
//'aliases' => [ /* ... */ ], | ||
//'creation_date' => '...' | ||
]); | ||
} | ||
|
||
/** | ||
* Delete this model's index | ||
*/ | ||
public static function deleteIndex() | ||
{ | ||
$db = static::getDb(); | ||
$command = $db->createCommand(); | ||
$command->deleteIndex(static::index(), static::type()); | ||
} | ||
} | ||
``` | ||
|
||
To create the index with proper mappings, call `Book::createIndex()`. If you have changed the mapping in a way that allows mapping update (e.g. created a new property), call `Book::updateMapping()`. | ||
|
||
However, if you have changed a property (e.g. went from `string` to `date`), ElasticSearch will not be able to update the mapping. In this case you need to delete your index (by calling `Book::deleteIndex()`), create it anew with updated mapping (by calling `Book::createIndex()`), and then repopulate it with data. | ||
|
||
## Indexing | ||
TBD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters