Adding url query to search files.
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Controller Tests / controller-tests (push) Has been cancelled
Jest Tests / jest-tests (push) Has been cancelled

This commit is contained in:
Roland 2026-02-19 18:22:59 -07:00
parent 4537c63d89
commit 48a81eccc2
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
4 changed files with 63 additions and 2 deletions

View file

@ -127,7 +127,7 @@ async def post_search_files(_: Request, body: schema.FilesSearchRequest):
with timer('Searching all files', 'info', logger__=logger):
file_groups, total = lib.search_files(body.search_str, body.limit, body.offset, body.mimetypes, body.model,
body.tag_names, body.headline, body.months, body.from_year, body.to_year,
body.any_tag, body.order)
body.any_tag, body.order, body.url)
return json_response(dict(file_groups=file_groups, totals=dict(file_groups=total)))

View file

@ -889,7 +889,8 @@ async def search_directories_by_name(session: Session, name: str, excluded: List
def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str] = None, model: str = None,
tag_names: List[str] = None, headline: bool = False, months: List[int] = None,
from_year: int = None, to_year: int = None, any_tag: bool = False, order: str = None) -> \
from_year: int = None, to_year: int = None, any_tag: bool = False, order: str = None,
url: str = None) -> \
Tuple[List[dict], int]:
"""Search the FileGroup table.
@ -908,6 +909,7 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str]
@param headline: Includes Postgresql headline if True.
@param months: A list of integers representing the index of the month of the year, starting at 1.
@param order: Used to change results from most relevant to recently viewed.
@param url: Filter by URL using case-insensitive partial match (ILIKE).
"""
params = dict(offset=offset, limit=limit, search_str=search_str, url_search_str=f'%{search_str}%')
wheres = []
@ -930,6 +932,10 @@ def search_files(search_str: str, limit: int, offset: int, mimetypes: List[str]
wheres, params = months_selector_to_where(wheres, params, months)
wheres, params = date_range_to_where(wheres, params, from_year, to_year)
if url:
params['url_filter'] = f'%{url}%'
wheres.append('fg.url ILIKE %(url_filter)s')
if search_str and headline:
headline = ''',
ts_headline(fg.title, websearch_to_tsquery(%(search_str)s)) AS "title_headline",

View file

@ -33,6 +33,7 @@ class FilesSearchRequest:
to_year: Optional[int] = None
any_tag: bool = False
order: Optional[str] = None
url: Optional[str] = None # Filter by URL (partial match)
def __post_init__(self):
if self.any_tag and self.tag_names:

View file

@ -1383,6 +1383,60 @@ async def test_rename_non_primary_file_renames_filegroup(async_client, test_sess
@pytest.mark.asyncio
async def test_file_search_url(test_session, make_files_structure, refresh_files):
"""Test searching FileGroups by their URL."""
foo, bar, baz = make_files_structure({
'foo.txt': 'content',
'bar.txt': 'content',
'baz.txt': 'content',
})
await refresh_files()
foo_fg, bar_fg, baz_fg = test_session.query(FileGroup).order_by(FileGroup.primary_path).all()
foo_fg.url = 'https://example.com/foo'
bar_fg.url = 'https://example.org/bar'
baz_fg.url = 'https://example.com/baz/page'
test_session.commit()
# Search by full URL
files, total = lib.search_files('', 10, 0, url='https://example.com/foo')
assert total == 1
assert files[0]['id'] == foo_fg.id
# Search by domain (partial match)
files, total = lib.search_files('', 10, 0, url='example.com')
assert total == 2
assert {f['id'] for f in files} == {foo_fg.id, baz_fg.id}
# Case insensitive search
files, total = lib.search_files('', 10, 0, url='EXAMPLE.COM')
assert total == 2
# No results for non-matching URL
files, total = lib.search_files('', 10, 0, url='nonexistent.com')
assert total == 0
@pytest.mark.asyncio
async def test_file_search_url_with_null(test_session, make_files_structure, refresh_files):
"""Test that files without URLs are excluded when URL filter is applied."""
foo, bar = make_files_structure({
'foo.txt': 'content',
'bar.txt': 'content',
})
await refresh_files()
foo_fg, bar_fg = test_session.query(FileGroup).order_by(FileGroup.primary_path).all()
foo_fg.url = 'https://example.com/foo'
# bar_fg.url remains None
test_session.commit()
files, total = lib.search_files('', 10, 0, url='example.com')
assert total == 1
assert files[0]['id'] == foo_fg.id
def test_bulk_update_file_groups_reorganize_handles_datetime(test_session, test_directory):
"""_bulk_update_file_groups_reorganize should handle datetime objects in files JSON.