From 4072ac30edb9576ddadd744b9b861cf526545563 Mon Sep 17 00:00:00 2001 From: lrnselfreliance <33263708+lrnselfreliance@users.noreply.github.com> Date: Thu, 12 Nov 2026 13:32:13 -0700 Subject: [PATCH] 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 --- modules/docs/lib.py | 3 ++- modules/docs/test/test_api.py | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/modules/docs/lib.py b/modules/docs/lib.py index 4c70df77..bf0446b6 100644 --- a/modules/docs/lib.py +++ b/modules/docs/lib.py @@ -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: diff --git a/modules/docs/test/test_api.py b/modules/docs/test/test_api.py index 0ef6626c..2b76f35e 100644 --- a/modules/docs/test/test_api.py +++ b/modules/docs/test/test_api.py @@ -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."""