Skip to content

Commit 5c07eb7

Browse files
committed
Add search lookup.
1 parent 1c2888f commit 5c07eb7

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

django_mongodb_backend/expressions/search.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django.db import NotSupportedError
2-
from django.db.models import Expression, FloatField
2+
from django.db.models import CharField, Expression, FloatField, TextField
33
from django.db.models.expressions import F, Value
4+
from django.db.models.lookups import Lookup
5+
6+
from ..query_utils import process_lhs, process_rhs
47

58

69
def cast_as_value(value):
@@ -989,3 +992,27 @@ def __init__(self, definitions=None):
989992

990993
def as_mql(self, compiler, connection):
991994
return self._definitions
995+
996+
997+
class SearchTextLookup(Lookup):
998+
lookup_name = "search"
999+
1000+
def __init__(self, lhs, rhs):
1001+
super().__init__(lhs, rhs)
1002+
self.lhs = SearchText(self.lhs, self.rhs)
1003+
self.rhs = Value(0)
1004+
1005+
def __str__(self):
1006+
return f"SearchText({self.lhs}, {self.rhs})"
1007+
1008+
def __repr__(self):
1009+
return f"SearchText({self.lhs}, {self.rhs})"
1010+
1011+
def as_mql(self, compiler, connection):
1012+
lhs_mql = process_lhs(self, compiler, connection)
1013+
value = process_rhs(self, compiler, connection)
1014+
return {"$gte": [lhs_mql, value]}
1015+
1016+
1017+
CharField.register_lookup(SearchTextLookup)
1018+
TextField.register_lookup(SearchTextLookup)

0 commit comments

Comments
 (0)