Integrations / Platforms / WordPress / Setting Index Configuration

Setting Index Configuration

If you want to manage your settings in your code instead of the Algolia dashboard, you can create a custom command which retrieves settings, synonyms, and Rules from WordPress.

Creating the configuration command

This new command takes a few parameters: the canonical index name, and what index configuration to send. We’re going to use the canonical index name and modify it with the algolia_index_name filter so we can let WordPress choose the right index.

In your Algolia_Command class, you can 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 rules to '.$index->getIndexName());
        }

    }
}

This method leverages some new filters to figure out settings, synonyms, and Rules. Because they depend on your website, you need to add them to your theme’s functions.php file.

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');

Handling replicas

Replicas are useful to let end users sort results in different orders. For example, if you have posts with comments, you could let end users sort results by ascending or descending number of comments.

To forward primary settings to replica indices, you can modify the previous command to add a --forward-to-replicas flag.

The names of your replicas live inside the index settings. If you’re suffixing your index names per environment, you need to apply the same suffix to the replica names in the settings.

After setting the index configuration for all primary indices with the forwardToReplicas option, you can loop through all replica names and check whether there’s a method to set specific settings.

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
56
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 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');

Did you find this page helpful?