Skip to content

Commit

Permalink
OPEN-2794: Add an option JSON or RAW Solr response option to individu…
Browse files Browse the repository at this point in the history
…al searches. Requites an update to the Django models.py
  • Loading branch information
thriuin committed Sep 19, 2023
1 parent edc4e26 commit b762cbd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
1 change: 1 addition & 0 deletions oc_search/settings-sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@

CORS_ALLOW_ALL_ORIGINS = True
CORS_REPLACE_HTTPS_REFERER = True
SECURE_REFERRER_POLICY = 'unsafe-url'

ROOT_URLCONF = 'oc_search.urls'

Expand Down
3 changes: 2 additions & 1 deletion search/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class SearchAdmin(admin.ModelAdmin):
('Disabled', {'fields': ('is_disabled', 'disabled_message_en', 'disabled_message_fr')}),
('Results', {'fields': ('results_page_size', 'results_sort_order_en', 'results_sort_order_fr',
'results_sort_order_display_en', 'results_sort_order_display_fr',
'results_sort_default_en','results_sort_default_fr')}),
'results_sort_default_en','results_sort_default_fr',
'json_response', 'raw_solr_response')}),
('Templates', {'fields': ('page_template', 'record_template', 'breadcrumb_snippet', 'footer_snippet',
'info_message_snippet', 'about_message_snippet', 'header_js_snippet',
'header_css_snippet', 'body_js_snippet', 'search_item_snippet',
Expand Down
6 changes: 6 additions & 0 deletions search/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class Search(models.Model):
help_text="Indicate if the this search will be using Solr's 'More Like This' functionality")
mlt_items = models.IntegerField(blank=True, default=10, verbose_name="No. Items returned for More-Like-This",
help_text="Number of itemsto show on the More-Like-This search results page. Default is 10")
json_response = models.BooleanField(blank=False, default=False, verbose_name="Enable JSON format response",
help_text="Allows JSON style responses suitable for REST API")
raw_solr_response = models.BooleanField(blank=False, default=False, verbose_name="Enable raw Solr format response",
help_text="Allows returning the raw Solr engine responses suitable for REST API")

def __str__(self):
return '%s (%s)' % (self.label_en, self.search_id)
Expand Down Expand Up @@ -148,6 +152,8 @@ def from_dict(self, data_dict: dict):
self.alt_formats = data_dict["alt_formats"]
self.mlt_enabled = data_dict["mlt_enabled"]
self.mlt_items = data_dict["mlt_items"]
self.json_response = data_dict["json_response"]
self.raw_solr_response = data_dict["raw_solr_response"]


class Field(models.Model):
Expand Down
9 changes: 4 additions & 5 deletions search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,7 @@ def get(self, request: HttpRequest, lang='en', search_type=''):
context['export_search_path'] = request.get_full_path()
context['about_msg'] = self.searches[search_type].about_message_fr if lang == 'fr' else self.searches[search_type].about_message_en
context['search_toggle'] = self.reverse_search_alias_en[search_type] if lang == 'fr' else self.reverse_search_alias_fr[search_type]
if hasattr(settings, "JSON_DOWNLOADS_ALLOWED") and isinstance(settings.JSON_DOWNLOADS_ALLOWED, bool):
context['json_download_allowed'] = settings.JSON_DOWNLOADS_ALLOWED
else:
context['json_download_allowed'] = False
context['json_download_allowed'] = self.searches[search_type].json_response

# Get search drop in message:
context["general_msg"] = ""
Expand Down Expand Up @@ -478,7 +475,7 @@ def get(self, request: HttpRequest, lang='en', search_type=''):
self.codes_fr[search_type] if lang == 'fr' else self.codes_en[search_type])
# Users can optionally get the search results as a JSON object instead of the normal HTML page
search_format = request.GET.get("search_format", "html")
if search_format == 'json':
if search_format == 'json' and self.searches[search_type].json_response:
full_facet_dict = {}
for facet in context['facets'].keys():
facet_list = []
Expand Down Expand Up @@ -507,6 +504,8 @@ def get(self, request: HttpRequest, lang='en', search_type=''):
'facets': full_facet_dict,
'selected_facets': context['selected_facets'] if context['selected_facets'] else []}
return JsonResponse(doc_dict)
elif search_format == 'solr' and self.searches[search_type].raw_solr_response:
return JsonResponse(solr_response.data)
else:
json_link = str(request.get_full_path())
if json_link.endswith("/"):
Expand Down

0 comments on commit b762cbd

Please sign in to comment.