mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 21:29:59 -05:00
Search: drop stopwords from unquoted terms, as Postgres did (#1085)
Postgres' english websearch_to_tsquery dropped stopwords; FTS5 indexes them. A stopword matches most of the library, so a search containing one read its whole doclist (slow on a cold HDD) and only found files that happened to contain it. translate_websearch now drops Postgres' english stopword list from unquoted terms; quoted phrases keep them. A search with nothing left to search (stopwords or punctuation only) now matches nothing in archive, video and doc search too, as files search, the search estimates and Postgres already did, instead of listing everything. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a61d528832
commit
45b52a698e
8 changed files with 107 additions and 11 deletions
|
|
@ -385,6 +385,9 @@ def _search_docs(session: Session, search_str=None, author=None, subject=None, l
|
|||
# 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
|
||||
if search_str and match is None:
|
||||
# Nothing to search (punctuation or stopwords only); match nothing, like an empty websearch_to_tsquery.
|
||||
return [], 0
|
||||
|
||||
query = session.query(FileGroup).options(*FileGroup.json_options()).join(Doc, Doc.file_group_id == FileGroup.id)
|
||||
|
||||
|
|
|
|||
|
|
@ -162,6 +162,23 @@ async def test_docs_search_title_order(async_client, test_session, doc_factory):
|
|||
assert await search_ids(order_by='-title') == [naturalist, bobby, awls, alps, untitled]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_docs_search_stopwords(async_client, test_session, doc_factory):
|
||||
"""Stopwords are dropped from a search; a search of only stopwords matches nothing (as Postgres did)."""
|
||||
solar, garden = doc_factory(), doc_factory()
|
||||
solar.file_group.title = solar.file_group.a_text = 'the solar panel'
|
||||
garden.file_group.title = garden.file_group.a_text = 'the garden'
|
||||
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']]
|
||||
|
||||
assert await search_ids(search_str='the') == []
|
||||
assert await search_ids(search_str='the solar') == [solar.file_group_id]
|
||||
|
||||
|
||||
@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."""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue