[bug 1164565] Fix document results in search suggest API

This switches document results to use DocumentDetailSerializer. It also
fixes DocumentDetailSerializer to include the document summary which is
on the current revision and the document url.
This commit is contained in:
Will Kahn-Greene 2015-05-13 15:39:10 -04:00
Родитель 1506092e9b
Коммит f48116bf87
5 изменённых файлов: 30 добавлений и 22 удалений

Просмотреть файл

@ -80,10 +80,15 @@ With an HTTP 200, you'll get back a set of results in JSON.
{
"documents": [
{
"id": ... # id of kb article
"title": ... # title of kb article
"url": ... # url of kb article
"slug": ... # slug of kb article
"locale": ... # locale of the article
"products": ... # list of products for the article
"topics": ... # list of topics for the article
"summary": ... # paragraph summary of kb article (plaintext)
"html": ... # html of the article
}
...
],

Просмотреть файл

@ -9,7 +9,8 @@ from kitsune.questions.models import Question, QuestionMappingType
from kitsune.questions.api import QuestionSerializer
from kitsune.search import es_utils
from kitsune.sumo.api import GenericAPIException
from kitsune.wiki.models import DocumentMappingType
from kitsune.wiki.api import DocumentDetailSerializer
from kitsune.wiki.models import Document, DocumentMappingType
def positive_integer(value):
@ -101,9 +102,11 @@ def _question_suggestions(searcher, text, locale, product, max_results):
questions = []
searcher = _query(searcher, QuestionMappingType, search_filter, text, locale)
for result in searcher[:max_results]:
q = Question.objects.get(id=result['id'])
questions.append(QuestionSerializer(instance=q).data)
question_ids = [result['id'] for result in searcher]
questions = [
QuestionSerializer(instance=q).data
for q in Question.objects.filter(id__in=question_ids)
]
return questions
@ -124,13 +127,12 @@ def _document_suggestions(searcher, text, locale, product, max_results):
documents = []
searcher = _query(searcher, DocumentMappingType, search_filter, text, locale)
for result in searcher[:max_results]:
documents.append({
'title': result['document_title'],
'slug': result['document_slug'],
'summary': result['document_summary'],
'url': result['url'],
})
doc_ids = [result['id'] for result in searcher]
documents = [
DocumentDetailSerializer(instance=doc).data
for doc in Document.objects.filter(id__in=doc_ids)
]
return documents

Просмотреть файл

@ -222,14 +222,6 @@ class SuggestViewTests(ElasticTestCase):
req = self.client.get(reverse('search.suggest'), {'q': 'emails', 'locale': 'fr'})
eq_([d['slug'] for d in req.data['documents']], [d1.slug])
def test_document_fields(self):
self._make_document()
self.refresh()
req = self.client.get(reverse('search.suggest'), {'q': 'emails'})
eq_(len(req.data['documents']), 1)
eq_(sorted(req.data['documents'][0].keys()), ['slug', 'summary', 'title', 'url'])
def test_serializer_fields(self):
"""Test that fields from the serializer are included."""
self._make_question()

Просмотреть файл

@ -19,10 +19,13 @@ class DocumentShortSerializer(serializers.ModelSerializer):
class DocumentDetailSerializer(DocumentShortSerializer):
summary = serializers.CharField(source='summary', read_only=True)
url = serializers.CharField(source='get_absolute_url', read_only=True)
class Meta:
model = Document
fields = ('id', 'title', 'slug', 'locale', 'products', 'topics',
'html')
fields = ('id', 'title', 'slug', 'url', 'locale', 'products', 'topics',
'summary', 'html')
class DocumentList(LocaleNegotiationMixin, generics.ListAPIView):

Просмотреть файл

@ -324,6 +324,12 @@ class Document(NotificationsMixin, ModelBase, BigVocabTaggableMixin,
return ''
return self.current_revision.content_parsed
@property
def summary(self):
if not self.current_revision:
return ''
return self.current_revision.summary
@property
def language(self):
return settings.LANGUAGES_DICT[self.locale.lower()]
@ -720,8 +726,8 @@ class DocumentMappingType(SearchMappingType):
d['document_is_archived'] = obj.is_archived
d['document_display_order'] = obj.original.display_order
d['document_summary'] = obj.summary
if obj.current_revision is not None:
d['document_summary'] = obj.current_revision.summary
d['document_keywords'] = obj.current_revision.keywords
d['updated'] = int(time.mktime(
obj.current_revision.created.timetuple()))