Skip to content

Commit

Permalink
Make diacritic_sensitive parameter optional to support $text operat…
Browse files Browse the repository at this point in the history
…or on Cosmos DB (#1089)

* Fix(find): allow excluding the diacritic sensitivity parameter

This fixes [the issue](#1088) with Azure CosmosDB for MongoDB which does not support diacritic sensitivity.

* Docs(find): explain when to set `diacritic_sensitive` to `None`

* Test(find): test when `diacritic_sensitive` is set to `None`
  • Loading branch information
mykolaskrynnyk authored Dec 18, 2024
1 parent 0b5c49f commit a2e6a5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions beanie/odm/operators/find/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,25 @@ class Sample(Document):
MongoDB doc:
<https://docs.mongodb.com/manual/reference/operator/query/text/>
Note: if you need to run a query against Azure Cosmos DB for MongoDB,
which does not support diacritic sensitivity yet, you can set
`diacritic_sensitive` argument to `None` to exclude it from the query.
"""

def __init__(
self,
search: str,
language: Optional[str] = None,
case_sensitive: bool = False,
diacritic_sensitive: bool = False,
diacritic_sensitive: Optional[bool] = False,
):
"""
:param search: str
:param language: Optional[str] = None
:param case_sensitive: bool = False
:param diacritic_sensitive: bool = False
:param diacritic_sensitive: Optional[bool] = False
"""
self.search = search
self.language = language
Expand All @@ -168,11 +172,14 @@ def query(self):
"$text": {
"$search": self.search,
"$caseSensitive": self.case_sensitive,
"$diacriticSensitive": self.diacritic_sensitive,
}
}
if self.language:
expression["$text"]["$language"] = self.language
if self.diacritic_sensitive is not None:
expression["$text"]["$diacriticSensitive"] = (
self.diacritic_sensitive
)
return expression


Expand Down
7 changes: 7 additions & 0 deletions tests/odm/operators/find/test_evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ async def test_text():
"$diacriticSensitive": True,
}
}
q = Text("something", diacritic_sensitive=None)
assert q == {
"$text": {
"$search": "something",
"$caseSensitive": False,
}
}
q = Text("something", language="test")
assert q == {
"$text": {
Expand Down

0 comments on commit a2e6a5a

Please sign in to comment.