Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced search queries #39

Open
jennydaman opened this issue Apr 11, 2021 · 0 comments
Open

Advanced search queries #39

jennydaman opened this issue Apr 11, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@jennydaman
Copy link
Collaborator

class PluginFilter(FilterSet):
"""
Filter class for the Plugin model.
"""
min_creation_date = django_filters.IsoDateTimeFilter(field_name='creation_date',
lookup_expr='gte')
max_creation_date = django_filters.IsoDateTimeFilter(field_name='creation_date',
lookup_expr='lte')
owner_username = django_filters.CharFilter(
field_name='meta__owner__username', lookup_expr='exact')
name = django_filters.CharFilter(field_name='meta__name', lookup_expr='icontains')
name_exact = django_filters.CharFilter(field_name='meta__name', lookup_expr='exact')
title = django_filters.CharFilter(field_name='meta__title', lookup_expr='icontains')
category = django_filters.CharFilter(field_name='meta__category',
lookup_expr='icontains')
type = django_filters.CharFilter(field_name='meta__type', lookup_expr='exact')
description = django_filters.CharFilter(field_name='description',
lookup_expr='icontains')
name_title_category = django_filters.CharFilter(method='search_name_title_category')
name_latest = django_filters.CharFilter(method='search_latest')
name_exact_latest = django_filters.CharFilter(method='search_latest')
def search_name_title_category(self, queryset, name, value):
"""
Custom method to get a filtered queryset with all plugins for which name or title
or category matches the search value.
"""
# construct the full lookup expression.
lookup = models.Q(meta__name__icontains=value)
lookup = lookup | models.Q(meta__title__icontains=value)
lookup = lookup | models.Q(meta__category__icontains=value)
return queryset.filter(lookup)
def search_latest(self, queryset, name, value):
"""
Custom method to get a filtered queryset with the latest version according to
creation date of all plugins whose name matches the search value.
"""
if name == 'name_exact_latest':
qs = queryset.filter(meta__name=value)
return qs.order_by('-creation_date')[:1]
else:
qs = queryset.filter(meta__name__icontains=value)
qs = qs.order_by('meta', '-creation_date')
result_id_list = []
meta_id = 0
for pl in qs:
pl_meta_id = pl.meta.id
if pl_meta_id != meta_id:
result_id_list.append(pl.id)
meta_id = pl_meta_id
return qs.filter(pk__in=result_id_list)
class Meta:
model = Plugin
fields = ['id', 'name', 'name_latest', 'name_exact', 'name_exact_latest',
'dock_image', 'type', 'category', 'owner_username', 'min_creation_date',
'max_creation_date', 'title', 'version', 'description',
'name_title_category']

Currently, only a few search queries are supported. By the looks of it, these are

fields = ['id', 'name', 'name_latest', 'name_exact', 'name_exact_latest',
'dock_image', 'type', 'category', 'owner_username', 'min_creation_date',
'max_creation_date', 'title', 'version', 'description',
'name_title_category']

These options are sufficient for most use cases. However there are some fields, such as author, which currently are not searchable.

More fields should be searchable, and individually searchable.

It would be a pain to implement the cross-product of fields as specific query-string options e.g. name_title_category. A preferred paradigm would be:

GET /api/v1/plugins/search/?name=fetal&title=fetal&category=fetal

Moreover, it should be possible to define the boolean operations to apply on search terms. Maybe this would look like

GET /api/v1/plugins/search/?q=(title%3D%22fetal%22ORname%3D%22fetal%22)ANDauthor%3D%22fetal%22

An advanced implementation of search queries might use a search engine like ElasticSearch.

@jennydaman jennydaman added the enhancement New feature or request label Apr 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant