Skip to content
14 changes: 8 additions & 6 deletions tortoise/contrib/postgres/functions.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
from pypika_tortoise.terms import Function, Term

DEFAULT_TEXT_SEARCH_CONFIG = "pg_catalog.simple"


class ToTsVector(Function):
"""
to to_tsvector function
"""

def __init__(self, field: Term) -> None:
super().__init__("TO_TSVECTOR", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("TO_TSVECTOR", config_name, field)


class ToTsQuery(Function):
"""
to_tsquery function
"""

def __init__(self, field: Term) -> None:
super().__init__("TO_TSQUERY", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("TO_TSQUERY", config_name, field)


class PlainToTsQuery(Function):
"""
plainto_tsquery function
"""

def __init__(self, field: Term) -> None:
super().__init__("PLAINTO_TSQUERY", field)
def __init__(self, field: Term, config_name: str = DEFAULT_TEXT_SEARCH_CONFIG) -> None:
super().__init__("PLAINTO_TSQUERY", config_name, field)


class Random(Function):
Expand Down
10 changes: 4 additions & 6 deletions tortoise/contrib/postgres/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ class Comp(Comparator):
search = " @@ "


class SearchCriterion(BasicCriterion):
class SearchCriterion(BasicCriterion): # type: ignore
def __init__(self, field: Term, expr: Union[Term, Function]) -> None:
if isinstance(expr, Function):
_expr = expr
else:
_expr = ToTsQuery(expr)
super().__init__(Comp.search, ToTsVector(field), _expr)
if not isinstance(expr, Function):
expr = ToTsQuery(expr)
super().__init__(Comp.search, ToTsVector(config_name=expr.args[0].value, field=field), expr)
Loading