Skip to content

Commit

Permalink
Merge pull request #1824 from MTG/tag-browse-no-solr
Browse files Browse the repository at this point in the history
Report an error during tag browse if there are no sounds in solr
  • Loading branch information
ffont authored Feb 11, 2025
2 parents 5cbc844 + 6c492c0 commit 8904da9
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions tags/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
from django.http import Http404, HttpResponsePermanentRedirect, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
import sentry_sdk

from search.views import search_view_helper
from tags.models import Tag, FS1Tag
from utils.search import SearchEngineException
from utils.search.search_sounds import perform_search_engine_query

search_logger = logging.getLogger("search")
Expand Down Expand Up @@ -62,18 +64,26 @@ def tags(request, multiple_tags=None):
if 'sqp' in tvars and not tvars['sqp'].get_tags_in_filters():
initial_tagcloud = cache.get('initial_tagcloud')
if initial_tagcloud is None:
# If tagcloud is not cached, make a query to retrieve it and save it to cache
results, _ = perform_search_engine_query(dict(
textual_query='',
query_filter= "*:*",
num_sounds=1,
facets={settings.SEARCH_SOUNDS_FIELD_TAGS: {'limit': 200}},
group_by_pack=True,
group_counts_as_one_in_facets=False,
))
initial_tagcloud = [dict(name=f[0], count=f[1], browse_url=reverse('tags', args=[f[0]])) for f in results.facets[settings.SEARCH_SOUNDS_FIELD_TAGS]]
cache.set('initial_tagcloud', initial_tagcloud, 60 * 60 * 12) # cache for 12 hours
tvars.update({'initial_tagcloud': initial_tagcloud})
try:
# If tagcloud is not cached, make a query to retrieve it and save it to cache
results, _ = perform_search_engine_query(dict(
textual_query='',
query_filter= "*:*",
num_sounds=1,
facets={settings.SEARCH_SOUNDS_FIELD_TAGS: {'limit': 200}},
group_by_pack=True,
group_counts_as_one_in_facets=False,
))
if settings.SEARCH_SOUNDS_FIELD_TAGS in results.facets:
initial_tagcloud = [dict(name=f[0], count=f[1], browse_url=reverse('tags', args=[f[0]])) for f in results.facets.get(settings.SEARCH_SOUNDS_FIELD_TAGS, [])]
cache.set('initial_tagcloud', initial_tagcloud, 60 * 60 * 12) # cache for 12 hours
tvars.update({'initial_tagcloud': initial_tagcloud})
else:
tvars.update({'error_text': 'There was an error while loading results, please try again later.'})
except SearchEngineException as e:
search_logger.info(f'Tag browse error: Could probably not connect to Solr - {e}')
sentry_sdk.capture_exception(e) # Manually capture exception so it has more info and Sentry can organize it properly
tvars.update({'error_text': 'The search server could not be reached, please try again later.'})

return render(request, 'search/search.html', tvars)

Expand Down

0 comments on commit 8904da9

Please sign in to comment.