Integrations / Platforms / WordPress / Copying Configuration to Prod
Feb. 19, 2019

Copying Configuration to Prod

If you manage your relevance in the dashboard, you may have a staging environment where you tweak settings, add new rules, synonyms, and a production index.

When you’re ready to go live, you may want to copy your index configuration (settings, synonyms, and query rules) to your production index.

An good way to achieve that is to create a custom command and call it in your deployment process.

You can also copy settings manually from the dashboard.

Create copy_config command

Inside algolia-custom-integration/wp-cli.php add the following method to the Algolia_Command class. This command is given as an example, feel free to modify it to fit your need.

In this case, we’re not passing the index names through the algolia_index_name filter, because the point of the command is to copy across indices. We don’t want the current environment to alter the name (like adding _prod) at the end of both indices.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public function copy_config( $args, $assoc_args ) {
    global $algolia;

    $srcIndexName = $assoc_args['from'];
    $destIndexName = $assoc_args['to'];

    if (!$srcIndexName || !$destIndexName) {
        throw new InvalidArgumentException('--from and --to arguments are required');
    }

    $scope = [];
    if (isset($assoc_args['settings']) && $assoc_args['settings']) {
        $scope[] = 'settings';
    }
    if (isset($assoc_args['synonyms']) && $assoc_args['synonyms']) {
        $scope[] = 'synonyms';
    }
    if (isset($assoc_args['rules']) && $assoc_args['rules']) {
        $scope[] = 'rules';
    }

    if (!empty($scope)) {
        $algolia->copyIndex($srcIndexName, $destIndexName, ['scope' => $scope]);
        WP_CLI::success('Copied '.implode(', ', $scope)." from $srcIndexName to $destIndexName");
    } else {
        WP_CLI::warning('Nothing to copy, use --settings, --synonyms or --rules.');
    }
}

This command lets you choose what to copy by adding or removing the --settings, --rules, --synonyms flags.

1
wp algolia copy_config --from=staging_posts --to=prod_posts --settings --rules --synonyms

Command wp algolia copy_config

Did you find this page helpful?