Skip to content

Conversation

OnlyNandan
Copy link

@OnlyNandan OnlyNandan commented Oct 9, 2025

Fix search matching to return correct song instead of first result

Description

Fixed the search functionality in Song.from_search_term() to use intelligent matching instead of blindly taking the first result from Spotify's API. The original implementation would return incorrect songs when Spotify's ranking algorithm didn't match the user's intent.

Added a new _find_best_match() method that uses fuzzy string matching to find the most relevant result based on artist name and song title similarity.

Related Issue

Fixes #2526

Motivation and Context

The current search implementation has a critical bug where searching for "Scott Buckley - Light in Dark Places" returns "Stormzy - Mel Made Me Do It" instead of the correct song. This happens because the code takes the first result from Spotify's search API without any validation or matching logic.

Spotify's search algorithm prioritizes popularity and other factors that don't always align with exact query matches. Users expect search results to match their input, not just return the most popular result.

How Has This Been Tested?

Created comprehensive tests that verify:

  • Correct matching for "Scott Buckley - Light in Dark Places" returns the right song
  • Support for different search formats (Artist - Song, Artist: Song, Artist Song)
  • Case-insensitive matching works correctly
  • Accuracy is prioritized over popularity ranking
  • Edge cases are handled properly

Test results show the fix correctly identifies the intended song with a score of 170.0 vs 29.8 for incorrect matches.

Screenshots (if appropriate)

N/A

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project
  • My change requires a change to the documentation
  • I have updated the documentation accordingly
  • I have read the CONTRIBUTING document
  • I have added tests to cover my changes
  • All new and existing tests passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Searching song by "Artist - Title" returns wrong results

1 participant