Search results sort as the filter dialog says (#959)

* Search results sort as the filter dialog says

Docs sorted ascending orders backwards and fell back to id order for every
descending order (`-size`, `-published_datetime`); it now takes the same `-`
prefixed orders as the other searches and rejects unknown ones with a 400.

Archives and Docs searches without an order now put the most relevant results
first, as Videos does and as the dialog shows.

Rank has no direction in the dialog: choosing it sends `rank` (most relevant
first) instead of inheriting a descending `-rank` (least relevant first).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* An old -rank link becomes rank when the filter dialog closes

The dialog shows Rank without a direction, but it seeded its draft from the URL
unchanged, so Done left a saved `-rank` (least relevant first) in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lrnselfreliance 2026-11-11 19:42:23 -07:00 • committed by GitHub
parent 2800112f3e
commit f1d344d8a2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 170 additions and 33 deletions

View file

@ -481,6 +481,9 @@ export function DocSearchFilterButton({size = 'medium'}) {
return <SearchFilter filters={docMimetypeFilterOptions} size={size}/>
}
// The search relevance sort; see `SearchFilterModal`.
const RANK_ORDER = 'rank';
// The app-wide default page size (matches usePages' fallback); used to reset Results Per Page on Clear All.
const DEFAULT_SEARCH_LIMIT = 24;
@ -540,7 +543,8 @@ export function SearchFilterModal(
// Seed the drafts from the URL each time the modal is opened.
React.useEffect(() => {
if (open) {
setDraftSort(sort || null);
// Rank has no direction here; an old `-rank` link becomes `rank` when the modal closes.
setDraftSort(sort === `-${RANK_ORDER}` ? RANK_ORDER : (sort || null));
setDraftFilter(filter || null);
setDraftTags(activeTags || []);
setDraftAnyTag(anyTag || false);
@ -614,17 +618,19 @@ export function SearchFilterModal(
setDraftDeep(false);
}
// Sort section (operates on the draft).
// Sort section (operates on the draft). Rank has no direction: `rank` is always the most relevant first
// (`-rank` would be the least relevant first), and a sort chosen after it starts descending.
const sortKey = draftSort ? draftSort.replace(/^-/, '') : (sorts ? sorts[0]['value'] : null);
const desc = draftSort ? draftSort.startsWith('-') : true;
const isRank = sortKey === RANK_ORDER;
const desc = draftSort && !isRank ? draftSort.startsWith('-') : true;
const sortButtons = sorts && sorts.map(o =>
<Button key={o['value']}
color={o['value'] === sortKey ? 'violet' : undefined}
onClick={() => setDraftSort(desc ? `-${o['value']}` : o['value'])}
onClick={() => setDraftSort(o['value'] === RANK_ORDER || !desc ? o['value'] : `-${o['value']}`)}
style={{margin: '0.2em'}}
>{o['text']}</Button>
);
const directionToggle = <Button icon={desc ? IconSortDescending : IconSortAscending}
const directionToggle = isRank ? null : <Button icon={desc ? IconSortDescending : IconSortAscending}
onClick={() => {
const base = sortKey || sorts[0]['value'];
setDraftSort(desc ? base : `-${base}`);
@ -658,7 +664,7 @@ export function SearchFilterModal(
<Stack gap='lg'>
{sorts &&
<SearchFilterSection header='Sort By'>
<div style={{marginBottom: '1em'}}>{directionToggle}</div>
{directionToggle && <div style={{marginBottom: '1em'}}>{directionToggle}</div>}
<div>{sortButtons}</div>
</SearchFilterSection>}

View file

@ -94,6 +94,41 @@ describe('SearchFilterButton', () => {
expect(spy.mock.calls[0][0]).toMatchObject({order: '-size'});
});
it('Rank always sorts the most relevant first, and has no direction', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
// The previous sort was descending; Rank must not inherit it as `-rank` (least relevant first).
renderButton({sorts: rankOrders}, {order: '-size'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Rank'));
expect(screen.queryByText('Descending')).toBeNull();
expect(screen.queryByText('Ascending')).toBeNull();
fireEvent.click(screen.getByText('Done'));
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0]).toMatchObject({order: 'rank'});
});
it('Done rewrites an old `-rank` link to the most relevant first', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
renderButton({sorts: rankOrders}, {order: '-rank'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Done'));
expect(spy).toHaveBeenCalledTimes(1);
expect(spy.mock.calls[0][0]).toMatchObject({order: 'rank'});
});
it('a sort chosen after Rank is descending', async () => {
const rankOrders = [{value: 'rank', text: 'Rank'}, ...videoOrders];
const spy = jest.fn();
renderButton({sorts: rankOrders}, {order: 'rank'}, spy);
fireEvent.click(screen.getByText('Filter'));
fireEvent.click(await screen.findByText('Size'));
expect(screen.getByText('Descending')).toBeInTheDocument();
fireEvent.click(screen.getByText('Done'));
expect(spy.mock.calls[0][0]).toMatchObject({order: '-size'});
});
it('Clear All resets results-per-page to the default on close', async () => {
const spy = jest.fn();
renderButton({sorts: videoOrders}, {l: '96'}, spy);

View file

@ -1344,6 +1344,10 @@ def search_archives(session: Session, search_str: str, domain: str, limit: int,
fts.b_headline, fts.c_headline, fts.d_headline,
fg.title AS title_headline'''
if not order and file_group_search:
# A search without an order puts the most relevant Archives first.
order = 'rank'
null_filter = None
if order:
try:

View file

@ -56,9 +56,9 @@ async def test_archives_search(test_session, archive_directory, archive_factory,
test_session.commit()
# Contents are d_text, so a deep search is required to match them.
# 1 and 2 contain "foo".
# 1 and 2 contain "foo" once; 1 is shorter, so it is more relevant.
data = {'search_str': 'foo', 'deep': True}
await check_results(async_client, data, [2, 1])
await check_results(async_client, data, [1, 2])
# The default (fast) search does not match contents.
data = {'search_str': 'foo'}
@ -119,6 +119,27 @@ async def test_search_archive_tags(test_session, async_client, archive_factory,
assert response.json['totals']['file_groups'] == 0
@pytest.mark.asyncio
async def test_search_archive_defaults_to_rank(test_session, async_client, archive_factory):
"""An Archive search without an order puts the most relevant Archives first."""
# archive_factory makes each Archive newer than the last, so date order would put the weak match first.
strong = archive_factory('example.com', title='foo foo foo', contents='foo foo foo')
weak = archive_factory('example.com', title='other', contents='foo bar baz qux quux corge grault garply')
test_session.commit()
async def search_ids(**body):
request, response = await async_client.post('/api/archive/search', content=json.dumps(body))
assert response.status_code == HTTPStatus.OK, response.json
return [i['id'] for i in response.json['file_groups']]
best_first = [strong.file_group_id, weak.file_group_id]
assert await search_ids(search_str='foo', deep=True) == best_first
assert await search_ids(search_str='foo', deep=True, order_by='rank') == best_first
assert await search_ids(search_str='foo', deep=True, order_by='-published_datetime') == best_first[::-1]
# Without a search, newest first.
assert await search_ids() == best_first[::-1]
@pytest.mark.asyncio
async def test_search_archive_any_tag(test_session, async_client, archive_factory, tag_factory):
"""Archives can be filtered to those with any Tag."""
@ -162,7 +183,7 @@ async def test_archives_search_headline(test_session, archive_directory, archive
assert response.json['file_groups'][0]['d_headline'] is None
assert response.json['file_groups'][1]['d_headline'] is None
content = dict(search_str='foo', headline=True, deep=True)
content = dict(search_str='foo', headline=True, deep=True, order_by='-published_datetime')
request, response = await async_client.post('/api/archive/search', content=json.dumps(content))
assert response.status_code == HTTPStatus.OK

View file

@ -10,7 +10,7 @@ from sqlalchemy.orm import Session
from wrolpi import fts
from wrolpi.collections.models import Collection
from wrolpi.collections.types import collection_type_registry
from wrolpi.errors import ValidationError
from wrolpi.errors import ValidationError, InvalidOrderBy
from wrolpi.files.lib import split_path_stem_and_suffix, get_mimetype, cached_search_total, \
search_filter_cache_key
from wrolpi.tags import TagFile, tag_names_to_file_group_sub_select
@ -351,11 +351,13 @@ def _get_doc(session, file_group_id: int, skip_viewed: bool = False):
return doc
# The orders a Docs search accepts; a `-` prefix is descending, like the other searches.
DOC_ORDERS = ('rank', '-rank', 'published_datetime', '-published_datetime', 'size', '-size', 'title', '-title')
# ORDER BY clauses for the unfiltered docs browse. Only orderings that walk a `file_group` index
# qualify; the rest keep the ORM query.
DOC_BROWSE_ORDERS = {
'published_datetime': 'fg.published_datetime DESC NULLS LAST, fg.id DESC',
'id': 'fg.id DESC',
'-published_datetime': 'fg.published_datetime DESC NULLS LAST, fg.id DESC',
}
@ -372,10 +374,14 @@ def _doc_browse_sql(order_by: str) -> Optional[str]:
def _search_docs(session: Session, search_str=None, author=None, subject=None, language=None, mimetype=None,
limit=20, offset=0, order_by='published_datetime', tag_names=None, deep=False, any_tag=False):
limit=20, offset=0, order_by=None, tag_names=None, deep=False, any_tag=False):
"""Search Docs. Without `order_by`, the most relevant Docs are first when searching, otherwise the newest."""
from .models import Doc
from wrolpi.files.models import FileGroup
if order_by and order_by not in DOC_ORDERS:
raise InvalidOrderBy(f'Invalid order_by {order_by}')
# Deep search matches all FTS columns (including d_text, the document contents); the default matches only
# the much smaller a/b/c columns.
match = fts.translate_websearch(search_str, None if deep else fts.ABC_COLUMNS) if search_str else None
@ -428,25 +434,25 @@ def _search_docs(session: Session, search_str=None, author=None, subject=None, l
# Count before ORDER BY / LIMIT; cache by filters not page.
total = cached_search_total(cache_key, lambda q=query: q.count())
# Ordering.
if order_by == 'rank' and fts_sq is not None:
query = query.order_by(
desc(fts_sq.c.ts_rank),
desc(FileGroup.id),
)
elif order_by == 'published_datetime':
query = query.order_by(nullslast(desc(FileGroup.published_datetime)), desc(FileGroup.id))
elif order_by == 'size':
query = query.order_by(nullslast(desc(Doc.size)))
elif order_by == 'title':
query = query.order_by(nullslast(asc(FileGroup.a_text)))
else:
query = query.order_by(desc(FileGroup.id))
# Ordering. Rank needs a search; without one, the newest Docs are first.
if not order_by:
order_by = 'rank' if fts_sq is not None else '-published_datetime'
elif order_by in ('rank', '-rank') and fts_sq is None:
order_by = '-published_datetime'
direction = desc if order_by.startswith('-') else asc
order_key = order_by.lstrip('-')
if order_key == 'rank':
# `rank` is the most relevant first (highest ts_rank), `-rank` the least relevant first.
rank_direction = asc if order_by.startswith('-') else desc
query = query.order_by(rank_direction(fts_sq.c.ts_rank), rank_direction(FileGroup.id))
elif order_key == 'published_datetime':
query = query.order_by(nullslast(direction(FileGroup.published_datetime)), direction(FileGroup.id))
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))
# `size` sorts on a doc column and `title` has no file_group index, so neither may fall back to `id`.
browse_sql = None
if unfiltered and order_by not in ('size', 'title'):
browse_sql = _doc_browse_sql(order_by if order_by in DOC_BROWSE_ORDERS else 'id')
browse_sql = _doc_browse_sql(order_by) if unfiltered else None
if browse_sql:
fg_ids = [i for i, in session.execute(text(browse_sql), dict(limit=limit, offset=offset)).fetchall()]
by_id = {fg.id: fg for fg in session.query(FileGroup).options(*FileGroup.json_options())

View file

@ -12,7 +12,7 @@ class DocSearchRequest:
language: Optional[str] = None
mimetype: Optional[str] = None
tag_names: Optional[list] = dataclasses.field(default_factory=list)
order_by: Optional[str] = 'published_datetime'
order_by: Optional[str] = None # Most relevant first when searching, otherwise newest first.
limit: int = 20
offset: int = 0
deep: bool = False # When True, search all text including document contents (d_text)

View file

@ -98,6 +98,71 @@ async def test_docs_search_by_tag(async_client, test_session, doc_factory, tag_f
assert response.json['totals']['file_groups'] == 0
@pytest.mark.asyncio
async def test_docs_search_orders(async_client, test_session, doc_factory):
"""Docs sort like the other searches: a `-` prefix is descending, and an unknown order is a 400."""
import datetime
utc = datetime.timezone.utc
# Each order puts these in a different sequence than their ids, so an ignored order cannot pass.
one = doc_factory(size=500)
two = doc_factory(size=10)
three = doc_factory(size=1000)
for doc, year, title in ((one, 2022, 'mmm'), (two, 2024, 'aaa'), (three, 2020, 'zzz')):
doc.file_group.published_datetime = datetime.datetime(year, 1, 1, tzinfo=utc)
doc.file_group.title = doc.file_group.a_text = title
test_session.commit()
one, two, three = one.file_group_id, two.file_group_id, three.file_group_id
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']]
expected = {
'size': [two, one, three],
'-size': [three, one, two],
'published_datetime': [three, one, two],
'-published_datetime': [two, one, three],
'title': [two, one, three],
'-title': [three, one, two],
}
for order_by, ids in expected.items():
assert await search_ids(order_by=order_by) == ids, order_by
# A filtered search orders the same as the unfiltered browse.
assert await search_ids(order_by=order_by, mimetype='application/pdf') == ids, order_by
# Without an order, the newest Docs are first.
assert await search_ids() == [two, one, three]
request, response = await async_client.post('/api/docs/search', content=json.dumps(dict(order_by='bogus')))
assert response.status_code == HTTPStatus.BAD_REQUEST
@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."""
import datetime
utc = datetime.timezone.utc
strong, weak = doc_factory(), doc_factory()
strong.file_group.published_datetime = datetime.datetime(2020, 1, 1, tzinfo=utc)
strong.file_group.title = strong.file_group.a_text = 'foo foo foo'
# The weak match is newer, so date order would put it first.
weak.file_group.published_datetime = datetime.datetime(2024, 1, 1, tzinfo=utc)
weak.file_group.title = weak.file_group.a_text = 'foo bar baz qux quux corge grault garply'
test_session.commit()
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']]
best_first = [strong.file_group_id, weak.file_group_id]
assert await search_ids(search_str='foo') == best_first
assert await search_ids(search_str='foo', order_by='rank') == best_first
assert await search_ids(search_str='foo', order_by='-rank') == best_first[::-1]
assert await search_ids(search_str='foo', order_by='-published_datetime') == best_first[::-1]
@pytest.mark.asyncio
async def test_docs_search_tag_names_and_any_tag(async_client, test_session, doc_factory, tag_factory):
"""Docs tag filtering matches the other searches: `tag_names` requires every tag, `any_tag` requires some tag."""

View file

@ -361,7 +361,7 @@ async def test_search_docs_browse_walks_file_group_date_index(test_session, test
docs[2].file_group.published_datetime = datetime(2021, 1, 1, tzinfo=timezone.utc)
test_session.commit()
sql = _doc_browse_sql('published_datetime')
sql = _doc_browse_sql('-published_datetime')
plan = ' '.join(row[3] for row in test_session.execute(text(f'EXPLAIN QUERY PLAN {sql}'),
dict(limit=20, offset=0)).fetchall())
assert 'file_group_published_datetime_idx' in plan, plan