Skip to content

Commit

Permalink
Follow up for search operators
Browse files Browse the repository at this point in the history
  • Loading branch information
VianneyMI committed Apr 21, 2024
1 parent 2136f91 commit e3b7cab
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/monggregate/search/commons/count.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 2 additions & 1 deletion src/monggregate/search/operators/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -171,7 +172,7 @@ class Autocomplete(SearchOperator):
score : dict|None

@property
def expression(self) -> dict:
def expression(self) -> Expression:

return self.express({
"autocomplete":{
Expand Down
4 changes: 2 additions & 2 deletions src/monggregate/search/operators/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/monggregate/search/operators/equals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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":{
Expand Down
3 changes: 2 additions & 1 deletion src/monggregate/search/operators/exists.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"""

from monggregate.base import Expression
from monggregate.search.operators.operator import SearchOperator

class Exists(SearchOperator):
Expand All @@ -51,7 +52,7 @@ class Exists(SearchOperator):


@property
def expression(self) -> dict:
def expression(self) -> Expression:

return self.express({
"exists" : {
Expand Down
8 changes: 4 additions & 4 deletions src/monggregate/search/operators/more_like_this.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -105,15 +105,15 @@ 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.")

return v

@property
def expression(self) -> dict:
def expression(self) -> Expression:

return self.express({
"moreLikeThis" : {
Expand Down
8 changes: 4 additions & 4 deletions src/monggregate/search/operators/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/monggregate/search/operators/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"""

from monggregate.base import Expression
from monggregate.search.operators.operator import SearchOperator

class Regex(SearchOperator):
Expand Down Expand Up @@ -81,7 +82,7 @@ class Regex(SearchOperator):
score : dict | None = None

@property
def expression(self) -> dict:
def expression(self) -> Expression:

return self.express({
"regex":{
Expand Down
4 changes: 2 additions & 2 deletions src/monggregate/search/operators/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/monggregate/search/operators/wildcard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand Down

0 comments on commit e3b7cab

Please sign in to comment.