Beispiel #1
0
    def related_documents(self):
        """Return documents that are 'morelikethis' one"""
        if not self.product:
            return []

        # First try to get the results from the cache
        key = 'questions_question:related_docs:%s' % self.id
        documents = cache.get(key)
        if documents is not None:
            statsd.incr('questions.related_documents.cache.hit')
            log.debug(
                'Getting MLT documents for {question} from cache.'.format(
                    question=repr(self)))
            return documents

        try:
            statsd.incr('questions.related_documents.cache.miss')
            s = Document.get_mapping_type().search()
            documents = (s.values_dict('id', 'document_title', 'url').filter(
                document_locale=self.locale,
                document_is_archived=False,
                document_category__in=settings.IA_DEFAULT_CATEGORIES,
                product__in=[self.product.slug]
            ).query(
                __mlt={
                    'fields':
                    ['document_title', 'document_summary', 'document_content'],
                    'like_text':
                    self.title,
                    'min_term_freq':
                    1,
                    'min_doc_freq':
                    1
                })[:3])
            documents = list(documents)
            cache.add(key, documents)
        except ES_EXCEPTIONS:
            statsd.incr('questions.related_documents.esexception')
            log.exception('ES MLT related_documents')
            documents = []

        return documents
Beispiel #2
0
    def related_documents(self):
        """Return documents that are 'morelikethis' one"""
        if not self.product:
            return []

        # First try to get the results from the cache
        key = 'questions_question:related_docs:%s' % self.id
        documents = cache.get(key)
        if documents is not None:
            statsd.incr('questions.related_documents.cache.hit')
            log.debug('Getting MLT documents for {question} from cache.'
                      .format(question=repr(self)))
            return documents

        try:
            statsd.incr('questions.related_documents.cache.miss')
            s = Document.get_mapping_type().search()
            documents = (
                s.values_dict('id', 'document_title', 'url')
                .filter(document_locale=self.locale,
                        document_is_archived=False,
                        document_category__in=settings.IA_DEFAULT_CATEGORIES,
                        product__in=[self.product.slug])
                .query(__mlt={
                    'fields': ['document_title', 'document_summary',
                               'document_content'],
                    'like_text': self.title,
                    'min_term_freq': 1,
                    'min_doc_freq': 1})
                [:3])
            documents = list(documents)
            cache.add(key, documents)
        except ES_EXCEPTIONS:
            statsd.incr('questions.related_documents.esexception')
            log.exception('ES MLT related_documents')
            documents = []

        return documents