Problem
Searching for an artist named MOON (artist ID 2439, 1 record on Imperial) is effectively impossible via the artist autocomplete dropdown. The general records search bar does return the record, but it's buried among 20+ other results containing "moon" (Moonglows, Moon Martin, Dead Moon, etc.).
What happens
Artist filter sidebar (/api/artists?q=moon):
- The API uses
ILIKE '%moon%' which matches 50+ artists containing "moon" anywhere in their name
- Results are ordered alphabetically by full name and limited to 10 results
- Artists like "AUGUST MOON", "BAIRD, Alton, & Moonlighters", "BLUE MOONS", etc. all sort before "MOON" alphabetically
- The exact match "MOON" never appears in the top 10 results — it's completely unreachable
- Same issue affects the label autocomplete and the record edit form's artist/label fields
General search (/records?search=moon):
- This does return the MOON record (around position 8), but it's easy to miss among Moonglows, Moon Mullican, Moon Martin, Keith Moon, Art Mooney, etc.
- The pg_search ranking doesn't strongly prioritize exact artist name matches
Verified
- Artist "MOON" (ID 2439) exists in the database, created 2010-12-21
- Record ID 17930, condition NM, on Imperial label, genre Psychedelic/Garage
- Directly filtering by artist_id (
?q[artist_id_eq]=2439) shows the record correctly
/api/artists?q=moon returns 10 results — none of which is "MOON"
Root Cause
1. Autocomplete API (/api/artists and /api/labels)
# app/controllers/api/artists_controller.rb
artists = Artist
.left_joins(:records)
.where("artists.name ILIKE ?", "%#{sanitized}%")
.group("artists.id")
.order(:name) # <-- Alphabetical only, no relevance
.limit(10) # <-- Only 10 results
Pure alphabetical ordering with a hard limit of 10 means exact matches and short names get buried behind longer compound names that happen to sort earlier.
2. General search ranking
The wide_search pg_search scope uses tsearch + trigram, but "moon" as a prefix matches many artists (moonglows, moondog, etc.) with similar relevance scores. The exact artist name match isn't strongly boosted.
Fix
Autocomplete API (primary fix)
Prioritize results by match quality:
- Exact matches first —
MOON when searching "moon"
- Prefix matches second —
MOON, Keith / MOONGLOWS
- Contains matches last —
DEAD MOON / HALF MOON RUN
- Within each tier, sort by name length (shorter = more relevant), then alphabetically
artists = Artist
.left_joins(:records)
.where("artists.name ILIKE ?", "%#{sanitized}%")
.group("artists.id")
.order(
Arel.sql("CASE " \
"WHEN artists.name ILIKE #{Artist.connection.quote(sanitized)} THEN 0 " \
"WHEN artists.name ILIKE #{Artist.connection.quote("#{sanitized}%")} THEN 1 " \
"ELSE 2 END"),
Arel.sql("LENGTH(artists.name)"),
:name
)
.limit(10)
Apply the same fix to both Api::ArtistsController and Api::LabelsController.
Affected files
app/controllers/api/artists_controller.rb
app/controllers/api/labels_controller.rb
Problem
Searching for an artist named MOON (artist ID 2439, 1 record on Imperial) is effectively impossible via the artist autocomplete dropdown. The general records search bar does return the record, but it's buried among 20+ other results containing "moon" (Moonglows, Moon Martin, Dead Moon, etc.).
What happens
Artist filter sidebar (
/api/artists?q=moon):ILIKE '%moon%'which matches 50+ artists containing "moon" anywhere in their nameGeneral search (
/records?search=moon):Verified
?q[artist_id_eq]=2439) shows the record correctly/api/artists?q=moonreturns 10 results — none of which is "MOON"Root Cause
1. Autocomplete API (
/api/artistsand/api/labels)Pure alphabetical ordering with a hard limit of 10 means exact matches and short names get buried behind longer compound names that happen to sort earlier.
2. General search ranking
The
wide_searchpg_search scope uses tsearch + trigram, but "moon" as a prefix matches many artists (moonglows, moondog, etc.) with similar relevance scores. The exact artist name match isn't strongly boosted.Fix
Autocomplete API (primary fix)
Prioritize results by match quality:
MOONwhen searching "moon"MOON, Keith/MOONGLOWSDEAD MOON/HALF MOON RUNApply the same fix to both
Api::ArtistsControllerandApi::LabelsController.Affected files
app/controllers/api/artists_controller.rbapp/controllers/api/labels_controller.rb