Skip to content

Search creators by surname when the query starts with a space - #674

Merged
bpepple merged 4 commits into
masterfrom
autocomplete-creator-improvement
Nov 3, 2026
Merged

bpepple merged 4 commits into
masterfrom
autocomplete-creator-improvement

Conversation

@bpepple

@bpepple bpepple commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Summary

When adding credits on the issue form, editors often search for a creator by typing a space and then the surname, e.g. Cameron. The old django-htmx-autocomplete widget passed that space to icontains, so first names were skipped. Since the move to izaya (#666), the search view strips the query, so Cameron matched "Cameron Stewart" and ranked it above "Don Cameron".

izaya 0.4.0 adds a clean_query hook so a lookup can keep whitespace that matters. CreatorLookup now uses it:

  • clean_query keeps one leading space and strips the rest. Only a plain space counts, so pasted text with a leading tab or non-breaking space searches normally. The space is dropped when fewer than 3 characters follow it, so Ca asks for more text instead of searching.
  • With a leading space, search only matches names and aliases where the text comes after a space, and puts names that end with it first. Cameron returns "Don Cameron" before "Jo Cameron Smith", and leaves out "Cameron Stewart", even when an alias like "John Cameron" makes it match.

Accent-insensitive queries

Lookups compared unaccent(name) with the query as typed, so an accented query never matched: Peña didn't find "Ana Peña". A new unaccent() helper strips accents from the query with PostgreSQL's own unaccent(), so both sides follow the same rules. It skips the database call for plain ASCII queries.

The new UnaccentModelLookup base class applies this to every lookup that searches through __unaccent__. Aliases and alternate names aren't unaccented, so they still get the query as typed. Issue searches without # now search the unaccented series name as well, so Spíder finds Spider-Man issues, and the search can use the series name trigram index.

Changes

  • comicsdb/lookups.py:
    • add clean_query and search to CreatorLookup
    • add unaccent() and UnaccentModelLookup, and use them in the Arc, Character, Creator, Publisher, Series, Team, Universe, Imprint and Issue lookups
  • pyproject.toml, uv.lock: require izaya 0.4.0
  • tests/comicsdb/test_lookups.py: cover query cleaning, surname matching and ordering, alias matching, the minimum length, accented queries and aliases, and the search endpoint

Editors often search for a credit's creator by typing a space and then the surname, e.g. " Cameron". The old autocomplete passed the space through to icontains, so first names were skipped. izaya stripped it,
so " Cameron" matched "Cameron Stewart" and even ranked first names above surnames.

Use izaya 0.4.0's clean_query hook to keep one leading space in CreatorLookup. With a leading space, search matches only names and aliases where the text follows a space, and puts names that end with it first. Searches without a leading space are unchanged.
@bpepple bpepple self-assigned this Nov 3, 2026
@bpepple bpepple added the enhancement New feature or request label Nov 3, 2026
- Sort leading-space results by surname match instead of the general rank, which put names starting with the query (found through an alias) ahead of the surname matches
- Drop the leading space when fewer than min_length characters follow it, so " Ca" shows the minimum-length message instead of searching
- Only treat a plain space as the surname prefix, and accept several leading spaces in search()
- Build the filter from search_fields and keep the distinct() guard
- Sort leading-space results by surname match instead of the general rank, which put names starting with the query (found through an alias) ahead of the surname matches
- Drop the leading space when fewer than min_length characters follow it, so " Ca" shows the minimum-length message instead of searching
- Only treat a plain space as the surname prefix, and accept several leading spaces in search()
- Unaccent the query with PostgreSQL's unaccent() for fields searched through __unaccent__, so "Peña" matches "Ana Peña" in every lookup
Search series names through the unaccented field and unaccent the query, matching the '#' path. "Spíder" now finds Spider-Man issues, and the search can use the series name trigram index.
@bpepple
bpepple merged commit 0e57bcd into master Nov 3, 2026
2 checks passed
@bpepple
bpepple deleted the autocomplete-creator-improvement branch November 3, 2026 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant