From 89c56628485b7b5d2a75fa49fc97619b31bc1219 Mon Sep 17 00:00:00 2001 From: maryhipp Date: Tue, 25 Jun 2024 20:27:37 -0400 Subject: [PATCH] add optional search term to search image metadata --- invokeai/app/api/routers/images.py | 2 ++ invokeai/app/services/image_records/image_records_base.py | 1 + .../app/services/image_records/image_records_sqlite.py | 8 ++++++++ invokeai/app/services/images/images_base.py | 1 + invokeai/app/services/images/images_default.py | 2 ++ 5 files changed, 14 insertions(+) diff --git a/invokeai/app/api/routers/images.py b/invokeai/app/api/routers/images.py index a947b83abe..3874a17ae1 100644 --- a/invokeai/app/api/routers/images.py +++ b/invokeai/app/api/routers/images.py @@ -316,6 +316,7 @@ async def list_image_dtos( ), offset: int = Query(default=0, description="The page offset"), limit: int = Query(default=10, description="The number of images per page"), + search_term: Optional[str] = Query(default=None, description="The term to search for"), ) -> OffsetPaginatedResults[ImageDTO]: """Gets a list of image DTOs""" @@ -326,6 +327,7 @@ async def list_image_dtos( categories, is_intermediate, board_id, + search_term ) return image_dtos diff --git a/invokeai/app/services/image_records/image_records_base.py b/invokeai/app/services/image_records/image_records_base.py index 45c0705090..e95026a1d3 100644 --- a/invokeai/app/services/image_records/image_records_base.py +++ b/invokeai/app/services/image_records/image_records_base.py @@ -41,6 +41,7 @@ class ImageRecordStorageBase(ABC): categories: Optional[list[ImageCategory]] = None, is_intermediate: Optional[bool] = None, board_id: Optional[str] = None, + search_term: Optional[str] = None, ) -> OffsetPaginatedResults[ImageRecord]: """Gets a page of image records.""" pass diff --git a/invokeai/app/services/image_records/image_records_sqlite.py b/invokeai/app/services/image_records/image_records_sqlite.py index ef73e79fa1..5bc72b965e 100644 --- a/invokeai/app/services/image_records/image_records_sqlite.py +++ b/invokeai/app/services/image_records/image_records_sqlite.py @@ -148,6 +148,7 @@ class SqliteImageRecordStorage(ImageRecordStorageBase): categories: Optional[list[ImageCategory]] = None, is_intermediate: Optional[bool] = None, board_id: Optional[str] = None, + search_term: Optional[str] = None, ) -> OffsetPaginatedResults[ImageRecord]: try: self._lock.acquire() @@ -208,6 +209,13 @@ class SqliteImageRecordStorage(ImageRecordStorageBase): """ query_params.append(board_id) + # Search term condition + if search_term: + query_conditions += """--sql + AND json_extract(images.metadata, '$') LIKE ? + """ + query_params.append(f'%{search_term}%') + query_pagination = """--sql ORDER BY images.starred DESC, images.created_at DESC LIMIT ? OFFSET ? """ diff --git a/invokeai/app/services/images/images_base.py b/invokeai/app/services/images/images_base.py index 9175fc4809..5093a85d5d 100644 --- a/invokeai/app/services/images/images_base.py +++ b/invokeai/app/services/images/images_base.py @@ -120,6 +120,7 @@ class ImageServiceABC(ABC): categories: Optional[list[ImageCategory]] = None, is_intermediate: Optional[bool] = None, board_id: Optional[str] = None, + search_term: Optional[str] = None ) -> OffsetPaginatedResults[ImageDTO]: """Gets a paginated list of image DTOs.""" pass diff --git a/invokeai/app/services/images/images_default.py b/invokeai/app/services/images/images_default.py index 1206526bd5..a7b9476fd0 100644 --- a/invokeai/app/services/images/images_default.py +++ b/invokeai/app/services/images/images_default.py @@ -206,6 +206,7 @@ class ImageService(ImageServiceABC): categories: Optional[list[ImageCategory]] = None, is_intermediate: Optional[bool] = None, board_id: Optional[str] = None, + search_term: Optional[str] = None, ) -> OffsetPaginatedResults[ImageDTO]: try: results = self.__invoker.services.image_records.get_many( @@ -215,6 +216,7 @@ class ImageService(ImageServiceABC): categories, is_intermediate, board_id, + search_term ) image_dtos = [