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:
lrnselfreliance 2026-12-03 17:16:36 -07:00 • committed by GitHub
parent a61d528832
commit 45b52a698e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 107 additions and 11 deletions

View file

@ -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."""