diff --git a/modules/archive/lib.py b/modules/archive/lib.py index d5afe6ba..a8057f73 100644 --- a/modules/archive/lib.py +++ b/modules/archive/lib.py @@ -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", diff --git a/modules/archive/test/test_api.py b/modules/archive/test/test_api.py index 241db215..9ad4906d 100644 --- a/modules/archive/test/test_api.py +++ b/modules/archive/test/test_api.py @@ -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 ... to highlight matching words. + assert response.json['file_groups'][0]['d_headline'] == 'foo baz qux qux' + assert response.json['file_groups'][1]['d_headline'] == 'foo bar qux' + + def test_search_offset(test_session, archive_factory, test_client): """Archive search can be offset.""" for i in range(500): diff --git a/modules/videos/video/lib.py b/modules/videos/video/lib.py index 2b314f23..a3760bde 100644 --- a/modules/videos/video/lib.py +++ b/modules/videos/video/lib.py @@ -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", diff --git a/wrolpi/files/lib.py b/wrolpi/files/lib.py index 44421081..6f0c63d2 100644 --- a/wrolpi/files/lib.py +++ b/wrolpi/files/lib.py @@ -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 ""}