Integrations / Platforms / WordPress / Setting Index Configuration
Feb. 19, 2019

Setting Index Configuration

If you want to manage your settings in your code, you can create a custom command, similar to copy_config but which gets settings, synonyms, and rules from WordPress.

This new command named ‘set_config’ will take a few parameters: the canonical index name and what index configuration to send (settings, rules, and synonyms). This first version doesn’t handle replicas, we’ll see in the next example how to handle them properly.

Unlike the copy_config command, this command takes a canonical index name, that will be modified via the algolia_index_name filter. We want each developer and each environment to be able to run the command to push settings for the posts’ indices and let WordPress choose the right index.

In our plugin Algolia_Command class, add the set_config method.

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
29
30
31
32
33
34
35
36
37
38
39
public function set_config( $args, $assoc_args ) {
    global $algolia;

    $canonicalIndexName = $assoc_args['index'];
    if (!$canonicalIndexName) {
        throw new InvalidArgumentException('--index argument is required');
    }

    $index = $algolia->initIndex(
        apply_filters('algolia_index_name', $canonicalIndexName)
    );

    if ($assoc_args['settings']) {
        $settings = (array) apply_filters('get_'.$canonicalIndexName.'_settings', []);
        if ($settings) {
            $index->setSettings($settings);
            WP_CLI::success('Push settings to '.$index->getIndexName());
        }

    }

    if ($assoc_args['synonyms']) {
        $synonyms = (array) apply_filters('get_'.$canonicalIndexName.'_synonyms', []);
        if ($synonyms) {
            $index->replaceAllSynonyms($synonyms);
            WP_CLI::success('Push synonyms to '.$index->getIndexName());
        }

    }

    if ($assoc_args['rules']) {
        $rules = (array) apply_filters('get_'.$canonicalIndexName.'$rules', []);
        if ($rules) {
            $index->replaceAllRules($rules);
            WP_CLI::success('Push query rules to '.$index->getIndexName());
        }

    }
}

This method will call some new filters to figure out settings, synonyms, and query rules. Because they depend on your site, these filters should be added in your theme’s functions.php.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function algolia_get_post_settings($defaultSettings) {
    return [
        'hitsPerPage' => 18,
        'searchableAttributes' => ['title', 'content', 'author.name'],
    ];
}
add_filter('get_post_settings', 'algolia_get_post_settings');

function algolia_get_post_synonyms($defaultSynonyms) {
    return json_decode(
        file_get_contents(get_template_directory() . '/my-synonyms.json'),
        true
    );
}
add_filter('get_post_synonyms', 'algolia_get_post_synonyms');

Command wp algolia set_config

Handling replicas

Replicas are very handy if you want to sort your results in different orders. It’s recommended to read more about replicas before going further.

We’ll modify the previous command to add a --forward-to-replicas option.

The one thing that can make replicas tricky is that their names live inside the index settings. If you are suffixing your index names per environment, you need to make sure the same suffix is applied to the replica names in the settings.

After the index configuration is set for all primary indices, with the fowardToReplicas options, we’ll loop on all replica names and see if a method is defined to set special settings or other configurations.

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public function set_config( $args, $assoc_args ) {
    global $algolia;

    $canonicalIndexName = $assoc_args['index'];
    if (!$canonicalIndexName) {
        throw new InvalidArgumentException('--index argument is required');
    }

    $forward = isset($assoc_args['forward-to-replicas']) ? $assoc_args['forward-to-replicas'] : false;

    $index = $algolia->initIndex(
        apply_filters('algolia_index_name', $canonicalIndexName)
    );

    $replicas = [];
    if ($assoc_args['settings']) {
        $settings = (array) apply_filters('get_'.$canonicalIndexName.'_settings', []);

        if (isset($settings['replicas'])) {
            $replicas = $settings['replicas'];

            $settings['replicas'] = array_map(function ($replicaName) {
                return apply_filters('algolia_index_name', $replicaName);
            }, $settings['replicas']);
        }

        if ($settings) {
            $index->setSettings($settings, ['forwardToReplicas' => $forward]);
        }

        WP_CLI::success('Push settings to '.$index->getIndexName());
    }

    if ($assoc_args['synonyms']) {
        $synonyms = (array) apply_filters('get_'.$canonicalIndexName.'_synonyms', []);
        if ($synonyms) {
            $index->replaceAllSynonyms($synonyms, ['forwardToReplicas' => $forward]);
        }

        WP_CLI::success('Push synonyms to '.$index->getIndexName());
    }

    if ($assoc_args['rules']) {
        $rules = (array) apply_filters('get_'.$canonicalIndexName.'$rules', []);
        if ($rules) {
            $index->replaceAllRules($rules, ['forwardToReplicas' => $forward]);
        }

        WP_CLI::success('Push query rules to '.$index->getIndexName());
    }

    foreach ($replicas as $replicaName) {
        $this->set_config([], ['index' => $replicaName] + $assoc_args);
    }
}

The following shows how to declare replicas in the index settings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function algolia_get_post_settings($defaultSettings) {
    return [
        'hitsPerPage' => 18,
        'searchableAttributes' => ['title', 'content', 'author.name'],
        'replicas' => [
            'post_replica'
        ],
    ];
}
add_filter('get_post_settings', 'algolia_get_post_settings');

function algolia_get_post_replica_settings($defaultSettings) {
    return [
        'hitsPerPage' => 100,
    ];
}
add_filter('get_post_replica_settings', 'algolia_get_post_replica_settings');

Command wp algolia set_config_replicas

Did you find this page helpful?