mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 21:29:59 -05:00
Doc section hints look up only the sections of the page's docs (#1087)
_fetch_section_hints matched the search against every doc section in the library, joined each match to doc_section and doc, and only then kept the requested FileGroups. A common word matches thousands of sections, and each join is a random read of a large section row, so a cold files or docs search spent most of its time here, even when no result on the page was a doc. The match is now limited to the rowids of the requested docs' sections, read from covering indexes. The results are the same. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
1d432483bf
commit
c973d2f40b
2 changed files with 47 additions and 3 deletions
|
|
@ -491,6 +491,8 @@ def _fetch_section_hints(session, file_group_ids, search_str):
|
|||
|
||||
# FTS5 auxiliary functions (snippet, rank) must be evaluated in the innermost SELECT that scans the
|
||||
# FTS table; the window function must live in a separate outer layer.
|
||||
# The match is limited to the rowids of the requested docs' sections (read from covering indexes), so only
|
||||
# those sections are joined; a common word may match thousands of sections elsewhere in the library.
|
||||
sql = text('''
|
||||
SELECT fg_id, kind, ordinal, label, snippet FROM (
|
||||
SELECT fg_id, kind, ordinal, label, snippet,
|
||||
|
|
@ -506,7 +508,11 @@ def _fetch_section_hints(session, file_group_ids, search_str):
|
|||
FROM doc_section_fts
|
||||
JOIN doc_section ds ON ds.id = doc_section_fts.rowid
|
||||
JOIN doc d ON d.id = ds.doc_id
|
||||
WHERE doc_section_fts MATCH :q AND d.file_group_id IN :ids
|
||||
WHERE doc_section_fts MATCH :q
|
||||
AND doc_section_fts.rowid IN (
|
||||
SELECT s.id FROM doc page_doc
|
||||
JOIN doc_section s ON s.doc_id = page_doc.id
|
||||
WHERE page_doc.file_group_id IN :ids)
|
||||
)
|
||||
) WHERE rn = 1
|
||||
''').bindparams(bindparam('ids', expanding=True))
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
from datetime import datetime, timezone
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy import event, text
|
||||
|
||||
from modules.docs.lib import is_valid_author, split_authors, get_or_create_author_collection, \
|
||||
get_or_create_subject_collection, normalize_author, normalize_subject, split_subjects, is_valid_subject, \
|
||||
search_authors_by_name, search_subjects_by_name, _search_docs
|
||||
search_authors_by_name, search_subjects_by_name, _search_docs, _fetch_section_hints
|
||||
from modules.docs.models import Doc, DocSection
|
||||
from wrolpi.files.models import FileGroup
|
||||
|
||||
|
|
@ -337,6 +337,44 @@ async def test_search_docs_attaches_section_hint_pdf(test_session, test_director
|
|||
assert hint['label'] == 'Page 3'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_section_hints_read_only_the_page_sections(test_session, test_directory):
|
||||
"""Hints are looked up among the sections of the requested FileGroups only. The FTS match is limited to
|
||||
those sections' rowids, so a word that matches thousands of sections elsewhere in the library is not
|
||||
joined to every one of them (each join is a random read of a large section row)."""
|
||||
page_fg, _ = _make_doc_with_sections(test_session, test_directory, 'page.pdf', 'application/pdf', [
|
||||
(1, 'Page 1', 'cover page text'),
|
||||
(2, 'Page 2', 'here is where mullen shows up'),
|
||||
])
|
||||
other_fg, _ = _make_doc_with_sections(test_session, test_directory, 'other.pdf', 'application/pdf', [
|
||||
(1, 'Page 1', 'mullen again'),
|
||||
(2, 'Page 2', 'and mullen once more'),
|
||||
])
|
||||
|
||||
statements = []
|
||||
engine = test_session.get_bind()
|
||||
|
||||
def capture(conn, cursor, statement, parameters, context, executemany):
|
||||
if 'doc_section_fts' in statement:
|
||||
statements.append((statement, parameters))
|
||||
|
||||
event.listen(engine, 'before_cursor_execute', capture)
|
||||
try:
|
||||
hints = _fetch_section_hints(test_session, [page_fg.id], 'mullen')
|
||||
finally:
|
||||
event.remove(engine, 'before_cursor_execute', capture)
|
||||
|
||||
assert hints == {page_fg.id: dict(kind='pdf_page', ordinal=2, label='Page 2',
|
||||
snippet='here is where [[WROLPI_HL]]mullen[[/WROLPI_HL]] shows up')}
|
||||
assert other_fg.id not in hints
|
||||
|
||||
[(statement, parameters)] = statements
|
||||
plan = test_session.connection().exec_driver_sql(f'EXPLAIN QUERY PLAN {statement}', parameters).fetchall()
|
||||
[fts_step] = [row[3] for row in plan if 'doc_section_fts VIRTUAL TABLE' in row[3]]
|
||||
# FTS5 writes "=" in its index string when the scan is limited by rowid: "INDEX 0:=M1", not "INDEX 0:M1".
|
||||
assert ':=' in fts_step, f'The section match is not limited to the page: {plan}'
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_docs_no_hint_when_no_query(test_session, test_directory):
|
||||
"""Without a search query, results have no section_hint."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue