Repository: mdn/kuma

Issue:
Search json endpoint is not available in new front-end
**Summary**
https://twitter.com/klaascuvelier/status/1182203293117886464


**Steps To Reproduce (STR)**
_How can we reproduce the problem?_

Go to https://developer.mozilla.org/en-US/search.json?q=array
 


**Actual behavior**
Blank page


**Expected behavior**
JSON is returned like it is now only at https://wiki.developer.mozilla.org/en-US/search.json?q=array


**Additional context**
There might be a few external services, twitter bots etc. that depend on this endpoint.



Discussion:
+1
@mesqueeb If you feel strongly for being able to reach that endpoint please let us know your use case. 
The Wiki was split up into a Wiki and a read-only site. 

We probably won't ever re-instate https://developer.mozilla.org/en-US/search.json?q=array but we might turn it into a permanent redirect to https://developer.mozilla.org/api/v1/search/en-US?q=array
Actually, @joedarc is that something you'd like to try? It'd warm you up a bit to how to hook up urls.py stuff. 
@peterbe Sure, i can take a look at this. 👍 
> @peterbe Sure, i can take a look at this. 👍

I think I know some good tricks. I hope. Take a look but don't venture too deep into a rabbit hole if you don't see an easy out. I think we just need to divorce `rest_framework` from the search entirely. 

Resolving patch:
diff --git a/kuma/search/urls.py b/kuma/search/urls.py
--- a/kuma/search/urls.py
+++ b/kuma/search/urls.py
@@ -1,11 +1,11 @@
 from django.conf.urls import url
-from rest_framework.urlpatterns import format_suffix_patterns
 
 from . import views
 
-# this allows using ".json" extensions for the view to force json output
-lang_base_urlpatterns = format_suffix_patterns(
-    [url(r'^$', views.search, name='search')])
+lang_base_urlpatterns = [
+    url(r'^$', views.search, name='search'),
+    url(r'^.(?P<format>json)$', views.SearchRedirectView.as_view())
+]
 
 
 lang_urlpatterns = [
diff --git a/kuma/search/views.py b/kuma/search/views.py
--- a/kuma/search/views.py
+++ b/kuma/search/views.py
@@ -1,6 +1,8 @@
 from django.shortcuts import render
+from django.urls import reverse_lazy
 from django.views.decorators.cache import never_cache
 from django.views.decorators.http import require_GET
+from django.views.generic import RedirectView
 from ratelimit.decorators import ratelimit
 
 from kuma.api.v1.views import search as search_api
@@ -37,6 +39,17 @@ def search(request, *args, **kwargs):
 wiki_search = SearchView.as_view()
 
 
+class SearchRedirectView(RedirectView):
+    permanent = True
+
+    def get_redirect_url(self, *args, **kwargs):
+        query_string = self.request.META.get('QUERY_STRING')
+        url = reverse_lazy('api.v1.search', kwargs={'locale': self.request.LANGUAGE_CODE})
+        if query_string:
+            url += '?' + query_string
+        return url
+
+
 @shared_cache_control(s_maxage=60 * 60 * 24 * 7)
 def plugin(request):
     """Render an OpenSearch Plugin."""


Test patch:
diff --git a/kuma/search/tests/test_views.py b/kuma/search/tests/test_views.py
--- a/kuma/search/tests/test_views.py
+++ b/kuma/search/tests/test_views.py
@@ -178,13 +178,10 @@ def test_index(self):
         expected = 'Search index: %s' % Index.objects.get_current().name
         assert expected in response.content.decode(response.charset)
 
-    def test_score(self):
+    def test_api_redirect(self):
         response = self.client.get('/en-US/search.json',
                                    HTTP_HOST=settings.WIKI_HOST)
-        assert response.status_code == 200
-        assert len(response.data['documents']) > 0
-        for document in response.data['documents']:
-            assert 'score' in document
+        assert response.status_code == 301
 
 
 @pytest.mark.parametrize('locale', ['en-US', 'fr'])