|
1 | 1 | from django.db import NotSupportedError
|
2 |
| -from django.db.models import Expression, FloatField |
| 2 | +from django.db.models import CharField, Expression, FloatField, TextField |
3 | 3 | 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 |
4 | 7 |
|
5 | 8 |
|
6 | 9 | def cast_as_value(value):
|
@@ -989,3 +992,27 @@ def __init__(self, definitions=None):
|
989 | 992 |
|
990 | 993 | def as_mql(self, compiler, connection):
|
991 | 994 | 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