Docs sort by their title, ignoring case (#961)

The title sort ordered by `a_text` (the indexed words) with binary collation:
uppercase titles sorted before all lowercase ones ("AWLS" before "Alps"), and
Docs with no indexed words sorted last though they show a title.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-12 13:32:13 -07:00 • committed by Roland
parent 30b2bf6df0
commit 4072ac30ed
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
2 changed files with 26 additions and 1 deletions

View file

@ -450,7 +450,8 @@ def _search_docs(session: Session, search_str=None, author=None, subject=None, l
elif order_key == 'size':
query = query.order_by(nullslast(direction(Doc.size)), direction(FileGroup.id))
elif order_key == 'title':
query = query.order_by(nullslast(direction(FileGroup.a_text)), direction(FileGroup.id))
# The title the user sees, ignoring case (a_text is the indexed words, and may be empty).
query = query.order_by(nullslast(direction(FileGroup.title.collate('NOCASE'))), direction(FileGroup.id))
browse_sql = _doc_browse_sql(order_by) if unfiltered else None
if browse_sql:

View file

@ -138,6 +138,30 @@ async def test_docs_search_orders(async_client, test_session, doc_factory):
assert response.status_code == HTTPStatus.BAD_REQUEST
@pytest.mark.asyncio
async def test_docs_search_title_order(async_client, test_session, doc_factory):
"""Docs sort by the title the user sees, ignoring case; Docs without a title are last in both directions."""
awls, alps, bobby, naturalist, untitled = (doc_factory() for _ in range(5))
awls.file_group.title = awls.file_group.a_text = 'AWLS Cover'
alps.file_group.title = alps.file_group.a_text = 'Alps Cover'
bobby.file_group.title = bobby.file_group.a_text = 'bobby make believe'
# A title with no indexed words: it must sort by its title, not last.
naturalist.file_group.title = 'iNaturalist Field Book'
naturalist.file_group.a_text = None
untitled.file_group.title = None
untitled.file_group.a_text = None
test_session.commit()
awls, alps, bobby, naturalist, untitled = (i.file_group_id for i in (awls, alps, bobby, naturalist, untitled))
async def search_ids(**body):
request, response = await async_client.post('/api/docs/search', content=json.dumps(body))
assert response.status_code == HTTPStatus.OK, response.json
return [i['id'] for i in response.json['file_groups']]
assert await search_ids(order_by='title') == [alps, awls, bobby, naturalist, untitled]
assert await search_ids(order_by='-title') == [naturalist, bobby, awls, alps, untitled]
@pytest.mark.asyncio
async def test_docs_search_defaults_to_rank(async_client, test_session, doc_factory):
"""A Docs search without an order puts the most relevant Docs first."""