Skip to content

Artist/label autocomplete fails to surface exact matches (e.g. "Moon") #355

Description

@holden

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:

  1. Exact matches firstMOON when searching "moon"
  2. Prefix matches secondMOON, Keith / MOONGLOWS
  3. Contains matches lastDEAD MOON / HALF MOON RUN
  4. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions