Skip to content

Commit 4f99a4d

Browse files
committed
Add search text lookup.
1 parent bbfd2b6 commit 4f99a4d

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

django_mongodb_backend/compiler.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def _prepare_search_query_for_aggregation_pipeline(self, order_by):
128128
self._prepare_search_expressions_for_pipeline(
129129
self.having, annotation_group_idx, replacements
130130
)
131+
self._prepare_search_expressions_for_pipeline(
132+
self.get_where(), annotation_group_idx, replacements
133+
)
131134
return replacements
132135

133136
def _prepare_annotations_for_aggregation_pipeline(self, order_by):
@@ -291,6 +294,8 @@ def pre_sql_setup(self, with_col_aliases=False):
291294
for target, expr in self.query.annotation_select.items()
292295
}
293296
self.order_by_objs = [expr.replace_expressions(all_replacements) for expr, _ in order_by]
297+
where_ = self.get_where().replace_expressions(all_replacements)
298+
self.set_where(where_)
294299
return extra_select, order_by, group_by
295300

296301
def execute_sql(
@@ -692,6 +697,9 @@ def _get_ordering(self):
692697
def get_where(self):
693698
return getattr(self, "where", self.query.where)
694699

700+
def set_where(self, value):
701+
self.where = value
702+
695703
def explain_query(self):
696704
# Validate format (none supported) and options.
697705
options = self.connection.ops.explain_query_prefix(
@@ -778,6 +786,9 @@ def check_query(self):
778786
def get_where(self):
779787
return self.query.where
780788

789+
def set_where(self, value):
790+
self.query.where = value
791+
781792
@cached_property
782793
def collection_name(self):
783794
return self.query.base_table
@@ -849,6 +860,9 @@ def check_query(self):
849860
def get_where(self):
850861
return self.query.where
851862

863+
def set_where(self, value):
864+
self.query.where = value
865+
852866
@cached_property
853867
def collection_name(self):
854868
return self.query.base_table

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):
@@ -1009,3 +1012,27 @@ def __init__(self, definitions=None):
10091012

10101013
def as_mql(self, compiler, connection):
10111014
return self.definitions
1015+
1016+
1017+
class SearchTextLookup(Lookup):
1018+
lookup_name = "search"
1019+
1020+
def __init__(self, lhs, rhs):
1021+
super().__init__(lhs, rhs)
1022+
self.lhs = SearchText(self.lhs, self.rhs)
1023+
self.rhs = Value(0)
1024+
1025+
def __str__(self):
1026+
return f"SearchText({self.lhs}, {self.rhs})"
1027+
1028+
def __repr__(self):
1029+
return f"SearchText({self.lhs}, {self.rhs})"
1030+
1031+
def as_mql(self, compiler, connection):
1032+
lhs_mql = process_lhs(self, compiler, connection)
1033+
value = process_rhs(self, compiler, connection)
1034+
return {"$gte": [lhs_mql, value]}
1035+
1036+
1037+
CharField.register_lookup(SearchTextLookup)
1038+
TextField.register_lookup(SearchTextLookup)

tests/queries_/test_search.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ def test_search_text(self):
298298
qs = Article.objects.annotate(score=SearchText(path="body", query="lazy"))
299299
self.wait_for_assertion(lambda: self.assertCountEqual([self.article], qs))
300300

301+
def test_search_lookup(self):
302+
qs = Article.objects.filter(body__search="lazy")
303+
self.wait_for_assertion(lambda: self.assertCountEqual([self.article], qs))
304+
301305
def test_search_text_with_fuzzy_and_criteria(self):
302306
qs = Article.objects.annotate(
303307
score=SearchText(

0 commit comments

Comments
 (0)