Writing a Solr Migration
In this tutorial, we will make a change to the Registry Index, specifically aim at the Portal collection and will add a new field
Create a new file at /applications/registry/maintenance/migrations/registry_index
, assuming the latest migration available here is 019, we will create a new file called 020_SampleMigration.php
<?php
/**
* Class: highlightFields
*
* @author: Minh Duc Nguyen <minh.nguyen@ands.org.au>
*/
namespace ANDS;
class SampleMigration extends GenericSolrMigration
{
/**
* highlightFields constructor.
*/
public function __construct()
{
parent::__construct();
$this->setFields([
[
'name' => 'new_field',
'type' => 'text_en_splitting',
'stored' => true,
'indexed' => true,
'multiValued' => true
],
]);
}
}
The setField functionality will ensure that the field define in this list will get into the index. If the field already exists, it will do a replace, if the field doesn't exist, it will create it.
copyField
use the setCopyFields functionality like so
$this->setCopyFields([
['source' => '*', 'dest' => 'fulltext'],
['source' => 'group', 'dest' => ['group_sort', 'group_search']],
['source' => 'type', 'dest' => ['type_sort', 'type_search']]
]);
Custom up() and down()
If there's any other specific actions (eg. defining a new field type) that needs to run after the default up()
and down()
. It can be override like so
/**
* Replacing the default field type of location_rpt as well
* @Note requires jts-1.8.0.jar installed in SOLR lib directory
* @Override
* @return mixed
*/
function up()
{
$result = array();
$result[] = parent::up();
$result[] = $this->ci->solr->schema([
'replace-field-type' => [
'name' => 'location_rpt',
'class' => 'solr.SpatialRecursivePrefixTreeFieldType',
'spatialContextFactory' => 'com.spatial4j.core.context.jts.JtsSpatialContextFactory',
'geo' => true,
'distErrPct' => '0.025',
'maxDistErr' => '0.000009',
'units' => 'degrees'
]
]);
return $result;
}
/**
* Return the field back to it's original state
* @Override
* @return mixed
*/
function down()
{
$result = array();
$result[] = parent::up();
$result[] = $this->ci->solr->schema([
'replace-field-type' => [
'name' => 'location_rpt',
'class' => 'solr.SpatialRecursivePrefixTreeFieldType',
'geo' => true,
'distErrPct' => '0.025',
'maxDistErr' => '0.001'
]
]);
return $result;
}