django-rest-framework-datatables icon indicating copy to clipboard operation
django-rest-framework-datatables copied to clipboard

Smart Filtering on the Server Side

Open maysu1914 opened this issue 4 years ago • 2 comments

Do we have any support for smart filtering on the server-side?

https://datatables.net/forums/discussion/24637/smart-search-whith-serverside-ssp#Comment_68324 https://datatables.net/forums/discussion/8045/smart-filtering-does-not-work-on-server-side-processing

Thank you for this package, it is very useful.

maysu1914 avatar Jul 09 '21 15:07 maysu1914

I guess smart filtering is splitting the search string into words and do the search for each word ? No this is not supported at the moment, If you want to help with a pull request, you're welcome.

izimobil avatar Jul 09 '21 15:07 izimobil

my ugly solution: use https://github.com/vsemionov/django-rest-fuzzysearch little magic:

# region 'SmartSearch'
class DatatablesFilterBackend2(DatatablesFilterBackend):
    def get_q(self, datatables_query):
        q = Q()
        return q

    def filter_queryset(self, request, queryset, view):
        queryset = super().filter_queryset(request, queryset, view).distinct()
        return queryset


class SmartFuzzySearchMixin:
    filter_backends = (RankedFuzzySearchFilter, DatatablesFilterBackend2)
    min_rank = 0.1

    # search_fields = ('id', )  # use search by id

    def __init__(self):
        if hasattr(self, 'search_fields'):
            self.search_fields = ('id', ) + self.search_fields # noqa
        pass
# endregion

.
.
.
class RequestResultViewSet(SmartFuzzySearchMixin, viewsets.ReadOnlyModelViewSet):
    search_fields = ('id', 'context') # define fields for smart search
# the you usual code, not rewrite filter_backends !

for speedup use GinIndex for search fields in models and i have fully working django-rest-framework-datatables but with FuzzySearch filter i know, ugly, but work fine

mmmcorpsvit avatar Sep 12 '21 23:09 mmmcorpsvit