class AnswerBaseIndex(indexes.SearchIndex, indexes.Indexable):
    text = fields.CharFieldWithSynonyms(document=True,
                                        use_template=True,
                                        boost=10.0)
    autocomplete = indexes.EdgeNgramField(use_template=True, indexed=True)
    url = indexes.CharField(use_template=True, indexed=False)
    tags = indexes.MultiValueField(indexed=True, boost=10.0)
    language = indexes.CharField(model_attr='language')
    suggestions = indexes.FacetCharField()

    def prepare_answer(self, obj):
        data = super(AnswerBaseIndex, self).prepare(obj)
        if obj.question.lower().startswith('what is'):
            data['boost'] = 2.0
        return data

    def prepare_tags(self, obj):
        return obj.clean_search_tags

    def prepare(self, obj):
        data = super(AnswerBaseIndex, self).prepare(obj)
        data['suggestions'] = data['text']
        return data

    def get_model(self):
        return AnswerPage

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(live=True,
                                               redirect_to_page=None)
Exemple #2
0
class AnswerPageIndex(indexes.SearchIndex, indexes.Indexable):
    text = fields.CharFieldWithSynonyms(
        document=True,
        use_template=True,
        boost=10.0)
    autocomplete = indexes.EdgeNgramField(
        use_template=True)
    url = indexes.CharField(model_attr='url', indexed=False)
    tags = indexes.MultiValueField(
        boost=10.0)
    language = indexes.CharField(
        model_attr='language')
    portal_topics = indexes.MultiValueField()
    portal_categories = indexes.MultiValueField()
    suggestions = indexes.FacetCharField()
    preview = indexes.CharField(indexed=False)

    def prepare_preview(self, page):
        raw_text = extract_raw_text(page.answer_content.stream_data)
        full_text = strip_tags(" ".join([page.short_answer, raw_text]))
        return truncatissimo(full_text)

    def prepare_tags(self, page):
        return page.clean_search_tags

    def prepare_portal_topics(self, page):
        return [topic.heading for topic in page.portal_topic.all()]

    def prepare_portal_categories(self, page):
        return [topic.heading for topic in page.portal_category.all()]

    def prepare(self, page):
        self.prepared_data = super(AnswerPageIndex, self).prepare(page)
        self.prepared_data['suggestions'] = self.prepared_data['text']
        return self.prepared_data

    def get_model(self):
        from ask_cfpb.models import AnswerPage
        return AnswerPage

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(
            live=True, redirect_to_page=None)
Exemple #3
0
class SpanishBaseIndex(indexes.SearchIndex, indexes.Indexable):
    text = fields.CharFieldWithSynonyms(language='es',
                                        document=True,
                                        use_template=True,
                                        boost=10.0)
    autocomplete = indexes.EdgeNgramField(use_template=True, indexed=True)
    url = indexes.CharField(use_template=True, indexed=False)
    tags = indexes.MultiValueField(indexed=True, boost=10.0)
    last_edited = indexes.DateTimeField(indexed=True,
                                        null=True,
                                        model_attr='last_edited_es',
                                        boost=2.0)
    suggestions = indexes.FacetCharField()

    def prepare_tags(self, obj):
        return obj.tags_es

    def prepare_spanish_answer_index(self, obj):
        data = super(SpanishBaseIndex, self).prepare(obj)
        if obj.question.lower().startswith('what is'):
            data['boost'] = 2.0
        return data

    def prepare(self, obj):
        data = super(SpanishBaseIndex, self).prepare(obj)
        data['suggestions'] = data['text']
        return data

    def get_model(self):
        return SpanishAnswerProxy

    def index_queryset(self, using=None):
        ids = [
            record.id for record in self.get_model().objects.all()
            if record.spanish_page and record.spanish_page.live is True
            and record.spanish_page.redirect_to is None
        ]
        return self.get_model().objects.filter(id__in=ids)