Skip to content

Instantly share code, notes, and snippets.

@richaber
Created October 16, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richaber/0cff732a357d073779bb01cd02d8ceef to your computer and use it in GitHub Desktop.
Save richaber/0cff732a357d073779bb01cd02d8ceef to your computer and use it in GitHub Desktop.
Custom WP Search with Algolia typoTolerance settings (indices and clients)

Algolia accepts the following values for the typoTolerance setting (from the Algolia "Configuring Typo Tolerance" documentation):

  1. true: Activate typo-tolerance (default, suggested value).
  2. false: Disable typo-tolerance.
  3. min: Only keep results with the lowest number of typos. This means that if you have 1 or more records that match, you’ll only receive those records; but if you have no records that match, you’ll receive records with typo counts of 1 (or 2 if there are none with 1).
  4. strict: Similar to 3, but keep the 2 lowest number of typos. This is useful when you want to ensure more results.

WP Search with Algolia can be customized through the use of PHP filters, and customizing the autocomplete.php and instantsearch.php templates.

There is additional documentation about customizing index settings here https://community.algolia.com/wordpress/indexing-settings.html#custom-index-settings.

There is additional documentation about customizing the Autocomplete template here https://community.algolia.com/wordpress/customize-autocomplete.html.

There is additional documentation about customizing the Instantsearch template here https://community.algolia.com/wordpress/customize-search-page.html.

To adjust the typoTolerance setting used by indices, use the following PHP filters (adjust the value as desired):

/**
 * Override the typoTolerance setting for the searchable post index.
 *
 * Searchable post index is, generally speaking,
 * the index used by instantsearch and backend search
 * (unless there have been other customizations).
 *
 * @see Algolia_Searchable_Posts_Index::get_settings()
 *
 * @param array $settings Array of searchable post index settings.
 *
 * @return array
 */
function myprefix_algolia_searchable_post_index_settings( $settings ) {
	$settings['typoTolerance'] = false;

	return $settings;
}

add_filter( 'algolia_searchable_posts_index_settings', 'myprefix_algolia_searchable_post_index_settings' );

/**
 * Override the typoTolerance setting for all other post indices.
 *
 * Individual post indices are, generally speaking,
 * indices used by autocomplete search
 * (unless there have been other customizations).
 *
 * @see Algolia_Posts_Index::get_settings()
 *
 * @param array  $settings  Array of post index settings.
 * @param string $post_type The post type.
 *
 * @return array
 */
function myprefix_algolia_post_index_settings( $settings, $post_type ) {
	$settings['typoTolerance'] = false;

	return $settings;
}

add_filter( 'algolia_posts_index_settings', 'myprefix_algolia_post_index_settings', 10, 2 );

/**
 * Override the typoTolerance setting for term indices.
 *
 * @see Algolia_Terms_Index::get_settings()
 *
 * @param array  $settings Array of term index settings.
 * @param string $taxonomy The taxonomy.
 *
 * @return array
 */
function myprefix_algolia_term_index_settings( $settings, $taxonomy ) {
	$settings['typoTolerance'] = false;

	return $settings;
}

add_filter( 'algolia_terms_index_settings', 'myprefix_algolia_term_index_settings', 10, 2 );

/**
 * Override the typoTolerance setting for user indices.
 *
 * @see Algolia_Users_Index::get_settings()
 *
 * @param array $settings Array of user index settings.
 *
 * @return array
 */
function myprefix_algolia_user_index_settings( $settings ) {
	$settings['typoTolerance'] = false;

	return $settings;
}

add_filter( 'algolia_users_index_settings', 'myprefix_algolia_user_index_settings' );

If you use the "Use Algolia in the backend" setting under Algolia Search -> Search Page -> Search results, you will want to adjust the PHP SearchClient params:

/**
 * Override the typoTolerance setting for PHP backed search.
 *
 * @param array $params Array of search params.
 *
 * @return array
 */
function myprefix_algolia_search_params( $params ) {
	$params['typoTolerance'] = false;

	return $params;
}

add_filter( 'algolia_search_params', 'myprefix_algolia_search_params' );

If you use the "Use Algolia with Instantsearch.js" setting under Algolia Search -> Search Page -> Search results, you will want to copy the instantsearch.php template to your child theme as described here and add your desired typoTolerance setting to the searchParameters configuration object. For example:

				/* Instantiate instantsearch.js */
				var search = instantsearch({
					appId: algolia.application_id,
					apiKey: algolia.search_api_key,
					indexName: algolia.indices.searchable_posts.name,
					urlSync: {
						mapping: {'q': 's'},
						trackedParameters: ['query']
					},
					searchParameters: {
						typoTolerance: false,
						facetingAfterDistinct: true,
						highlightPreTag: '__ais-highlight__',
						highlightPostTag: '__/ais-highlight__'
					}
				});

If you use the "Enable autocomplete" setting under Algolia Search -> Autocomplete, you will want to copy the autocomplete.php template to your child theme as described here and add your desired typoTolerance setting to the unnamed configuration object. For example:

				source: algoliaAutocomplete.sources.hits( client.initIndex( config[ 'index_name' ] ), {
					hitsPerPage: config[ 'max_suggestions' ],
					attributesToSnippet: [
						'content:10'
					],
					typoTolerance: false,
					highlightPreTag: '__ais-highlight__',
					highlightPostTag: '__/ais-highlight__'
				} ),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment