Skip to content

Index Settings

Michael Beckwith edited this page Feb 10, 2023 · 5 revisions

Understand the indices default settings and how to customize the ranking.

Introduction

Algolia allows you to configure your indices to improve the relevancy of your search results.

Each index comes with its own customizable settings. A complete list of the settings parameters can be found in the Algolia documentation at https://www.algolia.com/doc/api-client/getting-started/install/php/?client=php#index-settings.

This plugin automatically configures each index with some default settings. You can customize the settings by using filters.


Settings Anatomy

Before telling you about the default settings per content type and how to customize them, let's go through an overview of a few parameters.

searchableAttributes: Refers to the attributes that the search will be based on. By default, this will list all of the text attributes that the user would like to search. Unless the attribute name is inside of an unordered() function call, word match position matters. This means that if a word is found earlier in the indexed attribute, it will be considered a better match.

customRanking Internally, the Algolia Search engine uses a tie breaking algorithm, which means that at every step of the search process, it will keep only the most relevant results by applying every available ranking algorithm. There are a lot of ranking strategies provided out of the box, specifically typo,geo,words,filters,proximity,attribute,exact,custom. If the search engine has more results to offer than expected, Algolia considers it best practice to provide the engine with some custom metrics to make the best decision. This plugin provides defaults so that your search results are as accurate as possible. However, these defaults can be customized.

attributesForDistinct For the post content type, multiple records for the same item are stored in Algolia. The reason for this is explained on the index schema page. Even though multiple records are stored in Algolia for a unique post, only one post is displayed in the results. For this, the Algolia attributeForDistinct & distinct features are used. These are similar to the SQL DISTINCT feature. By using this feature, the search engine will only return the most relevant record for a given query.

attributesForFaceting Algolia allows you to prepare your records for faceting. Faceting is the feature that allows you to list all posts of a category. Out of the box usage of faceting is not included in the plugin, however some default values for the attributesForFaceting are included.


To further optimize the search relevancy for your needs, please see Algolia's relevancy documentation at https://www.algolia.com/doc/guides/managing-results/relevance-overview/.


Searchable Posts Index Settings

Default searchable posts settings:

<?php
$settings = array(
    'searchableAttributes' => array(
        'unordered(post_title)',
        'unordered(content)',
    ),
    'customRanking' => array(
        'desc(is_sticky)',
        'desc(post_date)',
    ),
    'attributeForDistinct'  => 'post_id',
    'distinct'              => true,
    'attributesForFaceting' => array(
        'taxonomy_post_tag',
        'taxonomy_category',
        'post_author.display_name',
        'post_type_label',
    ),
    'attributesToSnippet' => array(
        'post_title:30',
        'content:30',
    ),
    'snippetEllipsisText' => '',
);

Posts Index Settings

Default posts settings:

<?php
$settings = array(
    'searchableAttributes' => array(
        'unordered(post_title)',
        'unordered(content)',
    ),
    'customRanking' => array(
        'desc(is_sticky)',
        'desc(post_date)',
    ),
    'attributeForDistinct'  => 'post_id',
    'distinct'              => true,
    'attributesForFaceting' => array(
        'taxonomy_post_tag',
        'taxonomy_category',
        'post_author.display_name',
    ),
    'attributesToSnippet' => array(
        'post_title:30',
        'content:30',
    ),
    'snippetEllipsisText' => '',
);

Terms Index Settings

Default terms settings:

<?php
$settings = array(
    'searchableAttributes' => array(
        'unordered(name)',
        'unordered(description)',
    ),
    'customRanking' => array(
        'desc(posts_count)',
    ),
);

Users Index Settings

Default users settings:

<?php
$settings = array(
    'searchableAttributes' => array(
        'unordered(display_name)',
    ),
    'customRanking' => array(
        'desc(posts_count)',
    ),
);

Custom Index Settings

You can customize the settings pushed to Algolia for every content type. To do so simply use the available filters:

Filter Name Params
algolia_posts_index_settings $settings, the posts index settings. $post_type, the post type, like "page" or "post".
algolia_posts_{$post_type}_index_settings $settings, the posts index settings.
algolia_terms_index_settings $settings, the terms index settings. $taxonomy, the taxonomy like "post_tag" or "category".
algolia_terms_{$taxonomy}_index_settings $settings, the terms index settings.
algolia_users_index_settings $settings, the users index settings.

Custom Index Settings Example

Let's say you have a custom metric named visit_count and you would like to add a custom ranking rule on that on that metric:

<?php

function custom_posts_index_settings( array $settings ) {
    $custom_ranking = $settings['customRanking'];
    array_unshift( $custom_ranking, 'desc(visit_count)' );
    $settings['customRanking'] = $custom_ranking;

    return $settings;
}
add_filter( 'algolia_posts_index_settings', 'custom_posts_index_settings' );

Every post type has its own index in Algolia. Custom settings per post type can be determined:

<?php

// Customize settings for pages only.
function custom_pages_index_settings( array $settings ) {
    /* Your adjustments */

    return $settings;
}
add_filter( 'algolia_posts_page_index_settings', 'custom_pages_index_settings' );

// OR customize for posts only.
function custom_posts_index_settings( array $settings ) {
    /* Your adjustments */

    return $settings;
}
add_filter( 'algolia_posts_post_index_settings', 'custom_posts_index_settings' );

Register custom facet

Let's say you want to register a custom attribute number_bedroom as a facet in Algolia:

<?php

function custom_posts_index_settings( array $settings ) {
    $settings['attributesForFaceting'][] = 'number_bedroom';

    return $settings;
}
add_filter( 'algolia_posts_index_settings', 'custom_posts_index_settings' );