Fix exact match search to prevent partial matches in results #992
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.
Problem
The
/api/search
endpoint withexact=true
parameter was incorrectly returning partial matches instead of true exact matches. For example, searching for "Oomycetes" with exact match enabled would return:This behavior violated the expectation that
exact=true
should only return entries where the search term appears as an exact match in labels, synonyms, or other indexed fields.Root Cause
The original implementation used Solr's
edismax
query parser with phrase field boosting (pf
) and minimum match (mm
) parameters. While this approach attempted to prioritize exact matches through scoring, it still allowed partial matches to appear in results when they occurred within larger phrases.Solution
Replaced the
edismax
approach with Solr's{!term f=FIELD}VALUE
syntax for true exact matching, as suggested by @henrietteharmse in the issue comments. This ensures only documents where the search term appears as an exact match in the specified fields are returned.Changes Made
Default exact search: Updated to use term queries across label, synonym, short_form, obo_id, and iri fields with proper field mapping through
SolrFieldMapper
Custom queryFields exact search: Applied the same term query approach when users specify custom search fields
Backward compatibility: Non-exact searches continue to use the existing
edismax
approach and remain unchangedTesting
Created a test ontology with the exact scenario described in the issue:
The fix ensures that searching for "Oomycetes" with
exact=true
will now only return the "Oomycota" class, as it's the only one with "Oomycetes" as an exact match in its synonym field.Fixes #[issue_number]
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.