fix(did-you-mean): suppress suggestions for valid queries and matching terms#4330
Open
faisalahammad wants to merge 1 commit into
Open
fix(did-you-mean): suppress suggestions for valid queries and matching terms#4330faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
…hes original term - Suppress suggestion in get_suggestion() if query found_posts > 0 - Suppress suggestion if top suggestion equals original search term (case-insensitive) - Apply same guard in automatically_redirect_user() to prevent redirect loops - Add unit tests for both new behaviors Fixes 10up#3602
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
"Did You Mean" currently shows the top suggestion even when the current search term already returns results. This leads to confusing suggestions like "Did you mean: shift?" when searching for "shirt" with valid results. This fix suppresses the suggestion when the query has results, or when the suggestion matches the original term. It also applies the same guard to the redirect behavior to prevent a redirect loop.
Fixes #3602
Changes
includes/classes/Feature/DidYouMean/DidYouMean.phpget_suggestion()now returnsfalseearly in two cases:Before:
After:
Why: This matches the existing behavior of
get_alternatives_terms(), which already hides the alternative list when the query has results. The case-insensitive match guard prevents the feature from suggesting the same term the user already typed.automatically_redirect_user()also skips redirecting if the suggested term equals the current search term:After:
Why: Prevents an infinite redirect loop when the phrase suggester returns the same term as the original query.
Testing
Test 1: Valid query with results
Result: results load and no "Did you mean: shift?" message appears.
Test 2: Typo with no results
Result: "Did you mean: shirt?" appears because the query has no results.
Test 3: Same-term suggestion
Result: no suggestion is displayed.
Unit tests
Two new tests added to
tests/php/features/TestDidYouMean.php:testGetSuggestionNotShownWhenResultsExisttestGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTermtestGetSuggestionNotShownWhenTopSuggestionMatchesOriginalTermCaseInsensitiveRun with:
All 20 tests pass.