diff --git a/libervia/backend/memory/sqla.py b/libervia/backend/memory/sqla.py index 22c8897d..3d6ea4d4 100644 --- a/libervia/backend/memory/sqla.py +++ b/libervia/backend/memory/sqla.py @@ -28,7 +28,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union from alembic import config as al_config, script as al_script from alembic.runtime import migration as al_migration -from sqlalchemy import and_, delete, event, func, or_, update +from sqlalchemy import and_, delete, event, exists, func, or_, update from sqlalchemy import Integer, literal_column, text from sqlalchemy.dialects.sqlite import insert from sqlalchemy.engine import Connection, Engine @@ -667,6 +667,14 @@ class Storage: stmt = stmt.where(History.origin_id == filters["origin_id"]) if "thread_id" in filters: stmt = stmt.where(Thread.thread_id == filters["thread_id"]) + if "keyword" in filters: + keyword = filters["keyword"] + # FIXME: "json_each" works with SQLite, but when PostGreSQL support will + # be added, "json_array_elements" may have to be used with it. + subquery = func.json_each(History.extra["keywords"]).table_valued("value") + stmt = stmt.where( + exists().where(subquery.c.value == keyword) + ) if limit is not None: stmt = stmt.limit(limit)