Testing Archive headline search.

This commit is contained in:
Roland 2023-04-12 19:07:16 -06:00
parent bba6a341b7
commit f9ad9981db
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
4 changed files with 33 additions and 8 deletions

View file

@ -406,7 +406,7 @@ def search_archives(search_str: str, domain: str, limit: int, offset: int, order
params.update(params_)
joins.append(join_)
if headline:
if search_str and headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",

View file

@ -62,6 +62,31 @@ def test_archives_search(test_session, archive_directory, archive_factory, test_
assert response.status_code == HTTPStatus.BAD_REQUEST
def test_archives_search_headline(test_session, archive_directory, archive_factory, test_client):
"""Headlines can be requested."""
archive_factory('example.com', 'https://example.com/one', 'my archive', 'foo bar qux')
archive_factory('example.com', 'https://example.com/one', 'other archive', 'foo baz qux qux')
archive_factory('example.org', title='archive third', contents='baz qux qux qux')
archive_factory('example.org') # has no contents
test_session.commit()
content = dict(search_str='foo')
request, response = test_client.post('/api/archive/search', content=json.dumps(content))
assert response.status_code == HTTPStatus.OK
# Headlines are only fetched if requested.
assert response.json['file_groups'][0]['d_headline'] is None
assert response.json['file_groups'][1]['d_headline'] is None
content = dict(search_str='foo', headline=True)
request, response = test_client.post('/api/archive/search', content=json.dumps(content))
assert response.status_code == HTTPStatus.OK
# Postgresql uses <b>...</b> to highlight matching words.
assert response.json['file_groups'][0]['d_headline'] == '<b>foo</b> baz qux qux'
assert response.json['file_groups'][1]['d_headline'] == '<b>foo</b> bar qux'
def test_search_offset(test_session, archive_factory, test_client):
"""Archive search can be offset."""
for i in range(500):

View file

@ -107,7 +107,7 @@ def search_videos(
params.update(params_)
joins.append(join_)
if headline:
if search_str and headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",

View file

@ -540,7 +540,7 @@ async def apply_indexers():
def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] = None, model: str = None,
tags: List[str] = None, headlines: bool = False) -> \
tags: List[str] = None, headline: bool = False) -> \
Tuple[List[dict], int]:
"""Search the FileGroup table.
@ -554,7 +554,7 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str]
mimetypes: Only return files that match these mimetypes.
model: Only return files that match this model.
tags: A list of tag names.
headlines: Includes Postgresql headlines if True.
headline: Includes Postgresql headline if True.
"""
params = dict(offset=offset, limit=limit)
wheres = []
@ -590,21 +590,21 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str]
params.update(params_)
joins.append(join_)
if headlines:
headlines = ''',
if search_str and headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",
ts_headline(fg.b_text, websearch_to_tsquery(%(search_str)s)) AS "b_headline",
ts_headline(fg.c_text, websearch_to_tsquery(%(search_str)s)) AS "c_headline",
ts_headline(fg.d_text, websearch_to_tsquery(%(search_str)s)) AS "d_headline"'''
else:
headlines = ''
headline = ''
wheres = '\n AND '.join(wheres)
selects = f"{', '.join(selects)}, " if selects else ""
join = '\n'.join(joins)
stmt = f'''
SELECT fg.id, {selects} COUNT(*) OVER() AS total
{headlines}
{headline}
FROM file_group fg
{join}
{f"WHERE {wheres}" if wheres else ""}