Issue #3586543: Add AI reranking processor for Search API.
Description
A new Search API processor plugin (ai_reranker) that re-orders query results using the rerank operation type already in AI Core.
The rerank operation was added in #3488114 (closed) but nothing consumed it for Search API. #3557901 proposed putting reranking in ai_search, but reranking is a generic capability. It belongs in the ai core module where the operation type itself lives, so it works with any Search API backend (database, Solr, anything), not just the vector backend.
New files:
src/Plugin/search_api/processor/AiReranker.php:ProcessorPluginBaseplugin running atpostprocess_query. Reads source fields from each result item, callsprovider->rerank(), re-sorts items by the returned relevance scores, and applies an optional top-N slice. Logs a warning and returns the original result set if the rerank call fails.tests/src/Kernel/Plugin/Processor/AiRerankerTest.php: 7 Kernel tests covering plugin discovery, config round-trip, result reordering, top-N truncation, and three graceful-skip cases (no query keys, empty result set, missing provider config).
Modified:
tests/modules/ai_test/src/Plugin/AiProvider/EchoProvider.php: addsReRankInterfacewith deterministic descending mock scores so the Kernel tests have a provider to call.
No new runtime dependency on search_api. The plugin is only discovered when Search API is installed. drupal/search_api was already under require-dev in composer.json, so no change is needed there.
Closes #3586543
Testing instructions
Prerequisites:
- Enable
search_apiandsearch_api_db - Install and configure a rerank-capable AI provider (e.g. Cohere via
ai_provider_cohere) with a valid API key - Create a Search API server (Database backend) and an index with a node datasource,
titleandbodyfields indexed
Configuration:
- Open the index > Processors tab
- Check AI Reranker to enable it
- Select a rerank provider and model from the dropdown
- Set Top N (e.g.
5) and check Title and Body as source fields - Save. No errors expected.
Reranking:
- Run a search via a View or the Search API test page with a keyword
- Results are re-ordered by semantic relevance, not keyword rank
- With Top N set, the result count is capped to that number
Graceful skip:
- Browse the view without a search term. All results display normally, nothing logged.
Checklist
- I have linked the related issue in the MR title or description
- I have performed a self-review of my own code
- I have added or updated tests, or explained in the description why this change is not covered by tests
- I have updated documentation for any new or changed functionality
- I have written testing instructions and verified them locally
- I have noted any required post-merge steps (config imports, cache rebuilds, manual changes)
- This MR contains no breaking API or hook changes, or they are explicitly documented in the description
AI Compliance
Note
Check the one that best describes your usage, or leave all unchecked if AI was not significantly used.
-
AI Assisted Code
Mainly written by a human; AI used for autocomplete or partial generation under full human supervision. -
AI Generated Code
Mainly generated by AI, reviewed and approved by a human before this MR was created. -
Vibe Coded
Generated by AI and only functionally reviewed before this MR was created.