mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-14 06:15:26 -05:00
feat: diacritic-insensitive search (#2132)
* add normalized columns and use them for search * add migration to fill all normalized columns
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import Boolean, Float, ForeignKey, Integer, String, orm
|
||||
from sqlalchemy import Boolean, Float, ForeignKey, Integer, String, event, orm
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from text_unidecode import unidecode
|
||||
|
||||
from mealie.db.models._model_base import BaseMixins, SqlAlchemyBase
|
||||
from mealie.db.models.labels import MultiPurposeLabel
|
||||
@@ -63,7 +64,7 @@ class RecipeIngredient(SqlAlchemyBase, BaseMixins):
|
||||
recipe_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipes.id"))
|
||||
|
||||
title: Mapped[str | None] = mapped_column(String) # Section Header - Shows if Present
|
||||
note: Mapped[str | None] = mapped_column(String, index=True) # Force Show Text - Overrides Concat
|
||||
note: Mapped[str | None] = mapped_column(String) # Force Show Text - Overrides Concat
|
||||
|
||||
# Scaling Items
|
||||
unit_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("ingredient_units.id"), index=True)
|
||||
@@ -73,10 +74,35 @@ class RecipeIngredient(SqlAlchemyBase, BaseMixins):
|
||||
food: Mapped[IngredientFoodModel | None] = orm.relationship(IngredientFoodModel, uselist=False)
|
||||
quantity: Mapped[float | None] = mapped_column(Float)
|
||||
|
||||
original_text: Mapped[str | None] = mapped_column(String, index=True)
|
||||
original_text: Mapped[str | None] = mapped_column(String)
|
||||
|
||||
reference_id: Mapped[GUID | None] = mapped_column(GUID) # Reference Links
|
||||
|
||||
# Automatically updated by sqlalchemy event, do not write to this manually
|
||||
note_normalized: Mapped[str | None] = mapped_column(String, index=True)
|
||||
original_text_normalized: Mapped[str | None] = mapped_column(String, index=True)
|
||||
|
||||
@auto_init()
|
||||
def __init__(self, **_) -> None:
|
||||
pass
|
||||
def __init__(self, note: str | None = None, orginal_text: str | None = None, **_) -> None:
|
||||
# SQLAlchemy events do not seem to register things that are set during auto_init
|
||||
if note is not None:
|
||||
self.note_normalized = unidecode(note).lower().strip()
|
||||
|
||||
if orginal_text is not None:
|
||||
self.orginal_text = unidecode(orginal_text).lower().strip()
|
||||
|
||||
|
||||
@event.listens_for(RecipeIngredient.note, "set")
|
||||
def receive_note(target: RecipeIngredient, value: str, oldvalue, initiator):
|
||||
if value is not None:
|
||||
target.name_normalized = unidecode(value).lower().strip()
|
||||
else:
|
||||
target.name_normalized = None
|
||||
|
||||
|
||||
@event.listens_for(RecipeIngredient.original_text, "set")
|
||||
def receive_original_text(target: RecipeIngredient, value: str, oldvalue, initiator):
|
||||
if value is not None:
|
||||
target.original_text_normalized = unidecode(value).lower().strip()
|
||||
else:
|
||||
target.original_text_normalized = None
|
||||
|
||||
@@ -3,8 +3,10 @@ from typing import TYPE_CHECKING
|
||||
|
||||
import sqlalchemy as sa
|
||||
import sqlalchemy.orm as orm
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy.ext.orderinglist import ordering_list
|
||||
from sqlalchemy.orm import Mapped, mapped_column, validates
|
||||
from text_unidecode import unidecode
|
||||
|
||||
from mealie.db.models._model_utils.guid import GUID
|
||||
|
||||
@@ -54,8 +56,9 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
)
|
||||
|
||||
# General Recipe Properties
|
||||
name: Mapped[str] = mapped_column(sa.String, nullable=False, index=True)
|
||||
description: Mapped[str | None] = mapped_column(sa.String, index=True)
|
||||
name: Mapped[str] = mapped_column(sa.String, nullable=False)
|
||||
description: Mapped[str | None] = mapped_column(sa.String)
|
||||
|
||||
image: Mapped[str | None] = mapped_column(sa.String)
|
||||
|
||||
# Time Related Properties
|
||||
@@ -127,6 +130,10 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
# Automatically updated by sqlalchemy event, do not write to this manually
|
||||
name_normalized: Mapped[str] = mapped_column(sa.String, nullable=False, index=True)
|
||||
description_normalized: Mapped[str | None] = mapped_column(sa.String, index=True)
|
||||
|
||||
class Config:
|
||||
get_attr = "slug"
|
||||
exclude = {
|
||||
@@ -150,6 +157,8 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
def __init__(
|
||||
self,
|
||||
session,
|
||||
name: str | None = None,
|
||||
description: str | None = None,
|
||||
assets: list | None = None,
|
||||
notes: list[dict] | None = None,
|
||||
nutrition: dict | None = None,
|
||||
@@ -175,3 +184,23 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
|
||||
self.notes = [Note(**n) for n in notes]
|
||||
|
||||
self.date_updated = datetime.now()
|
||||
|
||||
# SQLAlchemy events do not seem to register things that are set during auto_init
|
||||
if name is not None:
|
||||
self.name_normalized = unidecode(name).lower().strip()
|
||||
|
||||
if description is not None:
|
||||
self.description_normalized = unidecode(description).lower().strip()
|
||||
|
||||
|
||||
@event.listens_for(RecipeModel.name, "set")
|
||||
def receive_name(target: RecipeModel, value: str, oldvalue, initiator):
|
||||
target.name_normalized = unidecode(value).lower().strip()
|
||||
|
||||
|
||||
@event.listens_for(RecipeModel.description, "set")
|
||||
def receive_description(target: RecipeModel, value: str, oldvalue, initiator):
|
||||
if value is not None:
|
||||
target.description_normalized = unidecode(value).lower().strip()
|
||||
else:
|
||||
target.description_normalized = None
|
||||
|
||||
@@ -7,6 +7,7 @@ from slugify import slugify
|
||||
from sqlalchemy import Select, and_, desc, func, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import joinedload
|
||||
from text_unidecode import unidecode
|
||||
|
||||
from mealie.db.models.recipe.category import Category
|
||||
from mealie.db.models.recipe.ingredient import RecipeIngredient
|
||||
@@ -150,12 +151,16 @@ class RepositoryRecipes(RepositoryGeneric[Recipe, RecipeModel]):
|
||||
return ids + additional_ids
|
||||
|
||||
def _add_search_to_query(self, query: Select, search: str) -> Select:
|
||||
normalized_search = unidecode(search).lower().strip()
|
||||
# I would prefer to just do this in the recipe_ingredient.any part of the main query, but it turns out
|
||||
# that at least sqlite wont use indexes for that correctly anymore and takes a big hit, so prefiltering it is
|
||||
ingredient_ids = (
|
||||
self.session.execute(
|
||||
select(RecipeIngredient.id).filter(
|
||||
or_(RecipeIngredient.note.ilike(f"%{search}%"), RecipeIngredient.original_text.ilike(f"%{search}%"))
|
||||
or_(
|
||||
RecipeIngredient.note_normalized.like(f"%{normalized_search}%"),
|
||||
RecipeIngredient.original_text_normalized.like(f"%{normalized_search}%"),
|
||||
)
|
||||
)
|
||||
)
|
||||
.scalars()
|
||||
@@ -164,11 +169,11 @@ class RepositoryRecipes(RepositoryGeneric[Recipe, RecipeModel]):
|
||||
|
||||
q = query.filter(
|
||||
or_(
|
||||
RecipeModel.name.ilike(f"%{search}%"),
|
||||
RecipeModel.description.ilike(f"%{search}%"),
|
||||
RecipeModel.name_normalized.like(f"%{normalized_search}%"),
|
||||
RecipeModel.description_normalized.like(f"%{normalized_search}%"),
|
||||
RecipeModel.recipe_ingredient.any(RecipeIngredient.id.in_(ingredient_ids)),
|
||||
)
|
||||
).order_by(desc(RecipeModel.name.ilike(f"%{search}%")))
|
||||
).order_by(desc(RecipeModel.name_normalized.like(f"%{normalized_search}%")))
|
||||
return q
|
||||
|
||||
def page_all(
|
||||
|
||||
Reference in New Issue
Block a user