Filter content providers from search results based on state of content_provider table
@@ -6,11 +6,12 @@ from elasticsearch_dsl import Q, Search, connections
from elasticsearch_dsl.response import Response
from cccatalog import settings
from django.core.cache import cache
+from cccatalog.api.models import ContentProvider
import logging as log
ELASTICSEARCH_MAX_RESULT_WINDOW = 10000
-
+CACHE_TIMEOUT = 60 * 5
def search(search_params, index, page_size, page=1) -> Response:
"""
@@ -49,6 +50,22 @@ def search(search_params, index, page_size, page=1) -> Response:
creator_filter = Q("term", creator=search_params.data['creator'])
s = s.filter('bool', should=creator_filter, minimum_should_match=1)
+ # Sometimes, it is desirable to hide content providers from the catalog
+ # without scrubbing them from the database or reindexing.
+ filter_cache_key = 'filtered_providers'
+ filtered_providers = cache.get(key=filter_cache_key)
+ if not filtered_providers:
+ filtered_providers = ContentProvider.objects\
+ .filter(filter_content=True)\
+ .values('provider_identifier')
+ cache.set(
+ key=filter_cache_key,
+ timeout=10,
+ value=filtered_providers
+ )
+ for filtered in filtered_providers:
+ s = s.exclude("match", provider=filtered['provider_identifier'])
+
# Search for keywords.
keywords = ' '.join(search_params.data['q'].lower().split(','))
s = s.query("constant_score", filter=Q("multi_match",
@@ -68,7 +85,6 @@ def get_providers(index):
:return: A dictionary mapping providers to the count of their images.`
"""
provider_cache_name = 'providers-' + index
- cache_timeout = 60 * 5
providers = cache.get(key=provider_cache_name)
if type(providers) == list:
# Invalidate old provider format.
@@ -93,7 +109,7 @@ def get_providers(index):
results = s.execute().aggregations['unique_providers']['buckets']
providers = {result['key']: result['doc_count'] for result in results}
cache.set(key=provider_cache_name,
- timeout=cache_timeout,
+ timeout=CACHE_TIMEOUT,
value=providers)
return providers
