From e3b7cab91ae88a64a10c689c0e0d783e39eafcd0 Mon Sep 17 00:00:00 2001 From: Vianney Date: Sun, 21 Apr 2024 21:51:24 +0200 Subject: [PATCH] Follow up for search operators --- src/monggregate/search/commons/count.py | 2 +- src/monggregate/search/operators/autocomplete.py | 3 ++- src/monggregate/search/operators/compound.py | 4 ++-- src/monggregate/search/operators/equals.py | 3 ++- src/monggregate/search/operators/exists.py | 3 ++- src/monggregate/search/operators/more_like_this.py | 8 ++++---- src/monggregate/search/operators/range.py | 8 ++++---- src/monggregate/search/operators/regex.py | 3 ++- src/monggregate/search/operators/text.py | 4 ++-- src/monggregate/search/operators/wildcard.py | 4 ++-- 10 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/monggregate/search/commons/count.py b/src/monggregate/search/commons/count.py index 92bbe97..9d3a423 100644 --- a/src/monggregate/search/commons/count.py +++ b/src/monggregate/search/commons/count.py @@ -41,7 +41,7 @@ def validate_type(cls, value:str)->str: return value @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express(self.dict(by_alias=True)) diff --git a/src/monggregate/search/operators/autocomplete.py b/src/monggregate/search/operators/autocomplete.py index c2ea559..1e9e592 100644 --- a/src/monggregate/search/operators/autocomplete.py +++ b/src/monggregate/search/operators/autocomplete.py @@ -129,6 +129,7 @@ """ +from monggregate.base import Expression from monggregate.utils import StrEnum from monggregate.search.operators.operator import SearchOperator from monggregate.search.commons import FuzzyOptions @@ -171,7 +172,7 @@ class Autocomplete(SearchOperator): score : dict|None @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "autocomplete":{ diff --git a/src/monggregate/search/operators/compound.py b/src/monggregate/search/operators/compound.py index d780ca7..514acb0 100644 --- a/src/monggregate/search/operators/compound.py +++ b/src/monggregate/search/operators/compound.py @@ -61,7 +61,7 @@ from typing_extensions import Self -from monggregate.base import pyd +from monggregate.base import Expression from monggregate.search.operators.operator import SearchOperator, OperatorLiteral from monggregate.search.operators.clause import ( Clause, @@ -111,7 +111,7 @@ class Compound(SearchOperator): minimum_should_match : int = 0 @property - def expression(self) -> dict: + def expression(self) -> Expression: clauses = {} if self.must: diff --git a/src/monggregate/search/operators/equals.py b/src/monggregate/search/operators/equals.py index 222466e..6afee42 100644 --- a/src/monggregate/search/operators/equals.py +++ b/src/monggregate/search/operators/equals.py @@ -69,6 +69,7 @@ """ from datetime import datetime +from monggregate.base import Expression from monggregate.search.operators.operator import SearchOperator class Equals(SearchOperator, smart_union=True): @@ -96,7 +97,7 @@ class Equals(SearchOperator, smart_union=True): score : dict|None @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "equals":{ diff --git a/src/monggregate/search/operators/exists.py b/src/monggregate/search/operators/exists.py index 990353e..09bbedb 100644 --- a/src/monggregate/search/operators/exists.py +++ b/src/monggregate/search/operators/exists.py @@ -28,6 +28,7 @@ """ +from monggregate.base import Expression from monggregate.search.operators.operator import SearchOperator class Exists(SearchOperator): @@ -51,7 +52,7 @@ class Exists(SearchOperator): @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "exists" : { diff --git a/src/monggregate/search/operators/more_like_this.py b/src/monggregate/search/operators/more_like_this.py index 2ff04f3..14c48b8 100644 --- a/src/monggregate/search/operators/more_like_this.py +++ b/src/monggregate/search/operators/more_like_this.py @@ -83,8 +83,8 @@ You can't use the moreLikeThis operator inside the embeddedDocument operator to query documents in an array. """ - -from monggregate.base import pyd +from typing import Any +from monggregate.base import pyd, Expression from monggregate.search.operators.operator import SearchOperator @@ -105,7 +105,7 @@ class MoreLikeThis(SearchOperator): like : dict | list[dict] @pyd.validator("like", pre=True, always=True) - def validate_like(cls, v): + def validate_like(cls, v:dict[str, Any])->dict[str, Any]|list[dict[str, Any]]: if isinstance(v, list): if len(v)==0: raise ValueError("The 'like' field must be a non-empty list of BSON documents.") @@ -113,7 +113,7 @@ def validate_like(cls, v): return v @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "moreLikeThis" : { diff --git a/src/monggregate/search/operators/range.py b/src/monggregate/search/operators/range.py index 7b88fd1..c50644b 100644 --- a/src/monggregate/search/operators/range.py +++ b/src/monggregate/search/operators/range.py @@ -68,7 +68,7 @@ from datetime import datetime -from monggregate.base import pyd +from monggregate.base import pyd, Expression from monggregate.search.operators.operator import SearchOperator class Range(SearchOperator, smart_union=True): @@ -97,20 +97,20 @@ class Range(SearchOperator, smart_union=True): score : dict|None @pyd.validator("gte", pre=True, always=True) - def at_least_one_lower(cls, value, values): + def at_least_one_lower(cls, value:int | float | datetime | None, values:dict)->int | float | datetime | None: if value is None and values.get("gt") is None: raise ValueError("at least one of gte or gt must be specified") return value @pyd.validator("lte", pre=True, always=True) - def at_least_one_upper(cls, value, values): + def at_least_one_upper(cls, value:int | float | datetime | None, values:dict)->int | float | datetime | None: if value is None and values.get("lt") is None: raise ValueError("at least one of lte or lt must be specified") return value @property - def expression(self) -> dict: + def expression(self) -> Expression: params = { "path": self.path, diff --git a/src/monggregate/search/operators/regex.py b/src/monggregate/search/operators/regex.py index 5fb5c7c..c2abd1f 100644 --- a/src/monggregate/search/operators/regex.py +++ b/src/monggregate/search/operators/regex.py @@ -53,6 +53,7 @@ """ +from monggregate.base import Expression from monggregate.search.operators.operator import SearchOperator class Regex(SearchOperator): @@ -81,7 +82,7 @@ class Regex(SearchOperator): score : dict | None = None @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "regex":{ diff --git a/src/monggregate/search/operators/text.py b/src/monggregate/search/operators/text.py index d24dca1..38312dc 100644 --- a/src/monggregate/search/operators/text.py +++ b/src/monggregate/search/operators/text.py @@ -30,7 +30,7 @@ """ -from monggregate.base import pyd +from monggregate.base import Expression from monggregate.search.operators.operator import SearchOperator from monggregate.search.commons.fuzzy import FuzzyOptions @@ -72,7 +72,7 @@ class Text(SearchOperator): synonyms : str | None = None @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "text" : self.dict(exclude_none=True, by_alias=True) diff --git a/src/monggregate/search/operators/wildcard.py b/src/monggregate/search/operators/wildcard.py index 1cb4a40..7c9bf14 100644 --- a/src/monggregate/search/operators/wildcard.py +++ b/src/monggregate/search/operators/wildcard.py @@ -65,7 +65,7 @@ """ -from monggregate.base import pyd +from monggregate.base import pyd, Expression from monggregate.search.operators.operator import SearchOperator class Wildcard(SearchOperator): @@ -92,7 +92,7 @@ class Wildcard(SearchOperator): score : dict | None = None @property - def expression(self) -> dict: + def expression(self) -> Expression: return self.express({ "wildcard":self.dict(exclude_none=True, by_alias=True)