зеркало из https://github.com/mozilla/kitsune.git
[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:
Родитель
1506092e9b
Коммит
f48116bf87
|
@ -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()))
|
||||
|
|
Загрузка…
Ссылка в новой задаче