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
|
|
@ -1323,9 +1323,11 @@ def search_archives(session: Session, search_str: str, domain: str, limit: int,
|
|||
fts_join = ''
|
||||
headline_columns = ''
|
||||
# The fast (deep=False) search only matches the a/b/c FTS columns; deep also matches d
|
||||
# (page contents). `file_group_search` returns None when the search string is unusable, in
|
||||
# which case we behave as if no search was requested.
|
||||
# (page contents). `file_group_search` returns None when the search string has nothing to search
|
||||
# (punctuation or stopwords only); that matches nothing, like an empty websearch_to_tsquery did.
|
||||
file_group_search = fts.file_group_search(search_str, deep=deep) if search_str else None
|
||||
if search_str and not file_group_search:
|
||||
return [], 0
|
||||
if file_group_search:
|
||||
# A search_str was provided by the user, modify the query to filter by it. Rank and
|
||||
# snippets come from a subquery (FTS5 auxiliary functions cannot mix with some SELECT shapes).
|
||||
|
|
|
|||
|
|
@ -80,9 +80,16 @@ async def test_archives_search(test_session, archive_directory, archive_factory,
|
|||
data = {'search_str': 'baz', 'domain': 'example.org', 'deep': True}
|
||||
await check_results(async_client, data, [3, ])
|
||||
|
||||
# 1's title contains "my". Postgres ignored this stopword; FTS5 has no stopwords, so it matches.
|
||||
# 1's title contains "my", a stopword. A search of only stopwords matches nothing (as Postgres did), rather
|
||||
# than listing every archive.
|
||||
data = {'search_str': 'my', 'deep': True}
|
||||
await check_results(async_client, data, [1, ])
|
||||
await check_results(async_client, data, [])
|
||||
# "my archive" is a search for "archive", which all three titles contain.
|
||||
request, response = await async_client.post('/api/archive/search',
|
||||
content=json.dumps({'search_str': 'archive', 'deep': True}))
|
||||
archive_ids = [i['id'] for i in response.json['file_groups']]
|
||||
assert len(archive_ids) == 3
|
||||
await check_results(async_client, {'search_str': 'my archive', 'deep': True}, archive_ids)
|
||||
|
||||
# 3's title contains "third", found by the default (fast) search and the deep search.
|
||||
data = {'search_str': 'third'}
|
||||
|
|
|
|||
|
|
@ -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."""
|
||||
|
|
|
|||
|
|
@ -685,7 +685,7 @@ async def test_search_videos_default_page_pagination(test_session, video_factory
|
|||
await assert_video_search(order_by='-published_datetime', assert_total=6)
|
||||
assert fast_path.called, 'the unfiltered date-ordered page should take the fast path'
|
||||
fast_path.reset_mock()
|
||||
await assert_video_search(order_by='-published_datetime', search_str='a', assert_total=1)
|
||||
await assert_video_search(order_by='-published_datetime', search_str='b', assert_total=1)
|
||||
await assert_video_search(order_by='-size', assert_total=6)
|
||||
assert not fast_path.called, 'searches and other orders use the generic query'
|
||||
|
||||
|
|
@ -786,3 +786,14 @@ async def test_video_json_does_not_read_info_json(async_client, test_session, vi
|
|||
request, response = await async_client.get(f'/api/videos/{video.file_group_id}')
|
||||
assert response.status_code == HTTPStatus.OK
|
||||
assert 'description' not in response.json['file_group']['video']
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_videos_stopwords(test_session, video_factory, assert_video_search):
|
||||
"""Stopwords are dropped from a search; a search of only stopwords matches nothing (as Postgres did)."""
|
||||
solar = video_factory(title='the solar panel', with_video_file=True)
|
||||
video_factory(title='the garden', with_video_file=True)
|
||||
test_session.commit()
|
||||
|
||||
await assert_video_search(search_str='the', assert_ids=[])
|
||||
await assert_video_search(search_str='the solar', assert_ids=[solar.id])
|
||||
|
|
|
|||
|
|
@ -227,6 +227,9 @@ def search_videos(
|
|||
# Rank/snippets come from a subquery so they stay valid if a caller adds window functions.
|
||||
fts_search = fts.file_group_search_join(search_str, deep=deep, headlines=bool(headline)) \
|
||||
if search_str else None
|
||||
if search_str and not fts_search:
|
||||
# Nothing to search (punctuation or stopwords only); match nothing, like an empty websearch_to_tsquery.
|
||||
return [], 0
|
||||
if fts_search:
|
||||
select_columns = f'fg.id, {fts_search.rank_select}'
|
||||
joins.append(fts_search.join)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue