Skip to content

Commit d955285

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

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

django_mongodb_backend/expressions/search.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,44 @@ def search_operator(self, compiler, connection):
156156
if self.score is not None:
157157
params["score"] = self.score.as_mql(compiler, connection)
158158
return {"equals": params}
159+
160+
161+
class SearchExists(SearchExpression):
162+
"""
163+
Atlas Search expression that matches documents where a field exists.
164+
165+
This expression uses the `exists` operator to check whether a given
166+
path is present in the document. Useful for filtering documents that
167+
include (or exclude) optional fields.
168+
169+
Example:
170+
SearchExists("metadata__author")
171+
172+
Args:
173+
path: The document path to check (as string or expression).
174+
score: Optional expression to modify the relevance score.
175+
176+
Reference: https://www.mongodb.com/docs/atlas/atlas-search/exists/
177+
"""
178+
179+
def __init__(self, path, score=None):
180+
self.path = cast_as_field(path)
181+
self.score = score
182+
super().__init__()
183+
184+
def get_search_fields(self, compiler, connection):
185+
return {self.path.as_mql(compiler, connection, as_path=True)}
186+
187+
def get_source_expressions(self):
188+
return [self.path]
189+
190+
def set_source_expressions(self, exprs):
191+
(self.path,) = exprs
192+
193+
def search_operator(self, compiler, connection):
194+
params = {
195+
"path": self.path.as_mql(compiler, connection, as_path=True),
196+
}
197+
if self.score is not None:
198+
params["score"] = self.score.as_mql(compiler, connection)
199+
return {"exists": params}

0 commit comments

Comments
 (0)