Search by properties

This commit is contained in:
marc
2025-05-04 22:10:10 +02:00
parent 4911935cdf
commit 47d18400c3
13 changed files with 245 additions and 68 deletions

View File

@@ -2,7 +2,9 @@ from collections.abc import Iterable
from typing import TypedDict
from folkugat_web.dal.sql import Connection, get_connection
from folkugat_web.model import search as search_model
from folkugat_web.model import temes as model
from folkugat_web.services import ngrams
PropertyRowTuple = tuple[int, int, str, str]
@@ -86,7 +88,8 @@ def insert_property(property: model.Property, con: Connection | None = None) ->
cur = con.cursor()
_ = cur.execute(query, data)
row: PropertyRowTuple = cur.fetchone()
return row_to_property(row)
evict_property_value_to_ngrams_cache()
return row_to_property(row)
def create_property(tema_id: int, field: model.PropertyField | None = None, con: Connection | None = None) -> model.Property:
@@ -111,6 +114,7 @@ def update_property(property: model.Property, con: Connection | None = None):
with get_connection(con) as con:
cur = con.cursor()
_ = cur.execute(query, data)
evict_property_value_to_ngrams_cache()
def delete_property(property_id: int, tema_id: int | None = None, con: Connection | None = None):
@@ -125,3 +129,32 @@ def delete_property(property_id: int, tema_id: int | None = None, con: Connectio
with get_connection(con) as con:
cur = con.cursor()
_ = cur.execute(query, data)
evict_property_value_to_ngrams_cache()
_property_value_to_ngrams_cache: dict[str, search_model.NGrams] | None = None
def evict_property_value_to_ngrams_cache():
global _property_value_to_ngrams_cache
_property_value_to_ngrams_cache = None
def get_property_value_to_ngrams(con: Connection | None = None) -> dict[str, search_model.NGrams]:
global _property_value_to_ngrams_cache
if _property_value_to_ngrams_cache is None:
_property_value_to_ngrams_cache = _get_property_value_to_ngrams(con)
return _property_value_to_ngrams_cache
def _get_property_value_to_ngrams(con: Connection | None = None) -> dict[str, search_model.NGrams]:
query = """
SELECT value
FROM tema_properties
"""
with get_connection(con) as con:
cur = con.cursor()
_ = cur.execute(query)
rows: list[tuple[str]] = cur.fetchall()
props = {prop[0] for prop in rows}
return {prop: ngrams.get_text_ngrams(prop.lower()) for prop in props}