Skip to content

Synonyms

Michael Beckwith edited this page Dec 15, 2022 · 4 revisions

How to leverage the Algolia synonyms feature.

Introduction

The plugin supports the synonym feature of Algolia.

To learn how synonyms work, please read Algolia's official synonyms documentation at https://www.algolia.com/doc/guides/managing-results/optimize-search-results/adding-synonyms/.

Push Your Own Synonyms

You can push your own synonyms using filters that the plugin provides.

Filter Name Parameters
algolia_posts_index_synonyms array $synonyms, string $post_type
algolia_posts_{$post_type}_index_synonyms array $synonyms
algolia_searchable_posts_index_synonyms array $synonyms
algolia_terms_index_synonyms array $synonyms, string $taxonomy
algolia_terms_{$taxonomy}_index_synonyms array $synonyms
algolia_users_index_synonyms array $synonyms

Example:

<?php
/*
Plugin Name: Algolia Greeting Synonyms Example
*/

function custom_posts_page_index_synonyms( array $synonyms ) {
    $synonyms[] = array(
        'objectID' => 'greeting',
        'type'     => 'synonym',
        'synonyms' => array( 'hello', 'hi', 'hey' )
    );

    $synonyms[] = array(
        'objectID' => 'tablet',
        'type'     => 'oneWaySynonym',
        'input'    => 'tablet',
        'synonyms' => array( 'ipad', 'galaxy note' )
    );

    $synonyms[] = array(
        'objectID'      => 'street',
        'type'          => 'placeholder',
        'placeholder'   => '<Street>',
        'replacements' 	=> array( 'street', 'st' )
    );

    return $synonyms;
}
add_filter( 'algolia_posts_page_index_synonyms', 'custom_posts_page_index_synonyms' );

Synonyms will be reset by the plugin each time you re-index your content. You should NOT configure your synonyms via the Algolia Dashboard. Instead create a plugin as explained in the documentation page about extending this plugin.