Skip to content

Commit 3ed0225

Browse files
committed
Add Equals operator.
1 parent 6686477 commit 3ed0225

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

django_mongodb_backend/expressions/search.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,46 @@ def search_operator(self, compiler, connection):
113113
if self.token_order is not None:
114114
params["tokenOrder"] = self.token_order.value
115115
return {"autocomplete": params}
116+
117+
118+
class SearchEquals(SearchExpression):
119+
"""
120+
Atlas Search expression that matches documents with a field equal to the given value.
121+
122+
This expression uses the `equals` operator to perform exact matches
123+
on fields indexed in a MongoDB Atlas Search index.
124+
125+
Example:
126+
SearchEquals("category", "fiction")
127+
128+
Args:
129+
path: The document path to compare (as string or expression).
130+
value: The exact value to match against.
131+
score: Optional expression to modify the relevance score.
132+
133+
Reference: https://www.mongodb.com/docs/atlas/atlas-search/equals/
134+
"""
135+
136+
def __init__(self, path, value, score=None):
137+
self.path = cast_as_field(path)
138+
self.value = cast_as_value(value)
139+
self.score = score
140+
super().__init__()
141+
142+
def get_search_fields(self, compiler, connection):
143+
return {self.path.as_mql(compiler, connection, as_path=True)}
144+
145+
def get_source_expressions(self):
146+
return [self.path, self.value]
147+
148+
def set_source_expressions(self, exprs):
149+
self.path, self.value = exprs
150+
151+
def search_operator(self, compiler, connection):
152+
params = {
153+
"path": self.path.as_mql(compiler, connection, as_path=True),
154+
"value": self.value.value,
155+
}
156+
if self.score is not None:
157+
params["score"] = self.score.as_mql(compiler, connection)
158+
return {"equals": params}

0 commit comments

Comments
 (0)