mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 01:16:18 -05:00
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:
parent
2800112f3e
commit
f1d344d8a2
8 changed files with 170 additions and 33 deletions
|
|
@ -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())
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue