fix(search): add keyword boost support for post search#4329
Open
faisalahammad wants to merge 1 commit into
Open
fix(search): add keyword boost support for post search#4329faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
- Add keyword_boosts setting to the Search feature schema. - Parse and sanitize keyword:boost pairs from admin textarea. - Inject boosted multi_match should clauses via ep_formatted_args at priority 25. - Add ep_search_keyword_boosts filter for programmatic overrides. - Add PHPUnit tests for injection, sanitization, and filter. Fixes 10up#3534
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
This PR adds the ability to boost specific keywords in post search results. Admins can configure keyword:boost pairs in the Post Search feature settings, and the plugin injects additional
multi_matchshouldclauses into the Elasticsearch query, increasing the relevance score for documents that contain those keywords. No reindex is required because the change only affects the query DSL at search time.Fixes #3534
Changes
Search feature settings (
includes/classes/Feature/Search/Search.php)Before:
There was no UI or mechanism to assign a boost factor to specific keywords.
After:
A new
keyword_booststextarea is exposed in the Post Search feature schema. The dashboard renders it automatically, and submitted values are sanitized into normalizedkeyword:boostlines.[ \x27default\x27 => \x27\x27, \x27help\x27 => __( \x27Boost specific keywords in search results. Enter one keyword per line, followed by a colon and a boost number, e.g. <code>premium:5</code>. Boosts must be between 0.01 and 100.\x27, \x27elasticpress\x27 ), \x27key\x27 => \x27keyword_boosts\x27, \x27label\x27 => __( \x27Keyword boosts\x27, \x27elasticpress\x27 ), \x27type\x27 => \x27textarea\x27, ],Why: The React dashboard is schema-driven, so adding a
textareaentry to the feature settings schema is enough to expose the option without touching any JavaScript.Query injection (
includes/classes/Feature/Search/Search.php)After:
A new
apply_keyword_boosts()method hooks intoep_formatted_argsat priority 25, after date decay (11) and weighting (20). It appends onemulti_matchphrase clause per configured keyword to the existingbool.shouldarray, reusing the same search fields so field weights remain effective.Why: The additional
shouldclauses are added inside thefunction_scorequery, so they work together with date decay and per-field weighting. Wrapping the original query inmustfor the edge case wherebool.shouldis missing keeps the original query required while the keyword clauses remain optional scoring signals.Filter hook
After:
Why: Developers can programmatically override or extend the keyword list without editing the admin setting.
Tests (
tests/php/features/TestSearch.php)After:
Five new tests verify the feature:
testKeywordBoostsInjectedWithDecay— confirms two keyword clauses are injected when date decay is enabled.testKeywordBoostsInjectedWithoutDecay— confirms injection when date decay is disabled.testKeywordBoostsNotInjectedWhenEmpty— confirms no extra clauses when the setting is empty.testKeywordBoostsSanitization— confirms malformed lines and out-of-range boosts are dropped.testKeywordBoostsFilter— confirms theep_search_keyword_boostsfilter works.Why: The tests assert the exact ES query shape and the sanitize behavior, which matches the existing Search/Weighting test patterns in the repo.
Testing
Test 1: Keyword boost affects ranking
keyword_boostswithpremium:5.Result: "Premium product" ranks higher than "Regular product".
Test 2: Sanitization
premium:5,sale:0,big:101,negative:-3, andno-colonon separate lines.Result: Only
premium:5is stored; invalid lines are dropped.Test 3: Empty setting
Result: The Elasticsearch query shape is unchanged from the default behavior.
Test 4: Filter override
ep_search_keyword_boostsreturningarray( \x27filterterm\x27 => 7 ).Result: The query contains a
multi_matchclause forfiltertermwithboost: 7.Automated tests
All five new keyword-boost tests pass. The full
TestSearchsuite has one pre-existing failure (testSearchOn) that also fails on the upstreamdevelopbranch in this environment and is unrelated to this change.