Did You Mean

ElasticPress’ “Did You Mean” feature intelligently understands and handles typos in search keywords. When a user enters a search term with a typo, ElasticPress analyzes the search query, compares it to a database of indexed content, and returns the potential matches.

Theme Integration:

To display the suggestions on your site, an additional step is required, which involves incorporating the following line of code into the theme’s search template:

PHP
<?php do_action( 'ep_suggestions' ); ?>

By adding this code snippet, you enable the integration with Elasticsearch and instruct the plugin to display suggested terms when Elasticsearch detects a typo in the search term or when the suggested term yields more results than the original search term.

Settings: 

The feature includes a setting called “Search behavior when no result is found.” This setting determines how the plugin behaves when a search term does not yield any results. There are three options available:

  • Display a top suggestion: With this option selected, the plugin will display only the top-suggested term on the frontend.
  • Display all the suggestions: This option will display all the suggested terms on the frontend
  • Automatically redirect the user to the top suggestion: This will automatically redirect the user to the search result page of the top suggested term.

Customizations

Changing the Suggestion HTML structure. 

The ep_suggestion_html filter can be used to change the output of the suggested html. This filter recive 3 arguments. 

  • $html: Original html generated by the ElasticPress
  • $terms: List of all the suggested terms
  • $query: WordPress Query Object. 

The below example displays all the suggested terms on frontend

PHP
/**
 * Display all suggested terms.
 * 
 * @param string $html Original HTML output.
 * @param array $terms Array of suggested terms.
 * @param WP_Query $query WP_Query object.
 * 
 * @return string
 */
add_filter(
	'ep_suggestion_html',
	function( $html, $terms, $query )  {
		$html  = '<ul class="ep-suggestions-list">';
		$html .= '<li class="ep-suggestion-item">' . esc_html__( 'All suggested terms', 'elasticpress' ) . '</li>';
		foreach( $terms as $term ) {
			$html .= '<li class="ep-suggestion-item">';
			$html .= '<a href="' . esc_url( get_search_link( $term['text'] ) ) . '">' . esc_html( $term['text'] ) . '</a>';
			$html .= '</li>';
		}
		$html .= '</ul>';

		return $html;
	},
	10,
	3
);