Skip to content

Commit

Permalink
Added search API and basic search
Browse files Browse the repository at this point in the history
  • Loading branch information
robhudson committed Oct 27, 2013
1 parent fbc46b0 commit 21c9741
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
49 changes: 45 additions & 4 deletions readthedocs/restapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from django.conf import settings

from distlib.version import UnsupportedVersionError
from rest_framework import decorators
from rest_framework import permissions
from rest_framework import viewsets
from rest_framework import decorators, permissions, viewsets, status
from rest_framework.renderers import JSONPRenderer, JSONRenderer, BrowsableAPIRenderer
from rest_framework.response import Response

from betterversion.better import version_windows, BetterVersion
from builds.models import Version
from projects.models import Project, EmailHook
from search.indexes import Page as PageIndex, Project as ProjectIndex
from djangome import views as djangome

from .serializers import ProjectSerializer
Expand All @@ -29,7 +28,7 @@ def valid_versions(self, request, **kwargs):
"""
project = get_object_or_404(Project, pk=kwargs['pk'])
if not project.num_major or not project.num_minor or not project.num_point:
return Response({'error': 'Project does not support point version control.'})
return Response({'error': 'Project does not support point version control'}, status=status.HTTP_400_BAD_REQUEST)
versions = []
for ver in project.versions.all():
try:
Expand Down Expand Up @@ -177,3 +176,45 @@ def quick_search(request):
value = ':'.join(data.split(':')[6:])
ret_dict[key] = value
return Response({"results": ret_dict})

@decorators.api_view(['GET'])
@decorators.permission_classes((permissions.AllowAny,))
@decorators.renderer_classes((JSONRenderer, JSONPRenderer, BrowsableAPIRenderer))
def search(request):
project_id = request.GET.get('project', None)
version_slug = request.GET.get('version', 'latest')
query = request.GET.get('q', None)

if project_id:
# This is a search within a project -- do a Page search.
body = {
'filter': {
'term': {'project': project_id},
'term': {'version': version_slug},
},
'query': {
'bool': {
'should': [
{'match': {'title': {'query': query, 'boost': 10}}},
{'match': {'headers': {'query': query, 'boost': 5}}},
{'match': {'content': {'query': query}}},
]
}
}
}
results = PageIndex().search(body)

else:
body = {
'query': {
'bool': {
'should': [
{'match': {'name': {'query': query, 'boost': 10}}},
{'match': {'description': {'query': query}}},
]
}
}
}
results = ProjectIndex().search(body)

return Response({'results': results})
7 changes: 6 additions & 1 deletion readthedocs/search/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def update_aliases(self, new_index, delete=True):
if delete and old_index:
self.es.indices.delete(index=old_index)

def search(self, body):
return self.es.search(index=self._index, doc_type=self._type,
body=body)


class Project(Index):

Expand Down Expand Up @@ -252,7 +256,8 @@ def get_mapping(self):
def extract_document(self, data):
doc = {}

attrs = ('id', 'project', 'title', 'version', 'path', 'content')
attrs = ('id', 'project', 'title', 'headers', 'version', 'path',
'content')
for attr in attrs:
doc[attr] = data.get(attr, '')

Expand Down
1 change: 1 addition & 0 deletions readthedocs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
url(r'^api/v2/', include(router.urls)),
url(r'^api/v2/footer_html/$', 'restapi.views.footer_html', name='footer_html'),
url(r'^api/v2/quick_search/$', 'restapi.views.quick_search', name='quick_search'),
url(r'^api/v2/search/$', 'restapi.views.search', name='search'),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^feeds/new/$',
NewProjectsFeed(),
Expand Down

0 comments on commit 21c9741

Please sign in to comment.