Skip to content

Commit d97d1a3

Browse files
Merge pull request #188 from GaetanF/master
Basic Implementation of Regexp Field Lookup
2 parents b792d32 + e97c858 commit d97d1a3

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/django_elasticsearch_dsl_drf/constants.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
'LOOKUP_FILTER_GEO_POLYGON',
2828
'LOOKUP_FILTER_PREFIX',
2929
'LOOKUP_FILTER_RANGE',
30+
'LOOKUP_FILTER_REGEXP',
3031
'LOOKUP_FILTER_TERM',
3132
'LOOKUP_FILTER_TERMS',
3233
'LOOKUP_FILTER_WILDCARD',
@@ -433,7 +434,7 @@
433434
LOOKUP_FILTER_EXISTS,
434435
LOOKUP_FILTER_PREFIX,
435436
LOOKUP_FILTER_WILDCARD,
436-
# LOOKUP_FILTER_REGEXP,
437+
LOOKUP_FILTER_REGEXP,
437438
# LOOKUP_FILTER_FUZZY,
438439
# LOOKUP_FILTER_TYPE,
439440

@@ -474,6 +475,7 @@
474475
LOOKUP_FILTER_TERMS,
475476
LOOKUP_FILTER_PREFIX,
476477
LOOKUP_FILTER_WILDCARD,
478+
LOOKUP_FILTER_REGEXP,
477479
LOOKUP_QUERY_CONTAINS,
478480
LOOKUP_QUERY_IN,
479481
LOOKUP_QUERY_STARTSWITH,

src/django_elasticsearch_dsl_drf/filter_backends/filtering/common.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
ALL_LOOKUP_FILTERS_AND_QUERIES,
1818
LOOKUP_FILTER_PREFIX,
1919
LOOKUP_FILTER_RANGE,
20+
LOOKUP_FILTER_REGEXP,
2021
LOOKUP_FILTER_TERMS,
2122
LOOKUP_FILTER_EXISTS,
2223
LOOKUP_FILTER_WILDCARD,
@@ -293,6 +294,35 @@ def apply_filter_range(cls, queryset, options, value):
293294
kwargs={options['field']: cls.get_range_params(value)}
294295
)
295296

297+
@classmethod
298+
def apply_filter_regexp(cls, queryset, options, value):
299+
"""Apply `reexp` filter.
300+
301+
Syntax:
302+
303+
/endpoint/?field_name__regexp={regexp}
304+
305+
Example:
306+
307+
http://localhost:8000/api/users/?age__regexp=1[6-9]
308+
http://localhost:8000/api/users/?age__regexp=2.*
309+
310+
:param queryset: Original queryset.
311+
:param options: Filter options.
312+
:param value: value to filter on.
313+
:type queryset: elasticsearch_dsl.search.Search
314+
:type options: dict
315+
:type value: str
316+
:return: Modified queryset.
317+
:rtype: elasticsearch_dsl.search.Search
318+
"""
319+
return cls.apply_filter(
320+
queryset=queryset,
321+
options=options,
322+
args=['regexp'],
323+
kwargs={options['field']: value}
324+
)
325+
296326
@classmethod
297327
def apply_query_exists(cls, queryset, options, value):
298328
"""Apply `exists` filter.
@@ -789,6 +819,12 @@ def filter_queryset(self, request, queryset, view):
789819
options,
790820
value)
791821

822+
# `regexp` filter lookup
823+
elif options['lookup'] == LOOKUP_FILTER_REGEXP:
824+
queryset = self.apply_filter_regexp(queryset,
825+
options,
826+
value)
827+
792828
# `exists` filter lookup
793829
elif options['lookup'] == LOOKUP_FILTER_EXISTS:
794830
queryset = self.apply_query_exists(queryset,

src/django_elasticsearch_dsl_drf/tests/test_filtering_common.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,19 @@ def test_field_filter_range(self):
193193
self.published_count
194194
)
195195

196+
def test_field_filter_regexp(self):
197+
"""Field filter regexp.
198+
199+
Example:
200+
201+
http://localhost:8000/api/users/?title__regexp=De.{8}Ins.*
202+
"""
203+
return self._field_filter_value(
204+
'title__regexp',
205+
'{}.{{8}}{}.*'.format(self.prefix[0:2], self.prefix[10:13]),
206+
self.prefix_count
207+
)
208+
196209
def test_field_filter_range_with_boost(self):
197210
"""Field filter range.
198211

0 commit comments

Comments
 (0)