Exclude filtered sources from recommendations endpoint
@@ -171,6 +171,29 @@ def _apply_filter(s: Search, search_params, param_name, renamed_param=None):
else:
return s
+def _exclude_filtered(s: Search):
+ """
+ Hide data sources from the catalog dynamically.
+ """
+ filter_cache_key = 'filtered_providers'
+ filtered_providers = cache.get(key=filter_cache_key)
+ if not filtered_providers:
+ filtered_providers = models.ContentProvider.objects\
+ .filter(filter_content=True)\
+ .values('provider_identifier')
+ cache.set(
+ key=filter_cache_key,
+ timeout=CACHE_TIMEOUT,
+ value=filtered_providers
+ )
+ to_exclude = [f['provider_identifier'] for f in filtered_providers]
+ s = s.exclude('terms', provider=to_exclude)
+ return s
+
+def _exclude_mature_by_param(s: Search, search_params):
+ if not search_params.data['mature']:
+ s = s.exclude('term', mature=True)
+ return s
def search(search_params, index, page_size, ip, request,
filter_dead, page=1) -> Tuple[List[Hit], int, int]:
@@ -208,23 +231,9 @@ def search(search_params, index, page_size, ip, request,
api_field, elasticsearch_field = tup
s = _apply_filter(s, search_params, api_field, elasticsearch_field)
- # Exclude mature content unless explicitly enabled by the requester
- if not search_params.data['mature']:
- s = s.exclude('term', mature=True)
- # Hide data sources from the catalog dynamically.
- filter_cache_key = 'filtered_providers'
- filtered_providers = cache.get(key=filter_cache_key)
- if not filtered_providers:
- filtered_providers = models.ContentProvider.objects\
- .filter(filter_content=True)\
- .values('provider_identifier')
- cache.set(
- key=filter_cache_key,
- timeout=CACHE_TIMEOUT,
- value=filtered_providers
- )
- to_exclude = [f['provider_identifier'] for f in filtered_providers]
- s = s.exclude('terms', provider=to_exclude)
+ # Exclude mature content and disabled sources
+ s = _exclude_mature_by_param(s, search_params)
+ s = _exclude_filtered(s)
# Search either by generic multimatch or by "advanced search" with
# individual field-level queries specified.
@@ -360,6 +369,7 @@ def related_images(uuid, index, request, filter_dead):
)
# Never show mature content in recommendations.
s = s.exclude('term', mature=True)
+ s = _exclude_filtered(s)
page_size = 10
page = 1
start, end = _get_query_slice(s, page_size, page, filter_dead)
